1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) 2005, 2008, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" (A few fragments remain from an earlier (1992) version by
5 .\" Drew Eckhardt <drew@cs.colorado.edu>.)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified by Michael Haardt <michael@moria.de>
28 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Modified 2005, mtk: added an example program
32 .\" Modified 2008-01-09, mtk: rewrote DESCRIPTION; minor additions
34 .\" 2008-10-10, mtk: add description of pipe2()
36 .\" Japanese Version Copyright (c) 1997 SUTO, Mitsuaki
37 .\" all rights reserved.
38 .\" Translated Thu Jun 26 21:09:51 JST 1997
39 .\" by SUTO, Mitsuaki <suto@av.crl.sony.co.jp>
40 .\" Updated & Modified Thu Feb 10 00:47:11 JST 2005
41 .\" by Yuichi SATO <ysato444@yahoo.co.jp>
42 .\" Updated & Modified Sat Dec 17 08:10:16 JST 2005 by Yuichi SATO
43 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
44 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
46 .TH PIPE 2 2009-09-15 "Linux" "Linux Programmer's Manual"
49 .\"O pipe, pipe2 \- create pipe
50 pipe, pipe2 \- ¥Ñ¥¤¥×¤òÀ¸À®¤¹¤ë
54 .B #include <unistd.h>
56 .BI "int pipe(int " pipefd "[2]);"
58 .B #define _GNU_SOURCE
59 .B #include <unistd.h>
61 .BI "int pipe2(int " pipefd "[2], int " flags );
66 .\"O creates a pipe, a unidirectional data channel that
67 .\"O can be used for interprocess communication.
70 .\"O is used to return two file descriptors referring to the ends of the pipe.
72 .\"O refers to the read end of the pipe.
74 .\"O refers to the write end of the pipe.
75 .\"O Data written to the write end of the pipe is buffered by the kernel
76 .\"O until it is read from the read end of the pipe.
77 .\"O For further details, see
81 ¥Ñ¥¤¥×¤Ï¡¢¥×¥í¥»¥¹´ÖÄÌ¿®¤Ë»ÈÍѤǤ¤ëñÊý¸þ¤Î¥Ç¡¼¥¿¥Á¥ã¥Í¥ë¤Ç¤¢¤ë¡£
84 ¤Ï¡¢¥Ñ¥¤¥×¤Îξü¤ò»²¾È¤¹¤ëÆó¤Ä¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
87 ¤¬¥Ñ¥¤¥×¤ÎÆɤ߽Ф·Â¦¡¢
89 ¤¬¥Ñ¥¤¥×¤Î½ñ¤¹þ¤ß¦¤Ç¤¢¤ë¡£
90 ¥Ñ¥¤¥×¤Î½ñ¤¹þ¤ß¦¤Ë½ñ¤¹þ¤Þ¤ì¤¿¥Ç¡¼¥¿¤Ï¡¢
91 ¥Ñ¥¤¥×¤ÎÆɤ߽Ф·Â¦¤«¤éÆɤ߽Фµ¤ì¤ë¤Þ¤Ç¥«¡¼¥Í¥ë¤Ç¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤ë¡£
102 .\"O The following values can be bitwise ORed in
104 .\"O to obtain different behavior:
112 ¤Ë°Ê²¼¤ÎÃͤò¥Ó¥Ã¥ÈËè¤ÎÏÀÍýÏ (OR) ¤Ç»ØÄꤹ¤ë¤³¤È¤Ç¡¢
113 °Û¤Ê¤ëÆ°ºî¤ò¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤ë¡£
118 .\"O file status flag on the two new open file descriptions.
119 .\"O Using this flag saves extra calls to
121 .\"O to achieve the same result.
122 ¿·¤·¤¯À¸À®¤µ¤ì¤ëÆó¤Ä¤Î¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò (open file description) ¤Î
124 ¥Õ¥¡¥¤¥ë¥¹¥Æ¡¼¥¿¥¹¥Õ¥é¥°¤ò¥»¥Ã¥È¤¹¤ë¡£
125 ¤³¤Î¥Õ¥é¥°¤ò»È¤¦¤³¤È¤Ç¡¢
129 ¤òÄɲäǸƤӽФ¹É¬Íפ¬¤Ê¤¯¤Ê¤ë¡£
132 .\"O Set the close-on-exec
133 .\"O .RB ( FD_CLOEXEC )
134 .\"O flag on the two new file descriptors.
135 .\"O See the description of the same flag in
137 .\"O for reasons why this may be useful.
138 ¿·¤·¤¯À¸À®¤µ¤ì¤ëÆó¤Ä¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
142 ¤³¤Î¥Õ¥é¥°¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
146 ¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
147 .\"O .SH "RETURN VALUE"
149 .\"O On success, zero is returned.
150 .\"O On error, \-1 is returned, and
152 .\"O is set appropriately.
153 À®¸ù¤·¤¿¾ì¹ç 0 ¤¬Ê֤롣¼ºÇÔ¤·¤¿¾ì¹ç \-1 ¤¬Ê֤ꡢ
155 ¤¬¥¨¥é¡¼¤ÎÆâÍƤ˽¾¤Ã¤ÆÀßÄꤵ¤ì¤ë¡£
167 .\"O Invalid value in
171 ¤Ë̵¸ú¤ÊÃͤ¬Æþ¤Ã¤Æ¤¤¤ë¡£
174 .\"O Too many file descriptors are in use by the process.
175 ¤³¤Î¥×¥í¥»¥¹¤Ç»È¤ï¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Â¿¤¹¤®¤ë¡£
178 .\"O The system limit on the total number of open files has been reached.
179 ¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¤ÎÁí¿ô¤¬¥·¥¹¥Æ¥à¤ÎÀ©¸Â¤Ë㤷¤¿¡£
183 .\"O was added to Linux in version 2.6.27;
184 .\"O glibc support is available starting with
187 ¤Ï¥Ð¡¼¥¸¥ç¥ó 2.6.27 ¤Ç Linux ¤ËÄɲ䵤줿¡£
188 glibc ¤Ë¤è¤ë¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.9 °Ê¹ß¤ÇÍøÍѤǤ¤ë¡£
189 .\"O .SH "CONFORMING TO"
195 .\"O is Linux-specific.
197 ¤Ï Linux ¸ÇͤǤ¢¤ë¡£
200 .\"O .\" fork.2 refers to this example program.
201 .\" fork.2 ¤Ï¤³¤ÎÎã¤Î¥×¥í¥°¥é¥à¤ò»²¾È¤·¤Æ¤¤¤ë¡£
202 .\"O The following program creates a pipe, and then
204 .\"O to create a child process;
205 .\"O the child inherits a duplicate set of file
206 .\"O descriptors that refer to the same pipe.
207 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ç¤Ï¥Ñ¥¤¥×¤òÀ¸À®¤·¡¢¤½¤Î¸å
209 ¤Ç»Ò¥×¥í¥»¥¹¤òÀ¸À®¤¹¤ë¡£
210 »Ò¥×¥í¥»¥¹¤ÏƱ¤¸¥Ñ¥¤¥×¤ò»²¾È¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Î¥³¥Ô¡¼¤ò
214 .\"O each process closes the descriptors that it doesn't need for the pipe
218 ¤Î¸å¡¢³Æ¥×¥í¥»¥¹¤Ï¥Ñ¥¤¥×
220 ¤ò»²¾È) ¤ËɬÍפ¬¤Ê¤¯¤Ê¤Ã¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤¹¤ë¡£
221 .\"O The parent then writes the string contained in the program's
222 .\"O command-line argument to the pipe,
223 .\"O and the child reads this string a byte at a time from the pipe
224 .\"O and echoes it on standard output.
225 ¿Æ¥×¥í¥»¥¹¤Ï¥×¥í¥°¥é¥à¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤Ë´Þ¤Þ¤ì¤ë
226 ʸ»úÎó¤ò¥Ñ¥¤¥×¤Ø½ñ¤¹þ¤ß¡¢
227 »Ò¥×¥í¥»¥¹¤Ï¤³¤Îʸ»úÎó¤ò¥Ñ¥¤¥×¤«¤é 1 ¥Ð¥¤¥È¤º¤ÄÆɤ߹þ¤ó¤Çɸ½à½ÐÎϤ˥¨¥³¡¼¤¹¤ë¡£
230 #include <sys/wait.h>
237 main(int argc, char *argv[])
244 fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
248 if (pipe(pipefd) == \-1) {
259 .\"O if (cpid == 0) { /* Child reads from pipe */
260 if (cpid == 0) { /* »Ò¥×¥í¥»¥¹¤¬¥Ñ¥¤¥×¤«¤éÆɤ߹þ¤à */
261 .\"O close(pipefd[1]); /* Close unused write end */
262 close(pipefd[1]); /* »ÈÍѤ·¤Ê¤¤ write ¦¤Ï¥¯¥í¡¼¥º¤¹¤ë */
264 while (read(pipefd[0], &buf, 1) > 0)
265 write(STDOUT_FILENO, &buf, 1);
267 write(STDOUT_FILENO, "\\n", 1);
271 .\"O } else { /* Parent writes argv[1] to pipe */
272 .\"O close(pipefd[0]); /* Close unused read end */
273 .\"O write(pipefd[1], argv[1], strlen(argv[1]));
274 .\"O close(pipefd[1]); /* Reader will see EOF */
275 .\"O wait(NULL); /* Wait for child */
276 } else { /* ¿Æ¥×¥í¥»¥¹¤Ï argv[1] ¤ò¥Ñ¥¤¥×¤Ø½ñ¤¹þ¤à */
277 close(pipefd[0]); /* »ÈÍѤ·¤Ê¤¤ read ¦¤Ï¥¯¥í¡¼¥º¤¹¤ë */
278 write(pipefd[1], argv[1], strlen(argv[1]));
279 close(pipefd[1]); /* Æɤ߹þ¤ß¦¤¬ EOF ¤Ë½Ð²ñ¤¦ */
280 wait(NULL); /* »Ò¥×¥í¥»¥¹¤òÂÔ¤Ä */