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

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

📁 关于Linux内核进程间通信的几篇文章
💻 HTM
📖 第 1 页 / 共 4 页
字号:
              <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="在本系列序中作者概述了 linux 进程间通信的几种主要手段。其中管道和有名管道是最早的进程间通信机制之一,管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。 认清管道和有名管道的读写规则是在程序中应用它们的关键,本文在详细讨论了管道和有名管道的通信机制的基础上,用实例对其读写规则进行了程序验证,这样做有利于增强读者对读写规则的感性认识,同时也提供了应用范例。" 
                          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/part1/index.html#author">郑彦兴</A> 
            (<A 
            href="mailto:mlinux@163.com?subject=Linux环境进程间通信(一)">mailto:mlinux@163.com?subject=Linux环境进程间通信(一)</A>)国防科大计算机学院<BR></P>
            <P>2002 年 12 月 11 日</P>
            <BLOCKQUOTE>在本系列序中作者概述了 linux 
              进程间通信的几种主要手段。其中管道和有名管道是最早的进程间通信机制之一,管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。 
              认清管道和有名管道的读写规则是在程序中应用它们的关键,本文在详细讨论了管道和有名管道的通信机制的基础上,用实例对其读写规则进行了程序验证,这样做有利于增强读者对读写规则的感性认识,同时也提供了应用范例。</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>1、 管道概述及相关API应用</SPAN></A></P>
            <P><A name=N1004A><SPAN class=smalltitle>1.1 
管道相关的关键概念</SPAN></A></P>
            <P>管道是Linux支持的最初Unix IPC形式之一,具有以下特点:</P>
            <UL>
              <LI>管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道; 
              <LI>只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程); 
              <LI>单独构成一种独立的文件系统:管道对于管道两端的进程而言,就是一个文件,但它不是普通的文件,它不属于某种文件系统,而是自立门户,单独构成一种文件系统,并且只存在与内存中。 

              <LI>数据的读出和写入:一个进程向管道中写的内容被管道另一端的进程读出。写入的内容每次都添加在管道缓冲区的末尾,并且每次都是从缓冲区的头部读出数据。 
              </LI></UL>
            <P><A name=N10062><SPAN class=smalltitle>1.2管道的创建:</SPAN></A></P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=code-outline><PRE class=displaycode>#include &lt;unistd.h&gt;
int pipe(int fd[2])
</PRE></TD></TR></TBODY></TABLE><BR>
            <P>该函数创建的管道的两端处于一个进程中间,在实际应用中没有太大意义,因此,一个进程在由pipe()创建管道后,一般再fork一个子进程,然后通过管道实现父子进程间的通信(因此也不难推出,只要两个进程中存在亲缘关系,这里的亲缘关系指的是具有共同的祖先,都可以采用管道方式来进行通信)。</P>
            <P><A name=N1006F><SPAN class=smalltitle>1.3管道的读写规则:</SPAN></A></P>
            <P>管道两端可分别用描述字fd[0]以及fd[1]来描述,需要注意的是,管道的两端是固定了任务的。即一端只能用于读,由描述字fd[0]表示,称其为管道读端;另一端则只能用于写,由描述字fd[1]来表示,称其为管道写端。如果试图从管道写端读取数据,或者向管道读端写入数据都将导致错误发生。一般文件的I/O函数都可以用于管道,如close、read、write等等。</P>
            <P>从管道中读取数据:</P>
            <UL>
              <LI>如果管道的写端不存在,则认为已经读到了数据的末尾,读函数返回的读出字节数为0; 
              <LI>当管道的写端存在时,如果请求的字节数目大于PIPE_BUF,则返回管道中现有的数据字节数,如果请求的字节数目不大于PIPE_BUF,则返回管道中现有数据字节数(此时,管道中数据量小于请求的数据量);或者返回请求的字节数(此时,管道中数据量不小于请求的数据量)。注:(PIPE_BUF在include/linux/limits.h中定义,不同的内核版本可能会有所不同。Posix.1要求PIPE_BUF至少为512字节,red 
              hat 7.2中为4096)。 </LI></UL>
            <P>关于管道的读规则验证:</P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=code-outline><PRE class=displaycode> /**************
 * readtest.c *
 **************/
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;errno.h&gt;
main()
{
	int pipe_fd[2];
	pid_t pid;
	char r_buf[100];
	char w_buf[4];
	char* p_wbuf;
	int r_num;
	int cmd;
	
	memset(r_buf,0,sizeof(r_buf));
	memset(w_buf,0,sizeof(r_buf));
	p_wbuf=w_buf;
	if(pipe(pipe_fd)&lt;0)
	{
		printf("pipe create error\n");
		return -1;
	}
	
	if((pid=fork())==0)
	{
		printf("\n");
		close(pipe_fd[1]);
		sleep(3);//确保父进程关闭写端
	    r_num=read(pipe_fd[0],r_buf,100);
printf(	"read num is %d   the data read from the pipe is %d\n",r_num,atoi(r_buf));
		
		close(pipe_fd[0]);
		exit();
	}
	else if(pid&gt;0)
	{
	close(pipe_fd[0]);//read
	strcpy(w_buf,"111");
	if(write(pipe_fd[1],w_buf,4)!=-1)
		printf("parent write over\n");
	close(pipe_fd[1]);//write
		printf("parent close fd[1] over\n");
	sleep(10);
	}	
}
 /**************************************************
 * 程序输出结果:
 * parent write over
 * parent close fd[1] over
 * read num is 4   the data read from the pipe is 111
 * 附加结论:
 * 管道写端关闭后,写入的数据将一直存在,直到读出为止.
 ****************************************************/ 
 </PRE></TD></TR></TBODY></TABLE><BR>
            <P>向管道中写入数据:</P>
            <UL>
              <LI>向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞。 
              <BR>注:只有在管道的读端存在时,向管道中写入数据才有意义。否则,向管道中写入数据的进程将收到内核传来的SIFPIPE信号,应用程序可以处理该信号,也可以忽略(默认动作则是应用程序终止)。 
              </LI></UL>
            <P>对管道的写规则的验证1:写端对读端存在的依赖性</P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=code-outline><PRE class=displaycode>#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
