.\" Copyright (C) 2005, 2008, Michael Kerrisk .\" (A few fragments remain from an earlier (1992) version by .\" Drew Eckhardt .) .\" .\" %%%LICENSE_START(VERBATIM) .\" 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. .\" %%%LICENSE_END .\" .\" 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() .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" 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 .\" Updated 2012-04-30, Akihiro MOTOKI .\" .TH PIPE 2 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 pipe, pipe2 \- パイプを生成する .SH 書式 .nf \fB#include \fP .PP /* On Alpha, IA\-64, MIPS, SuperH, and SPARC/SPARC64; see NOTES */ \fBstruct fd_pair {\fP \fBlong fd[2];\fP \fB};\fP \fBstruct fd_pair pipe();\fP .PP /* On all other architectures */ \fBint pipe(int \fP\fIpipefd\fP\fB[2]);\fP \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP /* O_* 定数の定義の取得 */ \fB#include \fP .PP \fBint pipe2(int \fP\fIpipefd\fP\fB[2], int \fP\fIflags\fP\fB);\fP .fi .SH 説明 \fBpipe\fP() はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 \fIpipefd\fP は、パイプの両端を参照する二つのファイルディスクリプターを 返すのに使用される。 \fIpipefd[0]\fP がパイプの読み出し側、 \fIpipefd[1]\fP がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、 パイプの読み出し側から読み出されるまでカーネルでバッファーリングされる。 さらなる詳細は \fBpipe\fP(7) を参照のこと。 .PP \fBpipe2\fP() は \fIflags\fP が 0 の場合には \fBpipe\fP() と同じである。 \fIflags\fP に以下の値をビット毎の論理和 (OR) で指定することで、 異なる動作をさせることができる。 .TP \fBO_CLOEXEC\fP 新しく生成される二つのファイルディスクリプターの close\-on\-exec (\fBFD_CLOEXEC\fP) フラグをセットする。 このフラグが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの説明を参照のこと。 .TP \fBO_DIRECT\fP (Linux 3.4 以降) .\" commit 9883035ae7edef3ec62ad215611cb8e17d6a1a5d 「パケット」モードで入出力を行うパイプを作成する。 このパイプへの \fBwrite\fP(2) それぞれが別のパケットとして扱われ、 このパイプからの \fBread\fP(2) では一度に一つパケットが読み出される。 以下の点に注意すること。 .RS .IP * 3 \fBPIPE_BUF\fP バイト (\fBpipe\fP(7) 参照) より大きいデータを書き込んだ場合、複数のパケットに分割される。 定数 \fBPIPE_BUF\fP は \fI\fP で定義されている。 .IP * \fBread\fP(2) で次のパケットよりも小さなバッファーサイズを指定した場合、要求されたバイト数のデータが読み出され、そのパケットの超過分のバイトは破棄される。 可能性のある最大サイズのパケットを読み出すには、\fBPIPE_BUF\fP のバッファーサイズを指定すれば十分である (上の項目を参照)。 .IP * 長さ 0 のパケットはサポートされていない。 (バッファーサイズ 0 を指定した \fBread\fP(2) は何も行わず 0 を返す)。 .RE .IP このフラグをサポートしていない古いカーネルでは、エラー \fBEINVAL\fP が返る。これによりカーネルがサポートしていないことが分かる。 .IP .\" commit 0dbf5f20652108106cb822ad7662c786baaa03ff .\" FIXME . But, it is not possible to specify O_DIRECT when opening a FIFO Since Linux 4.5, it is possible to change the \fBO_DIRECT\fP setting of a pipe file descriptor using \fBfcntl\fP(2). .TP \fBO_NONBLOCK\fP Set the \fBO_NONBLOCK\fP file status flag on the open file descriptions referred to by the new file descriptors. Using this flag saves extra calls to \fBfcntl\fP(2) to achieve the same result. .SH 返り値 On success, zero is returned. On error, \-1 is returned, \fIerrno\fP is set appropriately, and \fIpipefd\fP is left unchanged. .PP .\" http://austingroupbugs.net/view.php?id=467 On Linux (and other systems), \fBpipe\fP() does not modify \fIpipefd\fP on failure. A requirement standardizing this behavior was added in POSIX.1\-2008 TC2. The Linux\-specific \fBpipe2\fP() system call likewise does not modify \fIpipefd\fP on failure. .SH エラー .TP \fBEFAULT\fP \fIpipefd\fP が無効な値である。 .TP \fBEINVAL\fP (\fBpipe2\fP()) \fIflags\fP に無効な値が入っている。 .TP \fBEMFILE\fP The per\-process limit on the number of open file descriptors has been reached. .TP \fBENFILE\fP オープンされているファイルの総数がシステム全体の制限に達している。 .TP \fBENFILE\fP The user hard limit on memory that can be allocated for pipes has been reached and the caller is not privileged; see \fBpipe\fP(7). .SH バージョン \fBpipe2\fP() はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。 .SH 準拠 \fBpipe\fP(): POSIX.1\-2001, POSIX.1\-2008. .PP \fBpipe2\fP() は Linux 固有である。 .SH 注意 .\" See http://math-atlas.sourceforge.net/devel/assembly/64.psabi.1.33.ps.Z .\" for example, section 3.2.1 "Registers and the Stack Frame". The System V ABI on some architectures allows the use of more than one register for returning multiple values; several architectures (namely, Alpha, IA\-64, MIPS, SuperH, and SPARC/SPARC64) (ab)use this feature in order to implement the \fBpipe\fP() system call in a functional manner: the call doesn't take any arguments and returns a pair of file descriptors as the return value on success. The glibc \fBpipe\fP() wrapper function transparently deals with this. See \fBsyscall\fP(2) for information regarding registers used for storing second file descriptor. .SH 例 .\" fork.2 refers to this example program. 以下のプログラムではパイプを生成し、その後 \fBfork\fP(2) で子プロセスを生成する。 子プロセスは同じパイプを参照するファイルディスクリプター集合のコピーを 継承する。 \fBfork\fP(2) の後、各プロセスはパイプ (\fBpipe\fP(7) を参照) に必要がなくなったファイルディスクリプターをクローズする。 親プロセスはプログラムのコマンドライン引数に含まれる 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。 .SS プログラムのソース .EX #include #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 \en", argv[0]); exit(EXIT_FAILURE); } 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, "\en", 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); } } .EE .SH 関連項目 \fBfork\fP(2), \fBread\fP(2), \fBsocketpair\fP(2), \fBsplice\fP(2), \fBtee\fP(2), \fBvmsplice\fP(2), \fBwrite\fP(2), \fBpopen\fP(3), \fBpipe\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。