.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" Copyright (C) 2005, 2008, Michael Kerrisk .\" (A few fragments remain from an earlier (1992) version by .\" Drew Eckhardt .) .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified by Michael Haardt .\" Modified 1993-07-23 by Rik Faith .\" Modified 1996-10-22 by Eric S. Raymond .\" Modified 2004-06-17 by Michael Kerrisk .\" Modified 2005, mtk: added an example program .\" Modified 2008-01-09, mtk: rewrote DESCRIPTION; minor additions .\" to EXAMPLE text. .\" 2008-10-10, mtk: add description of pipe2() .\" .\" Japanese Version Copyright (c) 1997 SUTO, Mitsuaki .\" all rights reserved. .\" Translated Thu Jun 26 21:09:51 JST 1997 .\" by SUTO, Mitsuaki .\" Updated & Modified Thu Feb 10 00:47:11 JST 2005 .\" by Yuichi SATO .\" Updated & Modified Sat Dec 17 08:10:16 JST 2005 by Yuichi SATO .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13 .\" .TH PIPE 2 2010-09-10 "Linux" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O pipe, pipe2 \- create pipe pipe, pipe2 \- パイプを生成する .\"O .SH SYNOPSIS .SH 書式 .nf .B #include .sp .BI "int pipe(int " pipefd "[2]);" .sp .\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" .BR "#define _GNU_SOURCE" " /* feature_test_macros(7) 参照 */" .B #include .sp .BI "int pipe2(int " pipefd "[2], int " flags ); .fi .\"O .SH DESCRIPTION .SH 説明 .\"O .BR pipe () .\"O creates a pipe, a unidirectional data channel that .\"O can be used for interprocess communication. .\"O The array .\"O .IR pipefd .\"O is used to return two file descriptors referring to the ends of the pipe. .\"O .IR pipefd[0] .\"O refers to the read end of the pipe. .\"O .IR pipefd[1] .\"O refers to the write end of the pipe. .\"O Data written to the write end of the pipe is buffered by the kernel .\"O until it is read from the read end of the pipe. .\"O For further details, see .\"O .BR pipe (7). .BR pipe (2) はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 .I pipefd は、パイプの両端を参照する二つのファイルディスクリプタを 返すのに使用される。 .I pipefd[0] がパイプの読み出し側、 .I pipefd[1] がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、 パイプの読み出し側から読み出されるまでカーネルでバッファリングされる。 さらなる詳細は .BR pipe (7) を参照のこと。 .\"O If .\"O .IR flags .\"O is 0, then .\"O .BR pipe2 () .\"O is the same as .\"O .BR pipe (). .\"O The following values can be bitwise ORed in .\"O .IR flags .\"O to obtain different behavior: .BR pipe2 () は .I flags が 0 の場合には .BR pipe () と同じである。 .I flags に以下の値をビット毎の論理和 (OR) で指定することで、 異なる動作をさせることができる。 .TP 12 .B O_NONBLOCK .\"O Set the .\"O .BR O_NONBLOCK .\"O file status flag on the two new open file descriptions. .\"O Using this flag saves extra calls to .\"O .BR fcntl (2) .\"O to achieve the same result. 新しく生成される二つのオープンファイル記述 (open file description) の .B O_NONBLOCK ファイルステータスフラグをセットする。 このフラグを使うことで、 .B O_NONBLOCK をセットするために .BR fcntl (2) を追加で呼び出す必要がなくなる。 .TP .B O_CLOEXEC .\"O Set the close-on-exec .\"O .RB ( FD_CLOEXEC ) .\"O flag on the two new file descriptors. .\"O See the description of the same flag in .\"O .BR open (2) .\"O for reasons why this may be useful. 新しく生成される二つのファイルディスクリプタの close-on-exec .RB ( FD_CLOEXEC ) フラグをセットする。 このフラグが役に立つ理由については、 .BR open (2) の .B O_CLOEXEC フラグの説明を参照のこと。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O On success, zero is returned. .\"O On error, \-1 is returned, and .\"O .I errno .\"O is set appropriately. 成功した場合 0 が返る。失敗した場合 \-1 が返り、 .I errno がエラーの内容に従って設定される。 .\"O .SH ERRORS .SH エラー .TP .B EFAULT .\"O .I pipefd .\"O is not valid. .I pipefd が無効な値である。 .TP .B EINVAL .\"O .RB ( pipe2 ()) .\"O Invalid value in .\"O .IR flags . .RB ( pipe2 ()) .I flags に無効な値が入っている。 .TP .B EMFILE .\"O Too many file descriptors are in use by the process. このプロセスで使われているファイルディスクリプタが多すぎる。 .TP .B ENFILE .\"O The system limit on the total number of open files has been reached. オープンされているファイルの総数がシステムの制限に達した。 .\"O .SH VERSIONS .SH バージョン .\"O .BR pipe2 () .\"O was added to Linux in version 2.6.27; .\"O glibc support is available starting with .\"O version 2.9. .BR pipe2 () はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。 .\"O .SH "CONFORMING TO" .SH 準拠 .BR pipe (): POSIX.1-2001. .\"O .BR pipe2 () .\"O is Linux-specific. .BR pipe2 () は Linux 固有である。 .\"O .SH EXAMPLE .SH 例 .\"O .\" fork.2 refers to this example program. .\" fork.2 はこの例のプログラムを参照している。 .\"O The following program creates a pipe, and then .\"O .BR fork (2)s .\"O to create a child process; .\"O the child inherits a duplicate set of file .\"O descriptors that refer to the same pipe. 以下のプログラムではパイプを生成し、その後 .BR fork (2) で子プロセスを生成する。 子プロセスは同じパイプを参照するファイルディスクリプタ集合のコピーを 継承する。 .\"O After the .\"O .BR fork (2), .\"O each process closes the descriptors that it doesn't need for the pipe .\"O (see .\"O .BR pipe (7)). .BR fork (2) の後、各プロセスはパイプ .RB ( pipe (7) を参照) に必要がなくなったディスクリプタをクローズする。 .\"O The parent then writes the string contained in the program's .\"O command-line argument to the pipe, .\"O and the child reads this string a byte at a time from the pipe .\"O and echoes it on standard output. 親プロセスはプログラムのコマンドライン引き数に含まれる 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。 .nf #include #include #include #include #include int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s \\n", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == \-1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == \-1) { perror("fork"); exit(EXIT_FAILURE); } .\"O if (cpid == 0) { /* Child reads from pipe */ if (cpid == 0) { /* 子プロセスがパイプから読み込む */ .\"O close(pipefd[1]); /* Close unused write end */ 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); .\"O } else { /* Parent writes argv[1] to pipe */ .\"O close(pipefd[0]); /* Close unused read end */ .\"O write(pipefd[1], argv[1], strlen(argv[1])); .\"O close(pipefd[1]); /* Reader will see EOF */ .\"O wait(NULL); /* Wait for child */ } else { /* 親プロセスは argv[1] をパイプへ書き込む */ close(pipefd[0]); /* 使用しない read 側はクローズする */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* 読み込み側が EOF に出会う */ wait(NULL); /* 子プロセスを待つ */ exit(EXIT_SUCCESS); } } .fi .\"O .SH "SEE ALSO" .SH 関連項目 .BR fork (2), .BR read (2), .BR socketpair (2), .BR write (2), .BR popen (3), .BR pipe (7)