OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / draft / man2 / pipe.2
1 .\" Copyright (C) 2005, 2008, Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" (A few fragments remain from an earlier (1992) version by
3 .\" Drew Eckhardt <drew@cs.colorado.edu>.)
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
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 .\"*******************************************************************
37 .\"
38 .\" This file was generated with po4a. Translate the source file.
39 .\"
40 .\"*******************************************************************
41 .\"
42 .\" Japanese Version Copyright (c) 1997 SUTO, Mitsuaki
43 .\"         all rights reserved.
44 .\" Translated Thu Jun 26 21:09:51 JST 1997
45 .\"         by SUTO, Mitsuaki <suto@av.crl.sony.co.jp>
46 .\" Updated & Modified Thu Feb 10 00:47:11 JST 2005
47 .\"         by Yuichi SATO <ysato444@yahoo.co.jp>
48 .\" Updated & Modified Sat Dec 17 08:10:16 JST 2005 by Yuichi SATO
49 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
50 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
51 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
52 .\"
53 .TH PIPE 2 2014\-07\-08 Linux "Linux Programmer's Manual"
54 .SH 名前
55 pipe, pipe2 \- パイプを生成する
56 .SH 書式
57 .nf
58 \fB#include <unistd.h>\fP
59 .sp
60 \fBint pipe(int \fP\fIpipefd\fP\fB[2]);\fP
61 .sp
62 \fB#define _GNU_SOURCE\fP             /* feature_test_macros(7) 参照 */
63 \fB#include <fcntl.h>\fP              /* O_* 定数の定義の取得 */
64 \fB#include <unistd.h>\fP
65 .sp
66 \fBint pipe2(int \fP\fIpipefd\fP\fB[2], int \fP\fIflags\fP\fB);\fP
67 .fi
68 .SH 説明
69 \fBpipe\fP()  はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 \fIpipefd\fP
70 は、パイプの両端を参照する二つのファイルディスクリプターを 返すのに使用される。 \fIpipefd[0]\fP がパイプの読み出し側、
71 \fIpipefd[1]\fP がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、
72 パイプの読み出し側から読み出されるまでカーネルでバッファーリングされる。 さらなる詳細は \fBpipe\fP(7)  を参照のこと。
73
74 \fBpipe2\fP()  は \fIflags\fP が 0 の場合には \fBpipe\fP()  と同じである。 \fIflags\fP に以下の値をビット毎の論理和
75 (OR) で指定することで、 異なる動作をさせることができる。
76 .TP 
77 \fBO_CLOEXEC\fP
78 新しく生成される二つのファイルディスクリプターの close\-on\-exec (\fBFD_CLOEXEC\fP)  フラグをセットする。
79 このフラグが役に立つ理由については、 \fBopen\fP(2)  の \fBO_CLOEXEC\fP フラグの説明を参照のこと。
80 .TP 
81 \fBO_DIRECT\fP (Linux 3.4 以降)
82 .\" commit 9883035ae7edef3ec62ad215611cb8e17d6a1a5d
83 「パケット」モードで入出力を行うパイプを作成する。 このパイプへの \fBwrite\fP(2) それぞれが別のパケットとして扱われ、 このパイプからの
84 \fBread\fP(2) では一度に一つパケットが読み出される。 以下の点に注意すること。
85 .RS
86 .IP * 3
87 \fBPIPE_BUF\fP バイト (\fBpipe\fP(7) 参照) より大きいデータを書き込んだ場合、複数のパケットに分割される。
88 定数 \fBPIPE_BUF\fP は \fI<limits.h>\fP で定義されている。
89 .IP *
90 \fBread\fP(2)
91 で次のパケットよりも小さなバッファーサイズを指定した場合、要求されたバイト数のデータが読み出され、そのパケットの超過分のバイトは破棄される。
92 可能性のある最大サイズのパケットを読み出すには、\fBPIPE_BUF\fP のバッファーサイズを指定すれば十分である (上の項目を参照)。
93 .IP *
94 長さ 0 のパケットはサポートされていない。 (バッファーサイズ 0 を指定した \fBread\fP(2) は何も行わず 0 を返す)。
95 .RE
96 .IP
97 このフラグをサポートしていない古いカーネルでは、エラー \fBEINVAL\fP が返る。これによりカーネルがサポートしていないことが分かる。
98 .TP 
99 \fBO_NONBLOCK\fP
100 新しく生成される二つのオープンファイル記述 (open file description) の \fBO_NONBLOCK\fP
101 ファイルステータスフラグをセットする。 このフラグを使うことで、 \fBO_NONBLOCK\fP をセットするために \fBfcntl\fP(2)
102 を追加で呼び出す必要がなくなる。
103 .SH 返り値
104 成功した場合は 0 が返される。エラーの場合は \-1 が返され、 \fIerrno\fP が適切に設定される。
105 .SH エラー
106 .TP 
107 \fBEFAULT\fP
108 \fIpipefd\fP が無効な値である。
109 .TP 
110 \fBEINVAL\fP
111 (\fBpipe2\fP())  \fIflags\fP に無効な値が入っている。
112 .TP 
113 \fBEMFILE\fP
114 このプロセスで使われているファイルディスクリプターが多すぎる。
115 .TP 
116 \fBENFILE\fP
117 オープンされているファイルの総数がシステムの制限に達している。
118 .SH バージョン
119 \fBpipe2\fP()  はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。
120 .SH 準拠
121 \fBpipe\fP(): POSIX.1\-2001.
122
123 \fBpipe2\fP()  は Linux 固有である。
124 .SH 例
125 .\" fork.2 refers to this example program.
126 以下のプログラムではパイプを生成し、その後 \fBfork\fP(2)  で子プロセスを生成する。
127 子プロセスは同じパイプを参照するファイルディスクリプター集合のコピーを 継承する。 \fBfork\fP(2)  の後、各プロセスはパイプ
128 (\fBpipe\fP(7)  を参照) に必要がなくなったディスクリプターをクローズする。 親プロセスはプログラムのコマンドライン引き数に含まれる
129 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
130 .SS プログラムのソース
131 .nf
132 #include <sys/types.h>
133 #include <sys/wait.h>
134 #include <stdio.h>
135 #include <stdlib.h>
136 #include <unistd.h>
137 #include <string.h>
138
139 int
140 main(int argc, char *argv[])
141 {
142     int pipefd[2];
143     pid_t cpid;
144     char buf;
145
146     if (argc != 2) {
147         fprintf(stderr, "Usage: %s <string>\en", argv[0]);
148         exit(EXIT_FAILURE);
149     }
150
151     if (pipe(pipefd) == \-1) {
152         perror("pipe");
153         exit(EXIT_FAILURE);
154     }
155
156     cpid = fork();
157     if (cpid == \-1) {
158         perror("fork");
159         exit(EXIT_FAILURE);
160     }
161
162     if (cpid == 0) {    /* 子プロセスがパイプから読み込む */
163         close(pipefd[1]);  /* 使用しない write 側はクローズする */
164
165         while (read(pipefd[0], &buf, 1) > 0)
166             write(STDOUT_FILENO, &buf, 1);
167
168         write(STDOUT_FILENO, "\en", 1);
169         close(pipefd[0]);
170         _exit(EXIT_SUCCESS);
171
172     } else {            /* 親プロセスは argv[1] をパイプへ書き込む */
173         close(pipefd[0]);          /* 使用しない read 側はクローズする */
174         write(pipefd[1], argv[1], strlen(argv[1]));
175         close(pipefd[1]);          /* 読み込み側が EOF に出会う */
176         wait(NULL);                /* 子プロセスを待つ */
177         exit(EXIT_SUCCESS);
178     }
179 }
180 .fi
181 .SH 関連項目
182 \fBfork\fP(2), \fBread\fP(2), \fBsocketpair\fP(2), \fBwrite\fP(2), \fBpopen\fP(3),
183 \fBpipe\fP(7)
184 .SH この文書について
185 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
186 である。プロジェクトの説明とバグ報告に関する情報は
187 http://www.kernel.org/doc/man\-pages/ に書かれている。