pipe()

NAME
pipe, pipe2 - create pipe

SYNOPSIS
#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>

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

DESCRIPTION
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).

If flags is 0, then pipe2() is the same as pipe(). The following
values can be bitwise ORed in flags to obtain different behavior:

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.

RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno
is set appropriately.

先在父进程中创建管道,然后创建子进程,子进程复制了父进程管道文件的文件描述符,所以父进程和子进程各具有2个管道描述符,当在子进程中关闭读端, 这时关闭的是子进程中管道文件的读端,而父进程的读端没有关闭,这时子进程往写段写数据的时候,因管道读端未完全关闭,所以父进程不会收到SIGPIPE的信号。

原文地址:https://www.cnblogs.com/feiling/p/3450792.html