]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/pipe.2
Added comment noting that fork.2 refers to the example program on this
[thirdparty/man-pages.git] / man2 / pipe.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
4 .\"
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .\" Modified by Michael Haardt <michael@moria.de>
26 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified 2004-06-17 by Michael Kerrisk <mtk-manpages@gmx.net>
29 .\"
30 .TH PIPE 2 2004-06-17 "Linux 2.6.7" "Linux Programmer's Manual"
31 .SH NAME
32 pipe \- create pipe
33 .SH SYNOPSIS
34 .B #include <unistd.h>
35 .sp
36 .BI "int pipe(int " filedes "[2]);"
37 .SH DESCRIPTION
38 .BR pipe ()
39 creates a pair of file descriptors, pointing to a pipe inode, and places
40 them in the array pointed to by
41 .IR filedes .
42 .I filedes[0]
43 is for reading,
44 .I filedes[1]
45 is for writing.
46 .SH "RETURN VALUE"
47 On success, zero is returned. On error, \-1 is returned, and
48 .I errno
49 is set appropriately.
50 .SH ERRORS
51 .TP
52 .B EFAULT
53 .I filedes
54 is not valid.
55 .TP
56 .B EMFILE
57 Too many file descriptors are in use by the process.
58 .TP
59 .B ENFILE
60 The system limit on the total number of open files has been reached.
61 .SH "CONFORMING TO"
62 POSIX.1
63 .SH EXAMPLE
64 .\" fork.2 refers to this example program.
65 The following program creates a pipe, and then
66 .BR fork (2)s
67 to create a child process.
68 After the
69 .BR fork (2),
70 each process closes the descriptors that it doesn't need for the pipe
71 (see
72 .BR pipe (7)).
73 The parent then writes the string contained in the program's
74 command-line argument to the pipe,
75 and the child reads this string a byte at a time from the pipe
76 and echoes it on standard output.
77 .nf
78
79 #include <sys/wait.h>
80 #include <assert.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <unistd.h>
84 #include <string.h>
85
86 int
87 main(int argc, char *argv[])
88 {
89 int pfd[2];
90 pid_t cpid;
91 char buf;
92
93 assert(argc == 2);
94
95 if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
96
97 cpid = fork();
98 if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
99
100 if (cpid == 0) { /* Child reads from pipe */
101 close(pfd[1]); /* Close unused write end */
102
103 while (read(pfd[0], &buf, 1) > 0)
104 write(STDOUT_FILENO, &buf, 1);
105
106 write(STDOUT_FILENO, "\\n", 1);
107 close(pfd[0]);
108 _exit(EXIT_SUCCESS);
109
110 } else { /* Parent writes argv[1] to pipe */
111 close(pfd[0]); /* Close unused read end */
112 write(pfd[1], argv[1], strlen(argv[1]));
113 close(pfd[1]); /* Reader will see EOF */
114 wait(NULL); /* Wait for child */
115 exit(EXIT_SUCCESS);
116 }
117 }
118
119 .fi
120 .SH "SEE ALSO"
121 .BR fork (2),
122 .BR read (2),
123 .BR socketpair (2),
124 .BR write (2),
125 .BR pipe (7)