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 .\"*******************************************************************
38 .\" This file was generated with po4a. Translate the source file.
40 .\"*******************************************************************
41 .TH PIPE 2 2012\-02\-14 Linux "Linux Programmer's Manual"
43 pipe, pipe2 \- パイプを生成する
46 \fB#include <unistd.h>\fP
48 \fBint pipe(int \fP\fIpipefd\fP\fB[2]);\fP
50 \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */
51 \fB#include <fcntl.h>\fP /* O_* 定数の定義の取得 */
52 \fB#include <unistd.h>\fP
54 \fBint pipe2(int \fP\fIpipefd\fP\fB[2], int \fP\fIflags\fP\fB);\fP
57 \fBpipe\fP(2) はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 \fIpipefd\fP
58 は、パイプの両端を参照する二つのファイルディスクリプタを 返すのに使用される。 \fIpipefd[0]\fP がパイプの読み出し側、
59 \fIpipefd[1]\fP がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、
60 パイプの読み出し側から読み出されるまでカーネルでバッファリングされる。 さらなる詳細は \fBpipe\fP(7) を参照のこと。
62 \fBpipe2\fP() は \fIflags\fP が 0 の場合には \fBpipe\fP() と同じである。 \fIflags\fP に以下の値をビット毎の論理和
63 (OR) で指定することで、 異なる動作をさせることができる。
66 新しく生成される二つのオープンファイル記述 (open file description) の \fBO_NONBLOCK\fP
67 ファイルステータスフラグをセットする。 このフラグを使うことで、 \fBO_NONBLOCK\fP をセットするために \fBfcntl\fP(2)
71 新しく生成される二つのファイルディスクリプタの close\-on\-exec (\fBFD_CLOEXEC\fP) フラグをセットする。
72 このフラグが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの説明を参照のこと。
74 成功した場合は 0 が返される。エラーの場合は \-1 が返され、 \fIerrno\fP が適切に設定される。
78 \fIpipefd\fP が無効な値である。
81 (\fBpipe2\fP()) \fIflags\fP に無効な値が入っている。
84 このプロセスで使われているファイルディスクリプタが多すぎる。
87 オープンされているファイルの総数がシステムの制限に達している。
89 \fBpipe2\fP() はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。
91 \fBpipe\fP(): POSIX.1\-2001.
93 \fBpipe2\fP() は Linux 固有である。
95 .\" fork.2 refers to this example program.
96 以下のプログラムではパイプを生成し、その後 \fBfork\fP(2) で子プロセスを生成する。
97 子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピーを 継承する。 \fBfork\fP(2) の後、各プロセスはパイプ
98 (\fBpipe\fP(7) を参照) に必要がなくなったディスクリプタをクローズする。 親プロセスはプログラムのコマンドライン引き数に含まれる
99 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
102 #include <sys/wait.h>
109 main(int argc, char *argv[])
116 fprintf(stderr, "Usage: %s <string>\en", argv[0]);
120 if (pipe(pipefd) == \-1) {
131 if (cpid == 0) { /* 子プロセスがパイプから読み込む */
132 close(pipefd[1]); /* 使用しない write 側はクローズする */
134 while (read(pipefd[0], &buf, 1) > 0)
135 write(STDOUT_FILENO, &buf, 1);
137 write(STDOUT_FILENO, "\en", 1);
141 } else { /* 親プロセスは argv[1] をパイプへ書き込む */
142 close(pipefd[0]); /* 使用しない read 側はクローズする */
143 write(pipefd[1], argv[1], strlen(argv[1]));
144 close(pipefd[1]); /* 読み込み側が EOF に出会う */
145 wait(NULL); /* 子プロセスを待つ */
151 \fBfork\fP(2), \fBread\fP(2), \fBsocketpair\fP(2), \fBwrite\fP(2), \fBpopen\fP(3),