📄 linux环境进程间通信(三):消息队列.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0063)http://study.feloo.com/computer/pro/vc/system/200506/39162.html -->
<HTML><HEAD><TITLE>Linux环境进程间通信(三):消息队列</TITLE>
<META
content=Linux环境进程间通信(三):消息队列,Linux环境进程间通信(三):消息队列教程,Linux环境进程间通信(三):消息队列学习文章,Linux环境进程间通信(三):消息队列电脑教程,Linux环境进程间通信(三):消息队列电脑文章,Linux环境进程间通信(三):消息队列学习教程,Linux环境进程间通信(三):消息队列学习资料,Linux环境进程间通信(三):消息队列资料下载,Linux环境进程间通信(三):消息队列实例,Linux环境进程间通信(三):消息队列实例教学
name=Description>
<META
content='="Linux环境进程间通信(三):消息队列,Linux环境进程间通信(三):消息队列教程,Linux环境进程间通信(三):消息队列学习文章,Linux环境进程间通信(三):消息队列电脑教程,Linux环境进程间通信(三):消息队列电脑文章,Linux环境进程间通信(三):消息队列学习教程,Linux环境进程间通信(三):消息队列学习资料,Linux环境进程间通信(三):消息队列资料下载,Linux环境进程间通信(三):消息队列实例,Linux环境进程间通信(三):消息队列实例教学"'
name=keywords>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<STYLE type=text/css>.style1 {
COLOR: #0099cc
}
</STYLE>
<LINK href="Linux环境进程间通信(三):消息队列.files/Skin3.css" type=text/css rel=stylesheet>
<SCRIPT language=JavaScript src="Linux环境进程间通信(三):消息队列.files/menu.js"
type=text/JavaScript></SCRIPT>
<SCRIPT language=JavaScript1.2 src="Linux环境进程间通信(三):消息队列.files/stm31.js"
type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript type=text/JavaScript>
//改变图片大小
function resizepic(thispic)
{
if(thispic.width>700) thispic.width=700;
}
//无级缩放图片笮?
function bbimg(o)
{
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if (zoom>0) o.style.zoom=zoom+'%';
return false;
}
//双击鼠标滚动屏幕的代码
var currentpos,timer;
function initialize()
{
timer=setInterval ("scrollwindow ()",30);
}
function sc()
{
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scroll(0,++currentpos);
if (currentpos !=document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
//更改字体大小
var status0='';
var curfontsize=10;
var curlineheight=18;
function fontZoomA(){
if(curfontsize>8){
document.getElementById('fontzoom').style.fontSize=(--curfontsize)+'pt';
document.getElementById('fontzoom').style.lineHeight=(--curlineheight)+'pt';
}
}
function fontZoomB(){
if(curfontsize<64){
document.getElementById('fontzoom').style.fontSize=(++curfontsize)+'pt';
document.getElementById('fontzoom').style.lineHeight=(++curlineheight)+'pt';
}
}
</SCRIPT>
<META content="MSHTML 6.00.2900.2180" name=GENERATOR></HEAD>
<BODY leftMargin=0 topMargin=0>
<DIV id=menuDiv
style="Z-INDEX: 1000; VISIBILITY: hidden; WIDTH: 1px; POSITION: absolute; HEIGHT: 1px; BACKGROUND-COLOR: #9cc5f8"></DIV><!-- ********网页顶部代码开始******** -->
<TABLE class=top_tdbgall style="WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width=760 align=center border=0>
<TBODY>
<TR>
<TD bgColor=#4f4f4f height=3></TD></TR>
<TR>
<TD class=top_Channel align=right width="100%"><FONT
color=red> | <A class=Channel
href="http://study.feloo.com/Index.html">网站首页</A> | <A
class=Channel2 href="http://study.feloo.com/computer/Index.html"
target=_self>电脑学习</A> | <A class=Channel
href="http://study.feloo.com/Article/Index.html"
target=_self>英语学堂</A> | <A class=Channel
href="http://study.feloo.com/lunwen/Index.html"
target=_self>论文中心</A> | <A class=Channel
href="http://study.feloo.com/exam/Index.html"
target=_self>试题资源</A> | <A class=Channel
href="http://study.feloo.com/jiaoan/Index.html"
target=_self>教案集锦</A> | <A class=Channel
href="http://study.feloo.com/kejian/Index.html"
target=_self>课件下载</A> | <A class=Channel
href="http://study.feloo.com/school/Index.html"
target=_self>高校联盟</A> | <A class=Channel
href="http://study.feloo.com/job/Index.html"
target=_self>求职创业</A> | <A class=Channel
href="http://study.feloo.com/fanben/Index.html"
target=_self>实用范本</A> | <A class=Channel
href="http://study.feloo.com/down"
target=_blank>软件下载</A> | </FONT></TD></TR>
<TR>
<TD bgColor=#4f4f4f height=1></TD></TR></TBODY></TABLE>
<TABLE class=top_tdbgall style="WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width=760 align=center border=0>
<TBODY>
<TR>
<TD align=middle height=72>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=middle width=180 height=72><A title=中国学习联盟
href="http://study.feloo.com/" target=_blank><IMG height=60
src="Linux环境进程间通信(三):消息队列.files/logo.gif" width=180 border=0></A></TD>
<TD align=middle width=*>
<SCRIPT src="Linux环境进程间通信(三):消息队列.files/banner.js"></SCRIPT>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE class=top_tdbgall style="WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width=760 align=center border=0>
<TBODY>
<TR>
<TD bgColor=#4f4f4f height=1></TD></TR>
<TR><!--banner广告显示代码-->
<TD align=middle>
<SCRIPT src="Linux环境进程间通信(三):消息队列.files/ban-wen.js"></SCRIPT>
</TD></TR>
<TR>
<TD bgColor=#4f4f4f height=1></TD></TR></TBODY></TABLE><!--导航代码开始-->
<TABLE class=top_Path style="WORD-BREAK: break-all" cellSpacing=0 cellPadding=0
width=760 align=center border=0>
<TBODY>
<TR>
<TD align=middle width="5%"><IMG height=25
src="Linux环境进程间通信(三):消息队列.files/ad_mqdhl.gif" width=40></TD>
<TD width="73%">您现在的位置: <A
href="http://study.feloo.com/">中国学习联盟</A> >> <A
href="http://study.feloo.com/computer/Index.html">电脑学习</A> >> <A
href="http://study.feloo.com/computer/pro/Index.html">程序设计</A> >> <A
href="http://study.feloo.com/computer/pro/vc/Index.html">VC语言</A> >> <A
href="http://study.feloo.com/computer/pro/vc/system/Index.html">系统类编程</A> >> 电脑文章正文</TD>
<TD align=middle width="22%"><IMG height=13
src="Linux环境进程间通信(三):消息队列.files/User_Login.gif" width=13 align=absMiddle>
<A class=top_UserLogin href="http://study.feloo.com/User/User_Login.asp"
target=_blank>用户登录</A> <IMG height=13
src="Linux环境进程间通信(三):消息队列.files/User_Reg.gif" width=13 align=absMiddle> <A
class=top_UserLogin href="http://study.feloo.com/Reg/User_Reg.asp"
target=_blank>注册</A> </TD></TR></TBODY></TABLE><!--导航代码结束--><!-- ********网页顶部代码结束******** --><!-- ********网页中部代码开始******** -->
<TABLE class=center_tdbgall style="WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width=760 align=center border=0>
<TBODY>
<TR>
<TD vAlign=top width=*><!--网页中部左栏文章内容代码开始-->
<TABLE style="WORD-BREAK: break-all" cellSpacing=0 cellPadding=0
width="100%" align=center border=0>
<TBODY>
<TR>
<TD class=main_title_282 vAlign=center width="3%"><IMG height=28
src="Linux环境进程间通信(三):消息队列.files/ad_dh.gif" width=29
align=absMiddle></TD>
<TD class=main_title_282 width="66%">Linux环境进程间通信(三):消息队列</TD>
<TD class=main_title_282
align=middle> <FONT
color=#009999>★★★★★</FONT></TD></TR></TBODY></TABLE>
<TABLE class=main_tdbg_575 style="WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width="100%" align=center border=0>
<TBODY>
<TR vAlign=center align=middle>
<TD class=main_ArticleTitle style="WORD-BREAK: break-all"
background=Linux环境进程间通信(三):消息队列.files/ad_dybg2.gif colSpan=2
height=48>Linux环境进程间通信(三):消息队列</TD></TR>
<TR align=middle bgColor=#eeeeee>
<TD colSpan=2 height=24>作者:未知 文章来源:中国学习联盟 点击数:
<SCRIPT language=javascript
src="Linux环境进程间通信(三):消息队列.files/GetHits.htm"></SCRIPT>
更新时间:2005-6-9</TD></TR>
<TR>
<TD colSpan=2></TD></TR>
<TR align=right>
<TD colSpan=2 height=24>【字体:<A class=top_UserLogin
href="javascript:fontZoomA();">小</A> <A class=top_UserLogin
href="javascript:fontZoomB();">大</A>】【<A
href="http://study.feloo.com/computer/Comment.asp?ArticleID=39162"
target=_blank>发表评论</A>】【<A
href="http://study.feloo.com/User/User_Favorite.asp?Action=Add&ChannelID=25&InfoID=39162"
target=_blank>加入收藏</A>】【<A
href="http://study.feloo.com/computer/SendMail.asp?ArticleID=39162"
target=_blank>告诉好友</A>】【<A
href="http://study.feloo.com/computer/Print.asp?ArticleID=39162"
target=_blank>打印此文</A>】【<A
href="javascript:window.close();">关闭窗口</A>】 </TD></TR><!--文章标题下部广告代码开始-->
<TR>
<TD align=middle colSpan=2>
<SCRIPT src="Linux环境进程间通信(三):消息队列.files/wen-top.js"></SCRIPT>
</TD></TR><!--文章标题下部广告代码结束-->
<TR>
<TD background=Linux环境进程间通信(三):消息队列.files/ad_bx1.gif colSpan=2
height=6></TD></TR>
<TR>
<TD colSpan=6></TD></TR>
<TR>
<TD id=fontzoom style="WORD-BREAK: break-all" vAlign=top colSpan=2
height=600>
<BLOCKQUOTE>本系列文章中的前两部分,我们探讨<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part1/index.shtml">管道</A>及<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part2/index1.shtml">信号</A>两种通信机制,本文将深入第三部分,介绍系统
V 消息队列及其相应 API。</BLOCKQUOTE>
<P>消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点。作为早期unix通信机制之一的信号能够传送的信息量有限,后来虽然POSIX
1003.1b在信号的实时性方面作了拓广,使得信号在传递信息量方面有了相当程度的改进,但是信号这种通信方式更像"即时"的通信方式,它要求接受信号的进程在某个时间范围内对信号做出反应,因此该信号最多在接受信号进程的生命周期内才有意义,信号所传递的信息是接近于随进程持续的概念(process-persistent),见<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part3/index.shtml#listing1">附录
1</A>;管道及有名管道及有名管道则是典型的随进程持续IPC,并且,只能传送无格式的字节流无疑会给应用程序开发带来不便,另外,它的缓冲区大小也受到限制。</P>
<P>消息队列就是一个消息的链表。可以把消息看作一个记录,具有特定的格式以及特定的优先级。对消息队列有写权限的进程可以向中按照一定的规则添加新消息;对消息队列有读权限的进程则可以从消息队列中读走消息。消息队列是随内核持续的(参见<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part3/index.shtml#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 id=1 name=1><SPAN class=atitle2>一、消息队列基本概念</SPAN></A></P>
<OL class=n01>
<LI>系统V消息队列是随内核持续的,只有在内核重起或者显示删除一个消息队列时,该消息队列才会真正被删除。因此系统中记录消息队列的数据结构(struct
ipc_ids msg_ids)位于内核中,系统中的所有消息队列都可以在结构msg_ids中找到访问入口。
<LI>消息队列就是一个消息的链表。每个消息队列都有一个队列头,用结构struct msg_queue来描述(参见<A
href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part3/index.shtml#listing2">附录
2</A>)。队列头中包含了该消息队列的大量信息,包括消息队列键值、用户ID、组ID、消息队列中消息数目等等,甚至记录了最近对消息队列读写进程的ID。读者可以访问这些信息,也可以设置其中的某些信息。
<LI>下图说明了内核与消息队列是怎样建立起联系的:<BR>其中:struct ipc_ids
msg_ids是内核中记录消息队列的全局数据结构;struct msg_queue是每个消息队列的队列头。
<CENTER><IMG height=219 alt="" src="" width=529
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -