OSDN Git Service

a75d32a6854ed9825e9370ae4db116c147e4a79e
[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 2010-09-10 "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 .\"O .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
59 .BR "#define _GNU_SOURCE" "             /* feature_test_macros(7) 参照 */"
60 .B #include <unistd.h>
61 .sp
62 .BI "int pipe2(int " pipefd "[2], int " flags );
63 .fi
64 .\"O .SH DESCRIPTION
65 .SH 説明
66 .\"O .BR pipe ()
67 .\"O creates a pipe, a unidirectional data channel that
68 .\"O can be used for interprocess communication.
69 .\"O The array
70 .\"O .IR pipefd
71 .\"O is used to return two file descriptors referring to the ends of the pipe.
72 .\"O .IR pipefd[0]
73 .\"O refers to the read end of the pipe.
74 .\"O .IR pipefd[1]
75 .\"O refers to the write end of the pipe.
76 .\"O Data written to the write end of the pipe is buffered by the kernel
77 .\"O until it is read from the read end of the pipe.
78 .\"O For further details, see
79 .\"O .BR pipe (7).
80 .BR pipe (2)
81 はパイプを生成する。
82 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。
83 配列
84 .I pipefd
85 は、パイプの両端を参照する二つのファイルディスクリプタを
86 返すのに使用される。
87 .I pipefd[0]
88 がパイプの読み出し側、
89 .I pipefd[1]
90 がパイプの書き込み側である。
91 パイプの書き込み側に書き込まれたデータは、
92 パイプの読み出し側から読み出されるまでカーネルでバッファリングされる。
93 さらなる詳細は
94 .BR pipe (7)
95 を参照のこと。
96
97 .\"O If
98 .\"O .IR flags
99 .\"O is 0, then
100 .\"O .BR pipe2 ()
101 .\"O is the same as
102 .\"O .BR pipe ().
103 .\"O The following values can be bitwise ORed in
104 .\"O .IR flags
105 .\"O to obtain different behavior:
106 .BR pipe2 ()
107
108 .I flags
109 が 0 の場合には
110 .BR pipe ()
111 と同じである。
112 .I flags
113 に以下の値をビット毎の論理和 (OR) で指定することで、
114 異なる動作をさせることができる。
115 .TP 12
116 .B O_NONBLOCK
117 .\"O Set the
118 .\"O .BR O_NONBLOCK
119 .\"O file status flag on the two new open file descriptions.
120 .\"O Using this flag saves extra calls to
121 .\"O .BR fcntl (2)
122 .\"O to achieve the same result.
123 新しく生成される二つのオープンファイル記述 (open file description) の
124 .B O_NONBLOCK
125 ファイルステータスフラグをセットする。
126 このフラグを使うことで、
127 .B O_NONBLOCK
128 をセットするために
129 .BR fcntl (2)
130 を追加で呼び出す必要がなくなる。
131 .TP
132 .B O_CLOEXEC
133 .\"O Set the close-on-exec
134 .\"O .RB ( FD_CLOEXEC )
135 .\"O flag on the two new file descriptors.
136 .\"O See the description of the same flag in
137 .\"O .BR open (2)
138 .\"O for reasons why this may be useful.
139 新しく生成される二つのファイルディスクリプタの
140 close-on-exec
141 .RB ( FD_CLOEXEC )
142 フラグをセットする。
143 このフラグが役に立つ理由については、
144 .BR open (2)
145
146 .B O_CLOEXEC
147 フラグの説明を参照のこと。
148 .\"O .SH "RETURN VALUE"
149 .SH 返り値
150 .\"O On success, zero is returned.
151 .\"O On error, \-1 is returned, and
152 .\"O .I errno
153 .\"O is set appropriately.
154 成功した場合 0 が返る。失敗した場合 \-1 が返り、
155 .I errno
156 がエラーの内容に従って設定される。
157 .\"O .SH ERRORS
158 .SH エラー
159 .TP
160 .B EFAULT
161 .\"O .I pipefd
162 .\"O is not valid.
163 .I pipefd
164 が無効な値である。
165 .TP
166 .B EINVAL
167 .\"O .RB ( pipe2 ())
168 .\"O Invalid value in
169 .\"O .IR flags .
170 .RB ( pipe2 ())
171 .I flags
172 に無効な値が入っている。
173 .TP
174 .B EMFILE
175 .\"O Too many file descriptors are in use by the process.
176 このプロセスで使われているファイルディスクリプタが多すぎる。
177 .TP
178 .B ENFILE
179 .\"O The system limit on the total number of open files has been reached.
180 オープンされているファイルの総数がシステムの制限に達した。
181 .\"O .SH VERSIONS
182 .SH バージョン
183 .\"O .BR pipe2 ()
184 .\"O was added to Linux in version 2.6.27;
185 .\"O glibc support is available starting with
186 .\"O version 2.9.
187 .BR pipe2 ()
188 はバージョン 2.6.27 で Linux に追加された。
189 glibc によるサポートはバージョン 2.9 以降で利用できる。
190 .\"O .SH "CONFORMING TO"
191 .SH 準拠
192 .BR pipe ():
193 POSIX.1-2001.
194
195 .\"O .BR pipe2 ()
196 .\"O is Linux-specific.
197 .BR pipe2 ()
198 は Linux 固有である。
199 .\"O .SH EXAMPLE
200 .SH 例
201 .\"O .\" fork.2 refers to this example program.
202 .\" fork.2 はこの例のプログラムを参照している。
203 .\"O The following program creates a pipe, and then
204 .\"O .BR fork (2)s
205 .\"O to create a child process;
206 .\"O the child inherits a duplicate set of file
207 .\"O descriptors that refer to the same pipe.
208 以下のプログラムではパイプを生成し、その後
209 .BR fork (2)
210 で子プロセスを生成する。
211 子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピーを
212 継承する。
213 .\"O After the
214 .\"O .BR fork (2),
215 .\"O each process closes the descriptors that it doesn't need for the pipe
216 .\"O (see
217 .\"O .BR pipe (7)).
218 .BR fork (2)
219 の後、各プロセスはパイプ
220 .RB ( pipe (7)
221 を参照) に必要がなくなったディスクリプタをクローズする。
222 .\"O The parent then writes the string contained in the program's
223 .\"O command-line argument to the pipe,
224 .\"O and the child reads this string a byte at a time from the pipe
225 .\"O and echoes it on standard output.
226 親プロセスはプログラムのコマンドライン引き数に含まれる
227 文字列をパイプへ書き込み、
228 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
229 .nf
230
231 #include <sys/wait.h>
232 #include <stdio.h>
233 #include <stdlib.h>
234 #include <unistd.h>
235 #include <string.h>
236
237 int
238 main(int argc, char *argv[])
239 {
240     int pipefd[2];
241     pid_t cpid;
242     char buf;
243
244     if (argc != 2) {
245         fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
246         exit(EXIT_FAILURE);
247     }
248
249     if (pipe(pipefd) == \-1) {
250         perror("pipe");
251         exit(EXIT_FAILURE);
252     }
253
254     cpid = fork();
255     if (cpid == \-1) {
256         perror("fork");
257         exit(EXIT_FAILURE);
258     }
259
260 .\"O     if (cpid == 0) {    /* Child reads from pipe */
261     if (cpid == 0) {    /* 子プロセスがパイプから読み込む */
262 .\"O         close(pipefd[1]);          /* Close unused write end */
263         close(pipefd[1]);  /* 使用しない write 側はクローズする */
264
265         while (read(pipefd[0], &buf, 1) > 0)
266             write(STDOUT_FILENO, &buf, 1);
267
268         write(STDOUT_FILENO, "\\n", 1);
269         close(pipefd[0]);
270         _exit(EXIT_SUCCESS);
271
272 .\"O     } else {            /* Parent writes argv[1] to pipe */
273 .\"O         close(pipefd[0]);          /* Close unused read end */
274 .\"O         write(pipefd[1], argv[1], strlen(argv[1]));
275 .\"O         close(pipefd[1]);          /* Reader will see EOF */
276 .\"O         wait(NULL);                /* Wait for child */
277     } else {            /* 親プロセスは argv[1] をパイプへ書き込む */
278         close(pipefd[0]);          /* 使用しない read 側はクローズする */
279         write(pipefd[1], argv[1], strlen(argv[1]));
280         close(pipefd[1]);          /* 読み込み側が EOF に出会う */
281         wait(NULL);                /* 子プロセスを待つ */
282         exit(EXIT_SUCCESS);
283     }
284 }
285 .fi
286 .\"O .SH "SEE ALSO"
287 .SH 関連項目
288 .BR fork (2),
289 .BR read (2),
290 .BR socketpair (2),
291 .BR write (2),
292 .BR popen (3),
293 .BR pipe (7)