【进程间通信】pipe2

http://man7.org/linux/man-pages/man2/pipe.2.html

       pipe, pipe2 - create pipe


       #include <unistd.h>

       int pipe(int pipefd[2]);

       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <fcntl.h>              /* Obtain O_* constant definitions */
       #include <unistd.h>



fcntl.h中有各种O_*常量的定义。


       int pipe2(int pipefd[2], int flags);


       pipe() creates a pipe, a unidirectional data channel that can be used
       for interprocess communication.  The array pipefd is used to return
       two file descriptors referring to the ends of the pipe.  pipefd[0]
       refers to the read end of the pipe.  pipefd[1] refers to the write
       end of the pipe.  Data written to the write end of the pipe is
       buffered by the kernel until it is read from the read end of the

       pipe.  For further details, see pipe(7).

pipe创建一个管道,一种没有方向的数据通道,可用于进程间的通信。

数组pipefd被用于返回两个文件描述符,代表管道的两端。

pipefd[0] 是管道的读端,pipefd[1]是管道的写端。

数据写入pipe的写端的时候被内核缓冲,直到被管道的读端读出。



       If flags is 0, then pipe2() is the same as pipe().  The following

       values can be bitwise ORed in flags to obtain different behavior:

如果flags为0,pipe2()就是pipe()了。

为两个打开的文件描述符设置为O_NONBLOCK。

还可以设置O_CLOEXEC


       O_NONBLOCK  Set the O_NONBLOCK file status flag on the two new open
                   file descriptions.  Using this flag saves extra calls to
                   fcntl(2) to achieve the same result.


       O_CLOEXEC   Set the close-on-exec (FD_CLOEXEC) flag on the two new
                   file descriptors.  See the description of the same flag

                   in open(2) for reasons why this may be useful.


成功,返回0,失败返回-1,错误值存在errno中。

RETURN VALUE         top


       On success, zero is returned.  On error, -1 is returned, and errno is
       set appropriately.
ERRORS         top


       EFAULT pipefd is not valid.


       EINVAL (pipe2()) Invalid value in flags.


       EMFILE Too many file descriptors are in use by the process.


       ENFILE The system limit on the total number of open files has been
              reached.
VERSIONS         top


       pipe2() was added to Linux in version 2.6.27; glibc support is
       available starting with version 2.9.
CONFORMING TO         top


       pipe(): POSIX.1-2001.


       pipe2() is Linux-specific.
EXAMPLE         top


测试程序:


       The following program creates a pipe, and then fork(2)s to create a
       child process; the child inherits a duplicate set of file descriptors
       that refer to the same pipe.  After the fork(2), each process closes
       the descriptors that it doesn't need for the pipe (see pipe(7)).  The
       parent then writes the string contained in the program's command-line
       argument to the pipe, and the child reads this string a byte at a

       time from the pipe and echoes it on standard output.


创建管道,并fork创建子进程,子进程继承了一耳光复制的文件描述符,指向之前创建的管道。

fork之后,每个进程每个进程关闭不需要的管道。

父进程写入字符串到管道中,子进程以字节的方式读取这个字符串,并在stdout上输出。




       #include <sys/wait.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <string.h>

       int
       main(int argc, char *argv[])
       {
           int pipefd[2];
           pid_t cpid;
           char buf;

           if (argc != 2) {
            fprintf(stderr, "Usage: %s <string>\n", argv[0]);
            exit(EXIT_FAILURE);
           }

           if (pipe(pipefd) == -1)  {  //pipe创建的管道啊
               perror("pipe");
               exit(EXIT_FAILURE);
           }

           cpid = fork(); //子进程
           if (cpid == -1) {
               perror("fork");
               exit(EXIT_FAILURE);
           }

           if (cpid == 0) {    /* Child reads from pipe */ //子进程从pipe中读取
               close(pipefd[1]);          /* Close unused write end */ //关闭不使用的写端

               while (read(pipefd[0], &buf, 1) > 0)  //一次读一个字节
                   write(STDOUT_FILENO, &buf, 1);

               write(STDOUT_FILENO, "\n", 1);
               close(pipefd[0]);
               _exit(EXIT_SUCCESS);

           } else {            /* Parent writes argv[1] to pipe */  //父进程写入到管道
               close(pipefd[0]);          /* Close unused read end */
               write(pipefd[1], argv[1], strlen(argv[1]));
               close(pipefd[1]);          /* Reader will see EOF */
               wait(NULL);                /* Wait for child */
               exit(EXIT_SUCCESS);
           }
       }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值