pipe

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

pipe, pipe2 - パイプを生成する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>

int pipe(int pipefd[2]);

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Definition of O_* constants */
#include <unistd.h>

int pipe2(int pipefd[2], int flags);

/* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64, pipe() has the
   following prototype; see VERSIONS */

#include <unistd.h>

struct fd_pair {
long fd[2];
};
struct fd_pair pipe(void);
 

説明

pipe() はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 pipefd は、パイプの両端を参照する二つのファイルディスクリプターを 返すのに使用される。 pipefd[0] がパイプの読み出し側、 pipefd[1] がパイプの書き込み側である。 パイプの書き込み側に書き込まれたデータは、 パイプの読み出し側から読み出されるまでカーネルでバッファーリングされる。 さらなる詳細は pipe(7) を参照のこと。

pipe2() は flags が 0 の場合には pipe() と同じである。 flags に以下の値をビット毎の論理和 (OR) で指定することで、 異なる動作をさせることができる。

O_CLOEXEC
新しく生成される二つのファイルディスクリプターの close-on-exec (FD_CLOEXEC) フラグをセットする。 このフラグが役に立つ理由については、 open(2) の O_CLOEXEC フラグの説明を参照のこと。
O_DIRECT (Linux 3.4 以降)
「パケット」モードで入出力を行うパイプを作成する。 このパイプへの write(2) それぞれが別のパケットとして扱われ、 このパイプからの read(2) では一度に一つパケットが読み出される。 以下の点に注意すること。
•
PIPE_BUF バイト (pipe(7) 参照) より大きいデータを書き込んだ場合、複数のパケットに分割される。 定数 PIPE_BUF は <limits.h> で定義されている。
•
read(2) で次のパケットよりも小さなバッファーサイズを指定した場合、要求されたバイト数のデータが読み出され、そのパケットの超過分のバイトは破棄される。 可能性のある最大サイズのパケットを読み出すには、PIPE_BUF のバッファーサイズを指定すれば十分である (上の項目を参照)。
•
長さ 0 のパケットはサポートされていない。 (バッファーサイズ 0 を指定した read(2) は何も行わず 0 を返す)。
このフラグをサポートしていない古いカーネルでは、エラー EINVAL が返る。これによりカーネルがサポートしていないことが分かる。
Since Linux 4.5, it is possible to change the O_DIRECT setting of a pipe file descriptor using fcntl(2).
O_NONBLOCK
Set the O_NONBLOCK file status flag on the open file descriptions referred to by the new file descriptors. Using this flag saves extra calls to fcntl(2) to achieve the same result.
O_NOTIFICATION_PIPE
Since Linux 5.8, general notification mechanism is built on the top of the pipe where kernel splices notification messages into pipes opened by user space. The owner of the pipe has to tell the kernel which sources of events to watch and filters can also be applied to select which subevents should be placed into the pipe.
 

返り値

On success, zero is returned. On error, -1 is returned, errno is set to indicate the error, and pipefd is left unchanged.

On Linux (and other systems), pipe() does not modify pipefd on failure. A requirement standardizing this behavior was added in POSIX.1-2008 TC2. The Linux-specific pipe2() system call likewise does not modify pipefd on failure.  

エラー

EFAULT
pipefd が無効な値である。
EINVAL
(pipe2()) flags に無効な値が入っている。
EMFILE
The per-process limit on the number of open file descriptors has been reached.
ENFILE
オープンされているファイルの総数がシステム全体の制限に達している。
ENFILE
The user hard limit on memory that can be allocated for pipes has been reached and the caller is not privileged; see pipe(7).
ENOPKG
(pipe2()) O_NOTIFICATION_PIPE was passed in flags and support for notifications (CONFIG_WATCH_QUEUE) is not compiled into the kernel.
 

バージョン

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 pipe() 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 pipe() wrapper function transparently deals with this. See syscall(2) for information regarding registers used for storing second file descriptor.  

標準規格

POSIX.1-2024.  

歴史

pipe()
POSIX.1-2001.
pipe2()
POSIX.1-2024. Linux 2.6.27, glibc 2.9.
 

例

以下のプログラムではパイプを生成し、その後 fork(2) で子プロセスを生成する。 子プロセスは同じパイプを参照するファイルディスクリプター集合のコピーを 継承する。 fork(2) の後、各プロセスはパイプ (pipe(7) を参照) に必要がなくなったファイルディスクリプターをクローズする。 親プロセスはプログラムのコマンドライン引数に含まれる 文字列をパイプへ書き込み、 子プロセスはこの文字列をパイプから 1 バイトずつ読み込んで標準出力にエコーする。  

プログラムのソース

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    int    pipefd[2];
    char   buf;
    pid_t  cpid;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <string>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (pipe(pipefd) == -1)
        err(EXIT_FAILURE, "pipe");

    cpid = fork();
    if (cpid == -1)
        err(EXIT_FAILURE, "fork");

    if (cpid == 0) {    /* Child reads from pipe */
        if (close(pipefd[1]) == -1)  /* Close unused write end */
            err(EXIT_FAILURE, "close");

        while (read(pipefd[0], &buf, 1) > 0) {
            if (write(STDOUT_FILENO, &buf, 1) != 1)
                err(EXIT_FAILURE, "write");
        }

        if (write(STDOUT_FILENO, "\n", 1) != 1)
            err(EXIT_FAILURE, "write");
        if (close(pipefd[0]) == -1)
            err(EXIT_FAILURE, "close");
        _exit(EXIT_SUCCESS);

    } else {            /* Parent writes argv[1] to pipe */
        if (close(pipefd[0]) == -1)  /* Close unused read end */
            err(EXIT_FAILURE, "close");
        if (write(pipefd[1], argv[1], strlen(argv[1])) != strlen(argv[1]))
            err(EXIT_FAILURE, "write");
        if (close(pipefd[1]) == -1)  /* Reader will see EOF */
            err(EXIT_FAILURE, "close");
        if (wait(NULL) == -1)        /* Wait for child */
            err(EXIT_FAILURE, "wait");
        exit(EXIT_SUCCESS);
    }
}
 

関連項目


 fork(2), read(2), socketpair(2), splice(2), tee(2), vmsplice(2), write(2), popen(3), pipe(7)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
バージョン
標準規格
歴史
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.