main()
{
	int pipe_fd[2];
	pid_t pid;
	char r_buf[4];
	char* w_buf;
	int writenum;
	int cmd;
	
	memset(r_buf,0,sizeof(r_buf));
	if(pipe(pipe_fd)&lt;0)
	{
		printf("pipe create error\n");
		return -1;
	}
	
	if((pid=fork())==0)
	{
		close(pipe_fd[0]);
		close(pipe_fd[1]);
		sleep(10);	
		exit();
	}
	else if(pid&gt;0)
	{
	sleep(1);  //等待子进程完成关闭读端的操作
	close(pipe_fd[0]);//write
	w_buf="111";
	if((writenum=write(pipe_fd[1],w_buf,4))==-1)
		printf("write to pipe error\n");
	else	
		printf("the bytes write to pipe is %d \n", writenum);
	
	close(pipe_fd[1]);
	}	
}
</PRE></TD></TR></TBODY></TABLE><BR>
            <P>则输出结果为: Broken 
            pipe,原因就是该管道以及它的所有fork()产物的读端都已经被关闭。如果在父进程中保留读端,即在写完pipe后,再关闭父进程的读端,也会正常写入pipe,读者可自己验证一下该结论。因此,在向管道写入数据时,至少应该存在某一个进程,其中管道读端没有被关闭,否则就会出现上述错误(管道断裂,进程收到了SIGPIPE信号,默认动作是进程终止)</P>
            <P>对管道的写规则的验证2:linux不保证写管道的原子性验证</P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=code-outline><PRE class=displaycode>#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;errno.h&gt;
main(int argc,char**argv)
{
	int pipe_fd[2];
	pid_t pid;
	char r_buf[4096];
	char w_buf[4096*2];
	int writenum;
	int rnum;
	memset(r_buf,0,sizeof(r_buf));	
	if(pipe(pipe_fd)&lt;0)
	{
		printf("pipe create error\n");
		return -1;
	}
	
	if((pid=fork())==0)
	{
		close(pipe_fd[1]);
		while(1)
		{
		sleep(1);	
		rnum=read(pipe_fd[0],r_buf,1000);
		printf("child: readnum is %d\n",rnum);
		}
		close(pipe_fd[0]);
		
		exit();
	}
	else if(pid&gt;0)
	{
	close(pipe_fd[0]);//write
	memset(r_buf,0,sizeof(r_buf));	
	if((writenum=write(pipe_fd[1],w_buf,1024))==-1)
		printf("write to pipe error\n");
	else	
		printf("the bytes write to pipe is %d \n", writenum);
	writenum=write(pipe_fd[1],w_buf,4096);
	close(pipe_fd[1]);
	}	
}
输出结果:
the bytes write to pipe 1000
the bytes write to pipe 1000  //注意,此行输出说明了写入的非原子性
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 120  //注意,此行输出说明了写入的非原子性
the bytes write to pipe 0

⌨️ 快捷键说明

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