⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux环境进程间通信(四).htm

📁 关于Linux内核进程间通信的几篇文章
💻 HTM
📖 第 1 页 / 共 4 页
字号:
        <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/part4/index.html#resources">参考资料 
            </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/part4/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/part4/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>&nbsp;&nbsp;&gt;&nbsp;&nbsp;</SPAN><A 
                  class=bctl 
                  href="http://www.ibm.com/developerworks/cn/linux/">Linux</A><SPAN 
                  class=bct>&nbsp;&nbsp;&gt;</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=信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。 
                          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/part4/index.html#author">郑彦兴</A> 
            (<A 
            href="mailto:mlinux@163.com?subject=Linux环境进程间通信(四)">mailto:mlinux@163.com?subject=Linux环境进程间通信(四)</A>)国防科大计算机学院<BR></P>
            <P>2003 年 4 月 07 日</P>
            <BLOCKQUOTE>信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。</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><A name=N10044><SPAN class=atitle>一、信号灯概述</SPAN></A></P>
            <P>信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。信号灯有以下两种类型:</P>
            <UL>
              <LI>二值信号灯:最简单的信号灯形式,信号灯的值只能取0或1,类似于互斥锁。 
              <BR>注:二值信号灯能够实现互斥锁的功能,但两者的关注内容不同。信号灯强调共享资源,只要共享资源可用,其他进程同样可以修改信号灯的值;互斥锁更强调进程,占用资源的进程使用完资源后,必须由进程本身来解锁。 

              <LI>计算信号灯:信号灯的值可以取任意非负值(当然受内核本身的约束)。 </LI></UL><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/part4/index.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=N10058><SPAN class=atitle>二、Linux信号灯</SPAN></A></P>
            <P>linux对信号灯的支持状况与消息队列一样,在red had 
            8.0发行版本中支持的是系统V的信号灯。因此,本文将主要介绍系统V信号灯及其相应API。在没有声明的情况下,以下讨论中指的都是系统V信号灯。</P>
            <P>注意,通常所说的系统V信号灯指的是计数信号灯集。</P><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/part4/index.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=N10064><SPAN class=atitle>三、信号灯与内核</SPAN></A></P>
            <P>1、系统V信号灯是随内核持续的,只有在内核重起或者显示删除一个信号灯集时,该信号灯集才会真正被删除。因此系统中记录信号灯的数据结构(struct 
            ipc_ids sem_ids)位于内核中,系统中的所有信号灯都可以在结构sem_ids中找到访问入口。</P>
            <P>2、下图说明了内核与信号灯是怎样建立起联系的:</P>
            <P>其中:struct ipc_ids 
            sem_ids是内核中记录信号灯的全局数据结构;描述一个具体的信号灯及其相关信息。</P><BR><IMG height=248 
            alt="" src="Linux环境进程间通信(四).files/1.gif" width=628 border=0> <BR>
            <P>其中,struct sem结构如下:</P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=code-outline><PRE class=displaycode>struct sem{
int semval;		// current value
int sempid		// pid of last operation
}
</PRE></TD></TR></TBODY></TABLE><BR>
            <P>从上图可以看出,全局数据结构struct ipc_ids sem_ids可以访问到struct 
            kern_ipc_perm的第一个成员:struct kern_ipc_perm;而每个struct 
            kern_ipc_perm能够与具体的信号灯对应起来是因为在该结构中,有一个key_t类型成员key,而key则唯一确定一个信号灯集;同时,结构struct 
            kern_ipc_perm的最后一个成员sem_nsems确定了该信号灯在信号灯集中的顺序,这样内核就能够记录每个信号灯的信息了。kern_ipc_perm结构参见《Linux环境进程间通信(三):消息队列》。struct 
            sem_array见附录1。</P><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/part4/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>2、 信号灯值操作 <BR>linux可以增加或减小信号灯的值,相应于对共享资源的释放和占有。具体参见后面的semop系统调用。 
            </P>
            <P>3、 获得或设置信号灯属性: <BR>系统中的每一个信号灯集都对应一个struct 
            sem_array结构,该结构记录了信号灯集的各种信息,存在于系统空间。为了设置、获得该信号灯集的各种信息及属性,在用户空间有一个重要的联合结构与之对应,即union 
            semun。 </P><BR><IMG height=226 alt="" 
            src="Linux环境进程间通信(四).files/2.gif" width=529 border=0> <BR>
            <P>联合semun数据结构各成员意义参见附录2</P>
            <P><A name=N100BD><SPAN class=smalltitle>信号灯API</SPAN></A></P>
            <P>1、文件名到键值</P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -