pipe(2) System Calls Manual pipe(2) pipe, pipe2 - LIBRARY Standard C library (libc, -lc) #include int pipe(int pipefd[2]); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include /* Definition of O_* constants */ #include int pipe2(int pipefd[2], int flags); /* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64, pipe() has the following prototype; see VERSIONS */ #include struct fd_pair { long fd[2]; }; struct fd_pair pipe(void); pipe() , . pipefd , . pipefd[0] . pipefd[1] . , , , . . pipe(7). flags 0, pipe2() pipe(). flags : O_CLOEXEC close-on-exec (FD_CLOEXEC) . open(2) , . O_DIRECT ( Linux 3.4) , - <<>> . write(2) , read(2) . : o PIPE_BUF ( pipe(7)) . PIPE_BUF . o read(2) , , . PIPE_BUF ( ). o ( read(2) 0). , , EINVAL. Linux 4.5, O_DIRECT fcntl(2). O_NONBLOCK O_NONBLOCK , . fcntl(2) . O_NOTIFICATION_PIPE Since Linux 5.8, general notification mechanism is built on the top of the pipe where kernel splices notification messages into pipes opened by user space. The owner of the pipe has to tell the kernel which sources of events to watch and filters can also be applied to select which subevents should be placed into the pipe. On success, zero is returned. On error, -1 is returned, errno is set to indicate the error, and pipefd is left unchanged. Linux ( ) pipe() pipefd . POSIX.1-2008 TC2. Linux pipe2() pipefd . EFAULT pipefd . EINVAL (pipe2()) flags. EMFILE . ENFILE . ENFILE ; pipe(7). ENOPKG (pipe2()) O_NOTIFICATION_PIPE was passed in flags and support for notifications (CONFIG_WATCH_QUEUE) is not compiled into the kernel. System V ABI ; (Alpha, IA-64, MIPS, SuperH SPARC/SPARC64) pipe() : . glibc pipe() . syscall(2). pipe() POSIX.1-2008. pipe2() Linux. pipe() POSIX.1-2001. pipe2() Linux 2.6.27, glibc 2.9. , fork(2) ; , . fork(2) , (. pipe(7)). , , , , . #include #include #include #include #include int main(int argc, char *argv[]) { int pipefd[2]; char buf; pid_t cpid; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from 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); } } . fork(2), read(2), socketpair(2), splice(2), tee(2), vmsplice(2), write(2), popen(3), pipe(7) Alexey, Azamat Hackimov , kogamatranslator49 , Kogan, Max Is , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . pipe(2)