pipe(2) System Calls Manual pipe(2) pipe, pipe2 - (libc -lc) #include int pipe(int pipefd[2]); #define _GNU_SOURCE /* feature_test_macros(7) */ #include /* O_* */ #include int pipe2(int pipefd[2], int flags); /* Alpha IA-64 MIPS SuperH SPARC/SPARC64 pipe() 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(). OR flags : O_CLOEXEC (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 Linux 5.8 . . . -1 errno pipefd . 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 flags (CONFIG_WATCH_QUEUE) . System V ABI ( Alpha IA-64 MIPS SuperH SPARC/SPARC64) () pipe() : . glibc pipe() . syscall(2) . POSIX.1-2024. pipe() POSIX.1-2001. pipe2() POSIX.1-2024. Linux 2.6.27, glibc 2.9. fork(2) . fork(2) ( pipe(7)). . #include #include #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) err(EXIT_FAILURE, "pipe"); cpid = fork(); if (cpid == -1) err(EXIT_FAILURE, "fork"); if (cpid == 0) { /* Child reads from pipe */ if (close(pipefd[1]) == -1) /* Close unused write end */ err(EXIT_FAILURE, "close"); while (read(pipefd[0], &buf, 1) > 0) { if (write(STDOUT_FILENO, &buf, 1) != 1) err(EXIT_FAILURE, "write"); } if (write(STDOUT_FILENO, "\n", 1) != 1) err(EXIT_FAILURE, "write"); if (close(pipefd[0]) == -1) err(EXIT_FAILURE, "close"); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ if (close(pipefd[0]) == -1) /* Close unused read end */ err(EXIT_FAILURE, "close"); if (write(pipefd[1], argv[1], strlen(argv[1])) != strlen(argv[1])) err(EXIT_FAILURE, "write"); if (close(pipefd[1]) == -1) /* Reader will see EOF */ err(EXIT_FAILURE, "close"); if (wait(NULL) == -1) /* Wait for child */ err(EXIT_FAILURE, "wait"); exit(EXIT_SUCCESS); } } fork(2), read(2), socketpair(2), splice(2), tee(2), vmsplice(2), write(2), popen(3), pipe(7) 3 . . : . 6.18 8 2026 pipe(2)