OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / draft / man2 / pipe.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
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>.)
6 .\"
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.
10 .\"
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.
15 .\"
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
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
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
33 .\"     to EXAMPLE text.
34 .\" 2008-10-10, mtk: add description of pipe2()
35 .\"
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
45 .\"
46 .TH PIPE 2 2009-09-15 "Linux" "Linux Programmer's Manual"
47 .\"O .SH NAME
48 .SH Ì¾Á°
49 .\"O pipe, pipe2 \- create pipe
50 pipe, pipe2 \- ¥Ñ¥¤¥×¤òÀ¸À®¤¹¤ë
51 .\"O .SH SYNOPSIS
52 .SH ½ñ¼°
53 .nf
54 .B #include <unistd.h>
55 .sp
56 .BI "int pipe(int " pipefd "[2]);"
57 .sp
58 .B #define _GNU_SOURCE
59 .B #include <unistd.h>
60 .sp
61 .BI "int pipe2(int " pipefd "[2], int " flags );
62 .fi
63 .\"O .SH DESCRIPTION
64 .SH ÀâÌÀ
65 .\"O .BR pipe ()
66 .\"O creates a pipe, a unidirectional data channel that
67 .\"O can be used for interprocess communication.
68 .\"O The array
69 .\"O .IR pipefd
70 .\"O is used to return two file descriptors referring to the ends of the pipe.
71 .\"O .IR pipefd[0]
72 .\"O refers to the read end of the pipe.
73 .\"O .IR pipefd[1]
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
78 .\"O .BR pipe (7).
79 .BR pipe (2)
80 ¤Ï¥Ñ¥¤¥×¤òÀ¸À®¤¹¤ë¡£
81 ¥Ñ¥¤¥×¤Ï¡¢¥×¥í¥»¥¹´ÖÄÌ¿®¤Ë»ÈÍѤǤ­¤ëñÊý¸þ¤Î¥Ç¡¼¥¿¥Á¥ã¥Í¥ë¤Ç¤¢¤ë¡£
82 ÇÛÎó
83 .I pipefd
84 ¤Ï¡¢¥Ñ¥¤¥×¤Îξü¤ò»²¾È¤¹¤ëÆó¤Ä¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
85 ÊÖ¤¹¤Î¤Ë»ÈÍѤµ¤ì¤ë¡£
86 .I pipefd[0]
87 ¤¬¥Ñ¥¤¥×¤ÎÆɤ߽Ф·Â¦¡¢
88 .I pipefd[1]
89 ¤¬¥Ñ¥¤¥×¤Î½ñ¤­¹þ¤ß¦¤Ç¤¢¤ë¡£
90 ¥Ñ¥¤¥×¤Î½ñ¤­¹þ¤ß¦¤Ë½ñ¤­¹þ¤Þ¤ì¤¿¥Ç¡¼¥¿¤Ï¡¢
91 ¥Ñ¥¤¥×¤ÎÆɤ߽Ф·Â¦¤«¤éÆɤ߽Фµ¤ì¤ë¤Þ¤Ç¥«¡¼¥Í¥ë¤Ç¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤ë¡£
92 ¤µ¤é¤Ê¤ë¾ÜºÙ¤Ï
93 .BR pipe (7)
94 ¤ò»²¾È¤Î¤³¤È¡£
95
96 .\"O If
97 .\"O .IR flags
98 .\"O is 0, then
99 .\"O .BR pipe2 ()
100 .\"O is the same as
101 .\"O .BR pipe ().
102 .\"O The following values can be bitwise ORed in
103 .\"O .IR flags
104 .\"O to obtain different behavior:
105 .BR pipe2 ()
106 ¤Ï
107 .I flags
108 ¤¬ 0 ¤Î¾ì¹ç¤Ë¤Ï
109 .BR pipe ()
110 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
111 .I flags
112 ¤Ë°Ê²¼¤ÎÃͤò¥Ó¥Ã¥ÈËè¤ÎÏÀÍýÏ (OR) ¤Ç»ØÄꤹ¤ë¤³¤È¤Ç¡¢
113 °Û¤Ê¤ëÆ°ºî¤ò¤µ¤»¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
114 .TP 12
115 .B O_NONBLOCK
116 .\"O Set the
117 .\"O .BR O_NONBLOCK
118 .\"O file status flag on the two new open file descriptions.
119 .\"O Using this flag saves extra calls to
120 .\"O .BR fcntl (2)
121 .\"O to achieve the same result.
122 ¿·¤·¤¯À¸À®¤µ¤ì¤ëÆó¤Ä¤Î¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ­½Ò (open file description) ¤Î
123 .B O_NONBLOCK
124 ¥Õ¥¡¥¤¥ë¥¹¥Æ¡¼¥¿¥¹¥Õ¥é¥°¤ò¥»¥Ã¥È¤¹¤ë¡£
125 ¤³¤Î¥Õ¥é¥°¤ò»È¤¦¤³¤È¤Ç¡¢
126 .B O_NONBLOCK
127 ¤ò¥»¥Ã¥È¤¹¤ë¤¿¤á¤Ë
128 .BR fcntl (2)
129 ¤òÄɲäǸƤӽФ¹É¬Íפ¬¤Ê¤¯¤Ê¤ë¡£
130 .TP
131 .B O_CLOEXEC
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
136 .\"O .BR open (2)
137 .\"O for reasons why this may be useful.
138 ¿·¤·¤¯À¸À®¤µ¤ì¤ëÆó¤Ä¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
139 close-on-exec
140 .RB ( FD_CLOEXEC )
141 ¥Õ¥é¥°¤ò¥»¥Ã¥È¤¹¤ë¡£
142 ¤³¤Î¥Õ¥é¥°¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
143 .BR open (2)
144 ¤Î
145 .B O_CLOEXEC
146 ¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
147 .\"O .SH "RETURN VALUE"
148 .SH ÊÖ¤êÃÍ
149 .\"O On success, zero is returned.
150 .\"O On error, \-1 is returned, and
151 .\"O .I errno
152 .\"O is set appropriately.
153 À®¸ù¤·¤¿¾ì¹ç 0 ¤¬Ê֤롣¼ºÇÔ¤·¤¿¾ì¹ç \-1 ¤¬Ê֤ꡢ
154 .I errno
155 ¤¬¥¨¥é¡¼¤ÎÆâÍƤ˽¾¤Ã¤ÆÀßÄꤵ¤ì¤ë¡£
156 .\"O .SH ERRORS
157 .SH ¥¨¥é¡¼
158 .TP
159 .B EFAULT
160 .\"O .I pipefd
161 .\"O is not valid.
162 .I pipefd
163 ¤¬Ìµ¸ú¤ÊÃͤǤ¢¤ë¡£
164 .TP
165 .B EINVAL
166 .\"O .RB ( pipe2 ())
167 .\"O Invalid value in
168 .\"O .IR flags .
169 .RB ( pipe2 ())
170 .I flags
171 ¤Ë̵¸ú¤ÊÃͤ¬Æþ¤Ã¤Æ¤¤¤ë¡£
172 .TP
173 .B EMFILE
174 .\"O Too many file descriptors are in use by the process.
175 ¤³¤Î¥×¥í¥»¥¹¤Ç»È¤ï¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Â¿¤¹¤®¤ë¡£
176 .TP
177 .B ENFILE
178 .\"O The system limit on the total number of open files has been reached.
179 ¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¤ÎÁí¿ô¤¬¥·¥¹¥Æ¥à¤ÎÀ©¸Â¤Ë㤷¤¿¡£
180 .\"O .SH VERSIONS
181 .SH ¥Ð¡¼¥¸¥ç¥ó
182 .\"O .BR pipe2 ()
183 .\"O was added to Linux in version 2.6.27;
184 .\"O glibc support is available starting with
185 .\"O version 2.9.
186 .BR pipe2 ()
187 ¤Ï¥Ð¡¼¥¸¥ç¥ó 2.6.27 ¤Ç Linux ¤ËÄɲ䵤줿¡£
188 glibc ¤Ë¤è¤ë¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.9 °Ê¹ß¤ÇÍøÍѤǤ­¤ë¡£
189 .\"O .SH "CONFORMING TO"
190 .SH ½àµò
191 .BR pipe ():
192 POSIX.1-2001.
193
194 .\"O .BR pipe2 ()
195 .\"O is Linux-specific.
196 .BR pipe2 ()
197 ¤Ï Linux ¸ÇÍ­¤Ç¤¢¤ë¡£
198 .\"O .SH EXAMPLE
199 .SH Îã
200 .\"O .\" fork.2 refers to this example program.
201 .\" fork.2 ¤Ï¤³¤ÎÎã¤Î¥×¥í¥°¥é¥à¤ò»²¾È¤·¤Æ¤¤¤ë¡£
202 .\"O The following program creates a pipe, and then
203 .\"O .BR fork (2)s
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 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ç¤Ï¥Ñ¥¤¥×¤òÀ¸À®¤·¡¢¤½¤Î¸å
208 .BR fork (2)
209 ¤Ç»Ò¥×¥í¥»¥¹¤òÀ¸À®¤¹¤ë¡£
210 »Ò¥×¥í¥»¥¹¤ÏƱ¤¸¥Ñ¥¤¥×¤ò»²¾È¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Î¥³¥Ô¡¼¤ò
211 ·Ñ¾µ¤¹¤ë¡£
212 .\"O After the
213 .\"O .BR fork (2),
214 .\"O each process closes the descriptors that it doesn't need for the pipe
215 .\"O (see
216 .\"O .BR pipe (7)).
217 .BR fork (2)
218 ¤Î¸å¡¢³Æ¥×¥í¥»¥¹¤Ï¥Ñ¥¤¥×
219 .RB ( pipe (7)
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 ¥Ð¥¤¥È¤º¤ÄÆɤ߹þ¤ó¤Çɸ½à½ÐÎϤ˥¨¥³¡¼¤¹¤ë¡£
228 .nf
229
230 #include <sys/wait.h>
231 #include <stdio.h>
232 #include <stdlib.h>
233 #include <unistd.h>
234 #include <string.h>
235
236 int
237 main(int argc, char *argv[])
238 {
239     int pipefd[2];
240     pid_t cpid;
241     char buf;
242
243     if (argc != 2) {
244         fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
245         exit(EXIT_FAILURE);
246     }
247
248     if (pipe(pipefd) == \-1) {
249         perror("pipe");
250         exit(EXIT_FAILURE);
251     }
252
253     cpid = fork();
254     if (cpid == \-1) {
255         perror("fork");
256         exit(EXIT_FAILURE);
257     }
258
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 Â¦¤Ï¥¯¥í¡¼¥º¤¹¤ë */
263
264         while (read(pipefd[0], &buf, 1) > 0)
265             write(STDOUT_FILENO, &buf, 1);
266
267         write(STDOUT_FILENO, "\\n", 1);
268         close(pipefd[0]);
269         _exit(EXIT_SUCCESS);
270
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);                /* »Ò¥×¥í¥»¥¹¤òÂԤĠ*/
281         exit(EXIT_SUCCESS);
282     }
283 }
284 .fi
285 .\"O .SH "SEE ALSO"
286 .SH ´ØÏ¢¹àÌÜ
287 .BR fork (2),
288 .BR read (2),
289 .BR socketpair (2),
290 .BR write (2),
291 .BR popen (3),
292 .BR pipe (7)