📄 linux环境进程间通信(三):消息队列.htm
字号:
}
</CODE>
</PRE></TD></TR></TBODY></TABLE>程序输出结果见<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part3/index.shtml#listing3">附录
3</A>。<BR>
<P><A id=5 name=5><SPAN
class=atitle2>小结:</SPAN></A><BR>消息队列与管道以及有名管道相比,具有更大的灵活性,首先,它提供有格式字节流,有利于减少开发人员的工作量;其次,消息具有类型,在实际应用中,可作为优先级使用。这两点是管道以及有名管道所不能比的。同样,消息队列可以在几个进程间复用,而不管这几个进程是否具有亲缘关系,这一点与有名管道很相似;但消息队列是随内核持续的,与有名管道(随进程持续)相比,生命力更强,应用空间更大。</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 bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><TBODY>
<TR>
<TD><PRE><CODE>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;
};
</CODE>
</PRE></TD></TR></TBODY></TABLE><BR>
<P>结构msqid_ds用来设置或返回消息队列的信息,存在于用户空间;</P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><TBODY>
<TR>
<TD><PRE><CODE>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 */
};
</CODE>
</PRE></TD></TR></TBODY></TABLE>//可以看出上述两个结构很相似。<BR>
<P><B><A name=listing3>附录 3</A>:</B>消息队列实例输出结果:</P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><TBODY>
<TR>
<TD><PRE><CODE>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
</CODE>
</PRE></TD></TR></TBODY></TABLE><BR><!-- RESOURCES-->
<P><A id=resources name=resources><SPAN class=atitle2>参考文献:</SPAN></A></P>
<UL>
<LI>UNIX网络编程第二卷:进程间通信,作者:W.Richard
Stevens,译者:杨继张,清华大学出版社。对POSIX以及系统V消息队列都有阐述,对Linux环境下的程序开发有极大的启发意义。
<LI>linux内核源代码情景分析(上),毛德操、胡希明著,浙江大学出版社,给出了系统V消息队列相关的源代码分析。
<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对象的存取权限同样具有很好的借鉴意义。
<LI>msgget、msgsnd、msgrcv、msgctl手册 </LI></UL><BR><!-- AUTHOR BIOS--><!-- Make author heading singular or plural as needed-->
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD><A id=author1 name=author1><SPAN class=atitle2>关于作者:</SPAN></A>
<BR>郑彦兴,国防科大攻读博士学位。联系方式: <A
href="mailto:mlinux@163.com">mlinux@163.com</A></TD></TR></TBODY></TABLE><!-- END PAPER BODY--></TD>
<TD width=10><IMG alt="" border=0 height=1
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=10></TD></TR></TBODY></TABLE><BR
clear=all><IMG alt="" border=0 height=10 src="Linux环境进程间通信(三):消息队列.files/c.gif"
width=100><BR>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD align=right width="100%"><A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part3/index.shtml#top">到页首</A></TD>
<TD width=5><IMG alt="" border=0 height=1
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=5></TD></TR>
<TR vAlign=top>
<TD bgColor=#000000 colSpan=2><IMG alt="" border=0 height=1
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=100></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff colSpan=2><IMG alt="" border=0 height=8
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=10 cellSpacing=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD>
<FORM action=/developerWorks/cn/cnratings.nsf/RateArticle?CreateDocument
method=post name=getURL><INPUT name=ArticleTitle type=hidden
value=Linux环境进程间通信(三):消息队列> <INPUT name=url type=hidden>
<SCRIPT language=javascript type=text/javascript>getURL();</SCRIPT>
<INPUT name=Zone type=hidden value=linux> <INPUT name=RedirectURL
type=hidden value=/developerWorks/cn/thankyou/feedback-linux.html> <A
id=rating name=rating><B>您对这篇文章的看法如何?</B></A>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR>
<TD colSpan=5><IMG alt="" border=0 height=8
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=100></TD></TR>
<TR vAlign=top>
<TD width="16%"><INPUT name=Rating type=radio value=5>真棒!(5)</TD>
<TD width="20%"><INPUT name=Rating type=radio value=4>好材料 (4)</TD>
<TD width="24%"><INPUT name=Rating type=radio value=3>一般;尚可 (3)</TD>
<TD width="22%"><INPUT name=Rating type=radio value=2>需提高 (2)</TD>
<TD width="18%"><INPUT name=Rating type=radio value=1>太差!
(1)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA cols=60 name=Comments rows=5 wrap=virtual></TEXTAREA><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG alt="" border=0 height=8
src="Linux环境进程间通信(三):消息队列.files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD align=right>(c) Copyright IBM Corp. 2001, (c) Copyright IBM China
2001, All Right Reserved</TD></TR>
<TR vAlign=top>
<TD class=bbg height=21> <A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&origin=dwhead">关于
IBM</A><SPAN class=divider> | </SPAN><A
class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/privacy/index.shtml&origin=dwhead">隐私条约</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/legal/index.shtml&origin=dwhead">使用条款</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/contact/index.shtml&origin=dwhead">联系
IBM</A></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2 src="Linux环境进程间通信(三):消息队列.files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG alt="" border=0 height=1
src="F:\项目文档\进程间通信\Linux环境进程间通信(三):消息队列.files\c(1).gif" width=1></NOSCRIPT>
</A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -