pipeのヘルプ・マニュアル
日本語 英語
pipe --help
man pipe
PIPE(2) Linux Programmer’s Manual PIPE(2)
名前
pipe, pipe2 - パイプを生成する
書式
#include
int pipe(int pipefd[2]);
#define _GNU_SOURCE
#include
int pipe2(int pipefd[2], int flags);
説明
pipe(2) はパイプを生成する。パイプは、プロセス間通信に使用できる単方向
のデータチャネルである。配列 pipefd は、パイプの両端を参照する二つの フ
ァイルディスクリプタを返すのに使用される。 pipefd[0] がパイプの読み出し
側、 pipefd[1] がパイプの書き込み側である。パイプの書き込み側に書き込ま
れ たデータは、パイプの読み出し側から読み出されるまでカーネルでバッファ
リングされる。さらなる詳細は pipe(7) を参照のこと。
pipe2() は flags が 0 の場合には pipe() と同じである。 flags に以下の値
を ビット毎の論理和 (OR) で指定することで、異なる動作をさせることができ
る。
O_NONBLOCK 新しく生成される二つのオープンファ イ ル 記 述 (open file
description) の O_NONBLOCK ファイルステータスフラグをセット
する。このフラグを使うことで、 O_NONBLOCK をセットするた め
に fcntl(2) を追加で呼び出す必要がなくなる。
O_CLOEXEC 新 し く生成される二つのファイルディスクリプタの close-on-
exec (FD_CLOEXEC) フラグをセットする。このフラグが役に立 つ
理 由については、 open(2) の O_CLOEXEC フラグの説明を参照の
こと。
返り値
成功した場合 0 が返る。失敗した場合 -1 が返り、 errno がエラーの内容 に
従って設定される。
エラー
EFAULT pipefd が無効な値である。
EINVAL (pipe2()) flags に無効な値が入っている。
EMFILE このプロセスで使われているファイルディスクリプタが多すぎる。
ENFILE オープンされているファイルの総数がシステムの制限に達した。
バージョン
pipe2() はバージョン 2.6.27 で Linux に追加された。 glibc によるサポー
トはバージョン 2.9 以降で利用できる。
準拠
pipe(): POSIX.1-2001.
pipe2() は Linux 固有である。
例
以下のプログラムではパイプを生成し、その後 fork(2) で子プロセスを生成す
る 。子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピー
を継承する。 fork(2) の後、各プロセスはパイプ (pipe(7) を参照) に必要が
な くなったディスクリプタをクローズする。親プロセスはプログラムのコマン
ドライン引き数に含まれる文字列をパイプへ書き込み、子プロセスはこの文 字
列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。
#include
#include
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char buf;
assert(argc == 2);
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* 子プロセスがパイプから読み込む */
close(pipefd[1]); /* 使用しない write 側はクローズする */
while (read(pipefd[0], &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]);
_exit(EXIT_SUCCESS);
} else { /* 親プロセスは argv[1] をパイプへ書き込む */
close(pipefd[0]); /* 使用しない read 側はクローズする */
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]); /* 読み込み側が EOF に出会う */
wait(NULL); /* 子プロセスを待つ */
exit(EXIT_SUCCESS);
}
}
関連項目
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7)
Linux 2008-11-04 PIPE(2)
PIPE(2) Linux Programmer’s Manual PIPE(2)
NAME
pipe, pipe2 - create pipe
SYNOPSIS
#include
int pipe(int pipefd[2]);
#define _GNU_SOURCE
#include
int pipe2(int pipefd[2], int flags);
DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used
for interprocess communication. The array pipefd is used to return two
file descriptors referring to the ends of the pipe. pipefd[0] refers
to the read end of the pipe. pipefd[1] refers to the write end of the
pipe. Data written to the write end of the pipe is buffered by the
kernel until it is read from the read end of the pipe. For further
details, see pipe(7).
If flags is 0, then pipe2() is the same as pipe(). The following val-
ues can be bitwise ORed in flags to obtain different behavior:
O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open
file descriptions. Using this flag saves extra calls to
fcntl(2) to achieve the same result.
O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file
descriptors. See the description of the same flag in
open(2) for reasons why this may be useful.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
ERRORS
EFAULT pipefd is not valid.
EINVAL (pipe2()) Invalid value in flags.
EMFILE Too many file descriptors are in use by the process.
ENFILE The system limit on the total number of open files has been
reached.
VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is avail-
able starting with version 2.9.
CONFORMING TO
pipe(): POSIX.1-2001.
pipe2() is Linux-specific.
EXAMPLE
The following program creates a pipe, and then fork(2)s to create a
child process; the child inherits a duplicate set of file descriptors
that refer to the same pipe. After the fork(2), each process closes
the descriptors that it doesn’t need for the pipe (see pipe(7)). The
parent then writes the string contained in the program’s command-line
argument to the pipe, and the child reads this string a byte at a time
from the pipe and echoes it on standard output.
#include
#include
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char buf;
assert(argc == 2);
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child reads from pipe */
close(pipefd[1]); /* Close unused write end */
while (read(pipefd[0], &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]);
_exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pipefd[0]); /* Close unused read end */
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}
SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-11-04 PIPE(2)