OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / 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 .\"*******************************************************************
37 .\"
38 .\" This file was generated with po4a. Translate the source file.
39 .\"
40 .\"*******************************************************************
41 .TH PIPE 2 2012\-02\-14 Linux "Linux Programmer's Manual"
42 .SH 名前
43 pipe, pipe2 \- パイプを生成する
44 .SH 書式
45 .nf
46 \fB#include <unistd.h>\fP
47 .sp
48 \fBint pipe(int \fP\fIpipefd\fP\fB[2]);\fP
49 .sp
50 \fB#define _GNU_SOURCE\fP             /* feature_test_macros(7) 参照 */
51 \fB#include <fcntl.h>\fP              /* O_* 定数の定義の取得 */
52 \fB#include <unistd.h>\fP
53 .sp
54 \fBint pipe2(int \fP\fIpipefd\fP\fB[2], int \fP\fIflags\fP\fB);\fP
55 .fi
56 .SH 説明
57 \fBpipe\fP(2)  はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 \fIpipefd\fP
58 は、パイプの両端を参照する二つのファイルディスクリプタを 返すのに使用される。 \fIpipefd[0]\fP がパイプの読み出し側、
59 \fIpipefd[1]\fP がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、
60 パイプの読み出し側から読み出されるまでカーネルでバッファリングされる。 さらなる詳細は \fBpipe\fP(7)  を参照のこと。
61
62 \fBpipe2\fP()  は \fIflags\fP が 0 の場合には \fBpipe\fP()  と同じである。 \fIflags\fP に以下の値をビット毎の論理和
63 (OR) で指定することで、 異なる動作をさせることができる。
64 .TP  12
65 \fBO_NONBLOCK\fP
66 新しく生成される二つのオープンファイル記述 (open file description) の \fBO_NONBLOCK\fP
67 ファイルステータスフラグをセットする。 このフラグを使うことで、 \fBO_NONBLOCK\fP をセットするために \fBfcntl\fP(2)
68 を追加で呼び出す必要がなくなる。
69 .TP 
70 \fBO_CLOEXEC\fP
71 新しく生成される二つのファイルディスクリプタの close\-on\-exec (\fBFD_CLOEXEC\fP)  フラグをセットする。
72 このフラグが役に立つ理由については、 \fBopen\fP(2)  の \fBO_CLOEXEC\fP フラグの説明を参照のこと。
73 .SH 返り値
74 成功した場合は 0 が返される。エラーの場合は \-1 が返され、 \fIerrno\fP が適切に設定される。
75 .SH エラー
76 .TP 
77 \fBEFAULT\fP
78 \fIpipefd\fP が無効な値である。
79 .TP 
80 \fBEINVAL\fP
81 (\fBpipe2\fP())  \fIflags\fP に無効な値が入っている。
82 .TP 
83 \fBEMFILE\fP
84 このプロセスで使われているファイルディスクリプタが多すぎる。
85 .TP 
86 \fBENFILE\fP
87 オープンされているファイルの総数がシステムの制限に達している。
88 .SH バージョン
89 \fBpipe2\fP()  はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。
90 .SH 準拠
91 \fBpipe\fP(): POSIX.1\-2001.
92
93 \fBpipe2\fP()  は Linux 固有である。
94 .SH 例
95 .\" fork.2 refers to this example program.
96 以下のプログラムではパイプを生成し、その後 \fBfork\fP(2)  で子プロセスを生成する。
97 子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピーを 継承する。 \fBfork\fP(2)  の後、各プロセスはパイプ
98 (\fBpipe\fP(7)  を参照) に必要がなくなったディスクリプタをクローズする。 親プロセスはプログラムのコマンドライン引き数に含まれる
99 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
100 .nf
101
102 #include <sys/wait.h>
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <unistd.h>
106 #include <string.h>
107
108 int
109 main(int argc, char *argv[])
110 {
111     int pipefd[2];
112     pid_t cpid;
113     char buf;
114
115     if (argc != 2) {
116         fprintf(stderr, "Usage: %s <string>\en", argv[0]);
117         exit(EXIT_FAILURE);
118     }
119
120     if (pipe(pipefd) == \-1) {
121         perror("pipe");
122         exit(EXIT_FAILURE);
123     }
124
125     cpid = fork();
126     if (cpid == \-1) {
127         perror("fork");
128         exit(EXIT_FAILURE);
129     }
130
131     if (cpid == 0) {    /* 子プロセスがパイプから読み込む */
132         close(pipefd[1]);  /* 使用しない write 側はクローズする */
133
134         while (read(pipefd[0], &buf, 1) > 0)
135             write(STDOUT_FILENO, &buf, 1);
136
137         write(STDOUT_FILENO, "\en", 1);
138         close(pipefd[0]);
139         _exit(EXIT_SUCCESS);
140
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);                /* 子プロセスを待つ */
146         exit(EXIT_SUCCESS);
147     }
148 }
149 .fi
150 .SH 関連項目
151 \fBfork\fP(2), \fBread\fP(2), \fBsocketpair\fP(2), \fBwrite\fP(2), \fBpopen\fP(3),
152 \fBpipe\fP(7)