📄 linux环境进程间通信(三).htm
字号:
</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Linux环境进程间通信(三).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#author">关于作者</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Linux环境进程间通信(三).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#rate">对本文的评价</A></TD></TR>
<TR class=left-nav-last>
<TD width=14><IMG class=display-img height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=14></TD>
<TD width=136><IMG class=display-img height=19 alt=""
src="Linux环境进程间通信(三).files/left-nav-corner.gif"
width=136></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=related colSpan=2><B class=related>相关链接:</B></TD></TR>
<TR class=rlinks>
<TD><IMG height=8 alt="" src="Linux环境进程间通信(三).files/rl-bullet.gif"
width=2></TD>
<TD><A class=rlinks
href="http://www.ibm.com/developerworks/cn/views/linux/articles.jsp">Linux
技术文档库</A></TD></TR><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- No content currently --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
<TR>
<TD width=14><IMG class=display-img height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=14></TD>
<TD width=136><IMG class=display-img height=19 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=136></TD></TR></TBODY></TABLE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- No content currently --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--></TD><!--LEFTNAV_END-->
<TD width="100%">
<TABLE id=content-table cellSpacing=0 cellPadding=0 width="100%"
border=0><TBODY>
<TR vAlign=top>
<TD width="100%">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><A name=main><IMG height=1 alt=跳转到主要内容
src="Linux环境进程间通信(三).files/c.gif" width=592
border=0></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10 height=18><IMG height=18 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=10></TD>
<TD width="100%"><IMG height=6 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=1><BR><A class=bctl
href="http://www.ibm.com/developerworks/cn/">developerWorks
中国</A><SPAN class=bct> > </SPAN><A
class=bctl
href="http://www.ibm.com/developerworks/cn/linux/">Linux</A><SPAN
class=bct> ></SPAN><IMG height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=1><BR>
<H1>Linux环境进程间通信(三)</H1>
<P id=subtitle><EM>消息队列</EM></P><IMG class=display-img
height=6 alt="" src="Linux环境进程间通信(三).files/c.gif" width=1></TD>
<TD class=no-print width=192><IMG height=18 alt=developerWorks
src="Linux环境进程间通信(三).files/dw.gif"
width=192></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10><IMG height=1 alt="" src="Linux环境进程间通信(三).files/c.gif"
width=10></TD>
<TD width="100%">
<TABLE class=no-print cellSpacing=0 cellPadding=0 width=160
align=right border=0>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=10></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small>文档选项</TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0
border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143
border=0><IMG height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=8>
<FORM name=email
action=https://www.ibm.com/developerworks/secure/email-it.jsp><INPUT
type=hidden
value="本系列文章中的前两部分,我们探讨管道及信号两种通信机制,本文将深入第三部分,介绍系统 V 消息队列及其相应 API。"
name=body><INPUT type=hidden value=Linux环境进程间通信(三)
name=subject><INPUT type=hidden value=cn name=lang>
<SCRIPT language=JavaScript type=text/javascript><!--document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>');//--></SCRIPT>
<NOSCRIPT>
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=8></TD>
<TD width=16><IMG height=16 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=16></TD>
<TD class=small width=122>
<P><SPAN class=ast>未显示需要 JavaScript
的文档选项</SPAN></P></TD></TR></NOSCRIPT></FORM></TBODY></TABLE></TD></TR></TBODY></TABLE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- this content will be automatically generated across all content areas --><BR><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><BR></TD></TR></TBODY></TABLE>
<P>级别: 初级</P>
<P><A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#author">郑彦兴</A>
(<A
href="mailto:mlinux@163.com?subject=Linux环境进程间通信(三)">mailto:mlinux@163.com?subject=Linux环境进程间通信(三)</A>)国防科大计算机学院<BR></P>
<P>2003 年 1 月 17 日</P>
<BLOCKQUOTE>本系列文章中的前两部分,我们探讨管道及信号两种通信机制,本文将深入第三部分,介绍系统 V 消息队列及其相应
API。</BLOCKQUOTE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
<P>消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点。作为早期unix通信机制之一的信号能够传送的信息量有限,后来虽然POSIX
1003.1b在信号的实时性方面作了拓广,使得信号在传递信息量方面有了相当程度的改进,但是信号这种通信方式更像"即时"的通信方式,它要求接受信号的进程在某个时间范围内对信号做出反应,因此该信号最多在接受信号进程的生命周期内才有意义,信号所传递的信息是接近于随进程持续的概念(process-persistent),见
<A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#listing1">附录
1</A>;管道及有名管道及有名管道则是典型的随进程持续IPC,并且,只能传送无格式的字节流无疑会给应用程序开发带来不便,另外,它的缓冲区大小也受到限制。
</P>
<P>消息队列就是一个消息的链表。可以把消息看作一个记录,具有特定的格式以及特定的优先级。对消息队列有写权限的进程可以向中按照一定的规则添加新消息;对消息队列有读权限的进程则可以从消息队列中读走消息。消息队列是随内核持续的(参见
<A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#listing1">附录
1</A>)。 </P>
<P>目前主要有两种类型的消息队列:POSIX消息队列以及系统V消息队列,系统V消息队列目前被大量使用。考虑到程序的可移植性,新开发的应用程序应尽量使用POSIX消息队列。</P>
<P>在本系列专题的序(深刻理解Linux进程间通信(IPC))中,提到对于消息队列、信号灯、以及共享内存区来说,有两个实现版本:POSIX的以及系统V的。Linux内核(内核2.4.18)支持POSIX信号灯、POSIX共享内存区以及POSIX消息队列,但对于主流Linux发行版本之一redhad8.0(内核2.4.18),还没有提供对POSIX进程间通信API的支持,不过应该只是时间上的事。</P>
<P>因此,本文将主要介绍系统V消息队列及其相应API。 <B>在没有声明的情况下,以下讨论中指的都是系统V消息队列。</B> </P>
<P><A name=N1005E><SPAN class=atitle>一、消息队列基本概念</SPAN></A></P>
<OL>
<LI>系统V消息队列是随内核持续的,只有在内核重起或者显示删除一个消息队列时,该消息队列才会真正被删除。因此系统中记录消息队列的数据结构(struct
ipc_ids msg_ids)位于内核中,系统中的所有消息队列都可以在结构msg_ids中找到访问入口。
<LI>消息队列就是一个消息的链表。每个消息队列都有一个队列头,用结构struct msg_queue来描述(参见 <A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#listing2">附录
2</A>)。队列头中包含了该消息队列的大量信息,包括消息队列键值、用户ID、组ID、消息队列中消息数目等等,甚至记录了最近对消息队列读写进程的ID。读者可以访问这些信息,也可以设置其中的某些信息。
<LI>下图说明了内核与消息队列是怎样建立起联系的: <BR>其中:struct ipc_ids
msg_ids是内核中记录消息队列的全局数据结构;struct msg_queue是每个消息队列的队列头。 <BR><IMG
height=219 alt="" src="Linux环境进程间通信(三).files/fig1.gif" width=529
border=0> <BR></LI></OL>
<P>从上图可以看出,全局数据结构 struct ipc_ids msg_ids 可以访问到每个消息队列头的第一个成员:struct
kern_ipc_perm;而每个struct
kern_ipc_perm能够与具体的消息队列对应起来是因为在该结构中,有一个key_t类型成员key,而key则唯一确定一个消息队列。kern_ipc_perm结构如下:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>struct kern_ipc_perm{ //内核中记录消息队列的全局数据结构msg_ids能够访问到该结构;
key_t key; //该键值则唯一对应一个消息队列
uid_t uid;
gid_t gid;
uid_t cuid;
gid_t cgid;
mode_t mode;
unsigned long seq;
}
</PRE></TD></TR></TBODY></TABLE><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt=""
src="Linux环境进程间通信(三).files/blue_rule.gif"
width="100%"><BR><IMG height=6 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=8
border=0></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=4 alt="" src="Linux环境进程间通信(三).files/c.gif"
width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="Linux环境进程间通信(三).files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=N1008E><SPAN class=atitle>二、操作消息队列</SPAN></A></P>
<P><A name=N10094><SPAN
class=smalltitle>对消息队列的操作无非有下面三种类型:</SPAN></A></P>
<P>1、 打开或创建消息队列
<BR>消息队列的内核持续性要求每个消息队列都在系统范围内对应唯一的键值,所以,要获得一个消息队列的描述字,只需提供该消息队列的键值即可;
</P>
<P>注:消息队列描述字是由在系统范围内唯一的键值生成的,而键值可以看作对应系统内的一条路经。</P>
<P>2、 读写操作</P>
<P>消息读写操作非常简单,对开发人员来说,每个消息都类似如下的数据结构:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>struct msgbuf{
long mtype;
char mtext[1];
};
</PRE></TD></TR></TBODY></TABLE><BR>
<P>mtype成员代表消息类型,从消息队列中读取消息的一个重要依据就是消息的类型;mtext是消息内容,当然长度不一定为1。因此,对于发送消息来说,首先预置一个msgbuf缓冲区并写入消息类型和内容,调用相应的发送函数即可;对读取消息来说,首先分配这样一个msgbuf缓冲区,然后把消息读入该缓冲区即可。</P>
<P>3、 获得或设置消息队列属性:</P>
<P>消息队列的信息基本上都保存在消息队列头中,因此,可以分配一个类似于消息队列头的结构(struct msqid_ds,见 <A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#listing2">附录
2</A>),来返回消息队列的属性;同样可以设置该数据结构。 </P><BR><BR><IMG height=219 alt=""
src="Linux环境进程间通信(三).files/fig2.gif" width=507 border=0> <BR><BR>
<P><A name=N100CE><SPAN class=smalltitle>消息队列API</SPAN></A></P>
<P><B>1、文件名到键值</B> </P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok (char*pathname, char proj);
</PRE></TD></TR></TBODY></TABLE><BR><BR>
<P>它返回与路径pathname相对应的一个键值。该函数不直接对消息队列操作,但在调用ipc(MSGGET,…)或msgget()来获得消息队列描述字前,往往要调用该函数。典型的调用代码是:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>key=ftok(path_ptr, 'a');
ipc_id=ipc(MSGGET, (int)key, flags,0,NULL,0);
…
</PRE></TD></TR></TBODY></TABLE><BR><BR>
<P><B>2、linux为操作系统V进程间通信的三种方式(消息队列、信号灯、共享内存区)提供了一个统一的用户界面:</B>
<BR><CODE><SPAN class=boldcode>int ipc</SPAN>(unsigned int <SPAN
class=boldcode>call</SPAN>, int <SPAN class=boldcode>first</SPAN>,
int <SPAN class=boldcode>second</SPAN>, int <SPAN
class=boldcode>third</SPAN>, void * <SPAN class=boldcode>ptr</SPAN>,
long <SPAN class=boldcode>fifth</SPAN>); </CODE></P>
<P>第一个参数指明对IPC对象的操作方式,对消息队列而言共有四种操作:MSGSND、MSGRCV、MSGGET以及MSGCTL,分别代表向消息队列发送消息、从消息队列读取消息、打开或创建消息队列、控制消息队列;first参数代表唯一的IPC对象;下面将介绍四种操作。</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -