📄 linux环境进程间通信(三).htm
字号:
}
</PRE></TD></TR></TBODY></TABLE><BR>程序输出结果见 <A
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html#listing3">附录
3</A>。 <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=N10252><SPAN class=atitle>小结:</SPAN></A></P>
<P>消息队列与管道以及有名管道相比,具有更大的灵活性,首先,它提供有格式字节流,有利于减少开发人员的工作量;其次,消息具有类型,在实际应用中,可作为优先级使用。这两点是管道以及有名管道所不能比的。同样,消息队列可以在几个进程间复用,而不管这几个进程是否具有亲缘关系,这一点与有名管道很相似;但消息队列是随内核持续的,与有名管道(随进程持续)相比,生命力更强,应用空间更大。</P>
<P><B><A name=listing1>附录 1</A>:
</B>在参考文献[1]中,给出了IPC随进程持续、随内核持续以及随文件系统持续的定义: </P>
<OL>
<LI>随进程持续:IPC一直存在到打开IPC对象的最后一个进程关闭该对象为止。如管道和有名管道;
<LI>随内核持续:IPC一直持续到内核重新自举或者显示删除该对象为止。如消息队列、信号灯以及共享内存等;
<LI>随文件系统持续:IPC一直持续到显示删除该对象为止。 </LI></OL><BR>
<P><B><A name=listing2>附录 2</A>:
</B><BR>结构msg_queue用来描述消息队列头,存在于系统空间: </P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>struct msg_queue {
struct kern_ipc_perm q_perm;
time_t q_stime; /* last msgsnd time */
time_t q_rtime; /* last msgrcv time */
time_t q_ctime; /* last change time */
unsigned long q_cbytes; /* current number of bytes on queue */
unsigned long q_qnum; /* number of messages in queue */
unsigned long q_qbytes; /* max number of bytes on queue */
pid_t q_lspid; /* pid of last msgsnd */
pid_t q_lrpid; /* last receive pid */
struct list_head q_messages;
struct list_head q_receivers;
struct list_head q_senders;
};
</PRE></TD></TR></TBODY></TABLE><BR><BR>
<P>结构msqid_ds用来设置或返回消息队列的信息,存在于用户空间;</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>struct msqid_ds {
struct ipc_perm msg_perm;
struct msg *msg_first; /* first message on queue,unused */
struct msg *msg_last; /* last message in queue,unused */
__kernel_time_t msg_stime; /* last msgsnd time */
__kernel_time_t msg_rtime; /* last msgrcv time */
__kernel_time_t msg_ctime; /* last change time */
unsigned long msg_lcbytes; /* Reuse junk fields for 32 bit */
unsigned long msg_lqbytes; /* ditto */
unsigned short msg_cbytes; /* current number of bytes on queue */
unsigned short msg_qnum; /* number of messages in queue */
unsigned short msg_qbytes; /* max number of bytes on queue */
__kernel_ipc_pid_t msg_lspid; /* pid of last msgsnd */
__kernel_ipc_pid_t msg_lrpid; /* last receive pid */
};
</PRE></TD></TR></TBODY></TABLE><BR>//可以看出上述两个结构很相似。 <BR>
<P><B><A name=listing3>附录 3</A>: </B>消息队列实例输出结果: </P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>current number of bytes on queue is 0
number of messages in queue is 0
max number of bytes on queue is 16384
pid of last msgsnd is 0
pid of last msgrcv is 0
last msgsnd time is Thu Jan 1 08:00:00 1970
last msgrcv time is Thu Jan 1 08:00:00 1970
last change time is Sun Dec 29 18:28:20 2002
msg uid is 0
msg gid is 0
//上面刚刚创建一个新消息队列时的输出
current number of bytes on queue is 1
number of messages in queue is 1
max number of bytes on queue is 16384
pid of last msgsnd is 2510
pid of last msgrcv is 0
last msgsnd time is Sun Dec 29 18:28:21 2002
last msgrcv time is Thu Jan 1 08:00:00 1970
last change time is Sun Dec 29 18:28:20 2002
msg uid is 0
msg gid is 0
read from msg queue 1 bytes
//实际读出的字节数
current number of bytes on queue is 0
number of messages in queue is 0
max number of bytes on queue is 16384 //每个消息队列最大容量(字节数)
pid of last msgsnd is 2510
pid of last msgrcv is 2510
last msgsnd time is Sun Dec 29 18:28:21 2002
last msgrcv time is Sun Dec 29 18:28:22 2002
last change time is Sun Dec 29 18:28:20 2002
msg uid is 0
msg gid is 0
current number of bytes on queue is 0
number of messages in queue is 0
max number of bytes on queue is 16388 //可看出超级用户可修改消息队列最大容量
pid of last msgsnd is 2510
pid of last msgrcv is 2510 //对操作消息队列进程的跟踪
last msgsnd time is Sun Dec 29 18:28:21 2002
last msgrcv time is Sun Dec 29 18:28:22 2002
last change time is Sun Dec 29 18:28:23 2002 //msgctl()调用对msg_ctime有影响
msg uid is 8
msg gid is 8
</PRE></TD></TR></TBODY></TABLE><BR><BR><BR>
<P><A name=resources><SPAN class=atitle>参考资料 </SPAN></A></P>
<UL>
<LI>UNIX网络编程第二卷:进程间通信,作者:W.Richard
Stevens,译者:杨继张,清华大学出版社。对POSIX以及系统V消息队列都有阐述,对Linux环境下的程序开发有极大的启发意义。<BR><BR>
<LI>linux内核源代码情景分析(上),毛德操、胡希明著,浙江大学出版社,给出了系统V消息队列相关的源代码分析。<BR><BR>
<LI><A href="http://www.fanqiang.com/a4/b2/20010508/113315.html"
target=_blank>http://www.fanqiang.com/a4/b2/20010508/113315.html</A>,主要阐述linux下对文件的操作,详细介绍了对文件的存取权限位,对IPC对象的存取权限同样具有很好的借鉴意义。
<BR><BR>
<LI>msgget、msgsnd、msgrcv、msgctl手册<BR></LI></UL><BR><BR>
<P><A name=author><SPAN class=atitle>关于作者</SPAN></A></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=3><IMG height=5 alt=""
src="Linux环境进程间通信(三).files/c.gif" width="100%"></TD></TR>
<TR vAlign=top align=left>
<TD>
<P></P></TD>
<TD><IMG height=5 alt="" src="Linux环境进程间通信(三).files/c.gif"
width=4></TD>
<TD width="100%">
<P>郑彦兴,男,现攻读国防科大计算机学院网络方向博士学位。您可以通过电子邮件 <A
href="mailto:mlinux@163.com?cc=">mailto:mlinux@163.com?cc=</A>和他联系。
</P></TD></TR></TBODY></TABLE><BR><BR><BR>
<P class=no-print><SPAN class=atitle><A
name=rate>对本文的评价</A></SPAN></P><SPAN class=no-print>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD>
<FORM
action=https://www.ibm.com/developerworks/secure/cnratings.jsp
method=get><INPUT type=hidden value=Linux环境进程间通信(三)
name=ArticleTitle><INPUT type=hidden value=Linux
name=Zone><INPUT type=hidden
value=http://www.ibm.com/developerworks/cn/thankyou/
name=RedirectURL><INPUT type=hidden value=china
name=localsite>
<SCRIPT language=javascript>document.write('<input type="hidden" name="url" value="'+location.href+'" />');</SCRIPT>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><IMG height=8 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=100
border=0></TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=1 name=Rating>太差! (1)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=2 name=Rating>需提高 (2)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=3 name=Rating>一般;尚可
(3)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=4 name=Rating>好文章 (4)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=5
name=Rating>真棒!(5)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA id=Comments name=Comments rows=5 wrap=virtual cols=60> </TEXTAREA><BR><BR><INPUT type=submit value=反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG height=8 alt=""
src="Linux环境进程间通信(三).files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE></SPAN><SPAN class=no-print>
<TABLE cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=8 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></SPAN></TD>
<TD width=10><IMG height=1 alt="" src="Linux环境进程间通信(三).files/c.gif"
width=10></TD></TR></TBODY></TABLE><SPAN class=small>IBM 公司保留在
developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 <A
href="https://www.ibm.com/developerworks/secure/reprintreq.jsp?domain=dwchina">提交转载请求表单</A>
联系我们的编辑团队。</SPAN></TD></TR></TBODY></TABLE><!--FOOTER_BEGIN--><!-- IBM FOOTER -->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=bbg height=19>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><SPAN class=spacer> </SPAN><A
class=mainlink href="http://www.ibm.com/cn/ibm/index.shtml">关于
IBM</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink
href="http://www.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink href="http://www.ibm.com/contact/cn/">联系
IBM</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- end footer -->
<SCRIPT language=JavaScript1.2 src="Linux环境进程间通信(三).files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt=""
src="E:\sun_job\notebook\linux_进程间通信\Linux环境进程间通信(三).files\c(1).gif" width=1
border=0></NOSCRIPT><!--FOOTER_END--><!--XSLT stylesheet used to transform this file: dw-document-html-5.8.xsl--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -