pipe

PIPE(2)                    Linux Programmer's Manual                   PIPE(2)



åå
       pipe, pipe2 - ãã¤ããçæãã

æ¸å¼
       #include <unistd.h>

       int pipe(int pipefd[2]);

       #define _GNU_SOURCE             /* feature_test_macros(7) åç§ */
       #include <fcntl.h>              /* O_* å®æ°ã®å®ç¾©ã®åå¾ */
       #include <unistd.h>

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

説æ
       pipe(2)  ã¯ãã¤ããçæããã ãã¤ãã¯ããã‐
       ã»ã¹ééä¿¡ã«ä½¿ç¨ã§ããåæ¹åã®ãã¼ã¿ãã£ãã«ã§ããã éå pipefd
       ã¯ããã¤ãã®ä¸¡ç«¯ãåç§ããäºã¤ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã è¿ãã®ã«ä½¿ç¨ãããã
       pipefd[0] ããã¤ãã®èªã¿åºãå´ã pipefd[1] ããã¤ãã®æ¸ãè¾¼ã¿å´ã§ããã
       ãã¤ãã®æ¸ãè¾¼ã¿å´ã«æ¸ãè¾¼ã¾ãããã¼ã¿ã¯ã ãã¤ãã®èªã¿åºãå´ããèª‐
       ã¿åºãããã¾ã§ã«ã¼ãã«ã§ãããã¡ãªã³ã°ãããã ãããªã詳細㯠pipe(7)  ãåç§ã®ãã¨ã

       pipe2()  㯠flags ã 0 ã®å ´åã«ã¯ pipe()  ã¨åãã§ããã flags
       ã«ä»¥ä¸ã®å¤ããããæ¯ã®è«çå (OR) ã§æå®ãããã¨ã§ã ç°ãªãåä½ãããããã¨ãã§ããã

       O_NONBLOCK  æ°ããçæãããäºã¤ã®ãªã¼ãã³ãã¡ã¤ã«è¨è¿° (open file description)
                   ã® O_NONBLOCK ãã¡ã¤ã«ã¹ãã¼ã¿ã¹ãã©ã°ãã»ããããã
                   ãã®ãã©ã°ã使ããã¨ã§ã O_NONBLOCK ãã»ããããããã« fcntl(2)
                   ã追å ã§å¼ã³åºãå¿è¦ããªããªãã

       O_CLOEXEC   æ°ããçæãããäºã¤ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã® close-on-exec
                   (FD_CLOEXEC)  ãã©ã°ãã»ããããã ãã®ãã©ã°ãå½¹ã«ç«ã¤çç±ã«ã¤ãã¦ã¯ã
                   open(2)  ã® O_CLOEXEC ãã©ã°ã®èª¬æãåç§ã®ãã¨ã

è¿ãå¤
       æåããå ´å㯠0 ãè¿ããããã¨ã©ã¼ã®å ´å㯠-1 ãè¿ããã errno ãé©åã«è¨å®ãããã

ã¨ã©ã¼
       EFAULT pipefd ãç¡å¹ãªå¤ã§ããã

       EINVAL (pipe2())  flags ã«ç¡å¹ãªå¤ãå¥ã£ã¦ããã

       EMFILE ãã®ããã»ã¹ã§ä½¿ããã¦ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãå¤ãããã

       ENFILE ãªã¼ãã³ããã¦ãããã¡ã¤ã«ã®ç·æ°ãã·ã¹ãã ã®å¶éã«éãã¦ããã

ãã¼ã¸ã§ã³
       pipe2()  ã¯ãã¼ã¸ã§ã³ 2.6.27 㧠Linux ã«è¿½å ãããã glibc
       ã«ãããµãã¼ãã¯ãã¼ã¸ã§ã³ 2.9 以éã§å©ç¨ã§ããã

æºæ
       pipe(): POSIX.1-2001.

       pipe2()  㯠Linux åºæã§ããã

ä¾
       以ä¸ã®ããã°ã©ã ã§ã¯ãã¤ããçæãããã®å¾ fork(2)  ã§åããã»ã¹ãçæããã åãã‐
       ã»ã¹ã¯åããã¤ããåç§ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿éåã®ã³ãã¼ã ç¶æ¿ããã fork(2)
       ã®å¾ãåããã»ã¹ã¯ãã¤ã (pipe(7)  ãåç§) ã«å¿è¦ããªããªã£ããã£ã¹ã¯ãªãã¿ãã¯ã‐
       ã¼ãºããã 親ããã»ã¹ã¯ããã°ã©ã ã®ã³ãã³ãã©ã¤ã³å¼ãæ°ã«å«ã¾ãã æå‐
       åããã¤ãã¸æ¸ãè¾¼ã¿ã åããã»ã¹ã¯ãã®æååããã¤ããã 1 ãã¤ããã¤èª‐
       ã¿è¾¼ãã§æ¨æºåºåã«ã¨ã³ã¼ããã

       #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);
           }
       }

é¢é£é ç®
       fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2012-02-14                           PIPE(2)