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 2010-09-10 "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 .\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
59 .BR "#define _GNU_SOURCE" " /* feature_test_macros(7) 参照 */"
60 .B #include <unistd.h>
62 .BI "int pipe2(int " pipefd "[2], int " flags );
67 .\"O creates a pipe, a unidirectional data channel that
68 .\"O can be used for interprocess communication.
71 .\"O is used to return two file descriptors referring to the ends of the pipe.
73 .\"O refers to the read end of the pipe.
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
82 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。
85 は、パイプの両端を参照する二つのファイルディスクリプタを
92 パイプの読み出し側から読み出されるまでカーネルでバッファリングされる。
103 .\"O The following values can be bitwise ORed in
105 .\"O to obtain different behavior:
113 に以下の値をビット毎の論理和 (OR) で指定することで、
119 .\"O file status flag on the two new open file descriptions.
120 .\"O Using this flag saves extra calls to
122 .\"O to achieve the same result.
123 新しく生成される二つのオープンファイル記述 (open file description) の
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
138 .\"O for reasons why this may be useful.
139 新しく生成される二つのファイルディスクリプタの
148 .\"O .SH "RETURN VALUE"
150 .\"O On success, zero is returned.
151 .\"O On error, \-1 is returned, and
153 .\"O is set appropriately.
154 成功した場合 0 が返る。失敗した場合 \-1 が返り、
168 .\"O Invalid value in
175 .\"O Too many file descriptors are in use by the process.
176 このプロセスで使われているファイルディスクリプタが多すぎる。
179 .\"O The system limit on the total number of open files has been reached.
180 オープンされているファイルの総数がシステムの制限に達した。
184 .\"O was added to Linux in version 2.6.27;
185 .\"O glibc support is available starting with
188 はバージョン 2.6.27 で Linux に追加された。
189 glibc によるサポートはバージョン 2.9 以降で利用できる。
190 .\"O .SH "CONFORMING TO"
196 .\"O is Linux-specific.
201 .\"O .\" fork.2 refers to this example program.
202 .\" fork.2 はこの例のプログラムを参照している。
203 .\"O The following program creates a pipe, and then
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 以下のプログラムではパイプを生成し、その後
211 子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピーを
215 .\"O each process closes the descriptors that it doesn't need for the pipe
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 親プロセスはプログラムのコマンドライン引き数に含まれる
228 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
231 #include <sys/wait.h>
238 main(int argc, char *argv[])
245 fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
249 if (pipe(pipefd) == \-1) {
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 側はクローズする */
265 while (read(pipefd[0], &buf, 1) > 0)
266 write(STDOUT_FILENO, &buf, 1);
268 write(STDOUT_FILENO, "\\n", 1);
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); /* 子プロセスを待つ */