離弦之Ray

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            55 Posts :: 0 Stories :: 33 Comments :: 0 Trackbacks

          父進程和子進程創建雙向管道的實際步驟

          創建管道 1(fd1[0] fd1[1]) 和管道 2(fd2[0] fd2[1])

          Fork

          父進程關閉管道 1 的讀出端( fd1[0]) [MS1] ?

          父進程關閉管道 2 的寫入端( fd2[1]

          子進程關閉管道 1 的寫入端( fd1[1]

          子進程關閉管道 2 的讀出端( fd2[0]


          下面是示例程序:

          #include “unpipe.h”

          void client(int, int) ,server(int, int);

          int main(int argc, char* argv[])

          {

          ?????? int pipe1[2],pipe[2];

          pid_t childpid;

          Pipe(pipe1);

          Pipe(pipe2);

          ?

          if((childpid=fork())==0)

          {

          ?????? Close(pipe1[1])?;

          ?????? Close(pipe2[0])?;

          ?

          ?????? server(pipe1[0],pipe2[1])?;

          ?????? exit(0)?;

          }

          close(pipe1[0])?;

          close(pipe2[1])?;

          client(pipe2[0],pipe1[1])?;

          ?

          waitpid(childpid,NULL,0)?;

          exit(0)?;

          }

          //////////////////client

          void client(int readfd, int writefd)

          {

          ?????? size_t len;

          ssize_t n;

          char? buff[MAXLINE];

          ?

          fgets(buff,MAXLINET,stdin);

          len = strlen(buff);

          if(buff[len-1]==’\n’)

          ?????? len--;

          write(writefd,buff,len);

          while((n=read(readfd,buff,MAXLINE))>0)

          ??????? write(STDOUT_FILENO,buff,n);

          }

          ///////////////////server

          void server(int readfd, int writefd)

          {

          ?????? int fd;

          ?????? ssize_t n;

          ?????? char buff[MAXLINE+1];

          ?

          ?????? if((n=read(readfd,buff,MAXLINE))==0)

          ????????? err_quit(“end-of –file while reading pathname”);

          ?????? if((fd =open(buff,O_RDONLY)<0)){

          sprintf(buff+n,sizeof(buff)-n,”can’t open, %s\n”,strerror(errno));

          n = strlen(buff);

          write(writefd,buff,n);

          }else{

          ?While((n=read(fd,buff,MAXLINE))>0)

          ??????? Write(writefd,buff,n);

          Close(fd);

          }

          }


          Properties of Pipe:

          1) Pipes do not have a name. For this reason, the processes must share a parent process. This is the main drawback to pipes. However, pipes are treated as file descriptors, so the pipes remain open even after fork and exec.

          2) Pipes do not distinguish between messages; they just read a fixed number of bytes. New line (\n) can be used to separate messages. A structure with a length field can be used for message containing binary data.

          3) Pipes can also be used to get the output of a command or to provide input to a command


          ?[MS1] 換句話說就是父進程對管道 1 只能寫,以下可以依此類推。

          posted on 2006-06-14 20:14 離弦之ray的技術天空 閱讀(263) 評論(0)  編輯  收藏 所屬分類: Linux&C

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 安塞县| 视频| 湟源县| 盘锦市| 苏尼特左旗| 土默特右旗| 秦安县| 涿州市| 莱西市| 永康市| 玉溪市| 芦山县| 泉州市| 来安县| 宝坻区| 都江堰市| 从江县| 蓬莱市| 静宁县| 临邑县| 苏州市| 鹤岗市| 临夏县| 柳江县| 东辽县| 福贡县| 稷山县| 彭州市| 抚宁县| 嘉禾县| 琼中| 周宁县| 乌恰县| 德令哈市| 土默特右旗| 紫金县| 湛江市| 武宣县| 大连市| 葫芦岛市| 兰坪|