#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE #include <unistd.h> int pipe2(int pipefd[2], int flags);
pipe2() は flags が 0 の場合には pipe() と同じである。 flags に以下の値をビット毎の論理和 (OR) で指定することで、 異なる動作をさせることができる。
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char buf;
if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\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);
}
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);
}
}