Unix-system-calls-pipe

提供:Dev Guides
移動先:案内検索

[top]#

|http://www.google.com/[Google] | a|

Web This Site
  • 初心者向けのUnix *
  • 高度なUnix *

選択した読書

Copyright©2014 by finddevguides

[cols=",,,,,,,",]

| |  Home   | |  References   | |  Discussion Forums   | |  About TP  

[width="100%",cols="100%",]

a| == pipe()-Unix、Linuxシステムコール

[[File:]] image :http://www.finddevguides.com/images/next.gif [next] image:http://www.finddevguides.com/add- this.gif [AddThisソーシャルブックマークボタン]

広告

NAME

パイプ-パイプを作成する

概要

  • #include <unistd.h> *
*int pipe(int* __filedes __ * *[2]);* *

説明

*pipe* ()は、パイプiノードを指すファイル記述子のペアを作成し、_filedes_が指す配列に配置します。 _filedes [0] _は読み取り用、_filedes [1] _は書き込み用です。

返り値

成功すると、ゼロが返されます。 エラーの場合、-1が返され、_errno_が適切に設定されます。

エラー

Tag Description
EFAULT filedes is not valid.
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.

準拠

POSIX.1-2001。

次のプログラムはパイプを作成し、次に fork (2)sを使用して子プロセスを作成します。 fork (2)の後、各プロセスはパイプに必要のない記述子を閉じます( pipe (7)を参照)。 次に、親はプログラムのコマンドライン引数に含まれる文字列をパイプに書き込み、子はこの文字列をパイプから一度に1バイトずつ読み取り、標準出力にエコーします。

#include <sys/wait.h> #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pfd[2]; pid_t cpid; char buf; assert(argc == 2); if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /*Child reads from pipe*/ close(pfd[1]); /*Close unused write end*/ while (read(pfd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, "\n", 1); close(pfd[0]); _exit(EXIT_SUCCESS); } else { /*Parent writes argv[1] to pipe*/ close(pfd[0]); /*Close unused read end*/ write(pfd[1], argv[1], strlen(argv[1])); close(pfd[1]); /*Reader will see EOF*/ wait(NULL); /*Wait for child*/ exit(EXIT_SUCCESS); } }

関連項目

[[File:]] image :http://www.finddevguides.com/images/next.gif [next] [[File:]]

広告

|  

[cols="^",]

|Advertisements