pipe

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



åå
       pipe - ãã¤ã㨠FIFO ã®æ¦è¦

説æ
       ãã¤ã㨠FIFO (ååä»ããã¤ãã¨ããã) ã¯ã åæ¹åã®ããã»ã¹ééä¿¡ãã£ãã«ãæä¾ããã
       ãã¤ãã«ã¯ãèªã¿åºãå´ãã¨ãæ¸ãè¾¼ã¿å´ããããã
       ãã¤ãã®æ¸ãè¾¼ã¿å´ã§æ¸ãè¾¼ã¾ãããã¼ã¿ã¯ã ãã¤ãã®èªã¿åºãå´ããèªã¿åºããã¨ãã§ããã

       ãã¤ããä½æããã«ã¯ pipe(2)  ã使ç¨ããã pipe(2)
       ã¯æ°ãããã¤ããä½æãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ãäºã¤è¿ãã
       ãã£ã¹ã¯ãªãã¿ã®ãã¡ãä¸æ¹ã¯ãã¤ãã®èªã¿åºãå´ããããä¸æ¹ã¯ æ¸ãè¾¼ã¿å´ãåç§ãã¦ããã
       ãã¤ãã¯é¢é£ããããã»ã¹éã®éä¿¡ãä½æããã®ã«ä½¿ç¨ã§ããã ä¾ã¯ pipe(2)  ãåç§ã

       FIFO (First In First Out ã®çç¥) ã¯ãã¡ã¤ã«ã·ã¹ãã ã§ã®ååãæã¡ã open(2)
       ã使ã£ã¦ãªã¼ãã³ã§ãã (mkfifo(3)  ã使ã£ã¦ä½æããã)ã ã©ããªãã‐
       ã»ã¹ã§ãããã¡ã¤ã«ã®ã¢ã¯ã»ã¹è¨±å¯ãããã° FIFO ããªã¼ãã³ãã ãã¨ãã§ããã èª‐
       ã¿åºãå´ããªã¼ãã³ããã«ã¯ O_RDONLY ãã©ã°ã使ç¨ããæ¸ãè¾¼ã¿å´ããªã¼ãã³ããã«ã¯
       O_WRONLY ãã©ã°ã使ç¨ããã詳細㯠fifo(7)  ãåç§ã ã注æã FIFO
       ã¯ãã¡ã¤ã«ã·ã¹ãã åã®ãã¹åãæã¤ãã FIFO
       ã«å¯¾ãã¦å¥åºåãè¡ã£ã¦ãã(ãã¡ã¤ã«ã·ã¹ãã ãåå¨ããããã¤ã¹ã ãã£ãã¨ãã¦ã)
       ãã®ããã¤ã¹ã«å¯¾ããæä½ã¯çºçããªãã

   ãã¤ãã FIFO ã«å¯¾ããå¥åºå
       ãã¤ã㨠FIFO ã®éãã¯ä½æããªã¼ãã³ã®æ¹æ³ã ãã§ããã ãããã®æä½ãå®äºããå¾ã¯ããã¤ãã¨
       FIFO ã«å¯¾ããå¥åºå㯠å¨ãåãä»çµã¿ã§è¡ãããã

       ããã»ã¹ã空ã®ãã¤ãããèªã¿åºããè¡ããã¨ããå ´åã read(2)  ã¯ãã¼ã¿ãèª‐
       ã¿åºãå¯è½ã«ãªãã¾ã§åæ¢ããã ããã»ã¹ããã«ç¶æã®ãã¤ãã«æ¸ãè¾¼ã¿ãè¡ããã¨ããå ´åã
       write(2)  ã¯æ¸ãè¾¼ã¿ãå®äºããã®ã«ååãªéã®ãã¤ãããã®èªã¿åºãã è¡ãããã¾ã§åæ¢ããã
       éåæ¢ (nonblocking) I/O ã使ããã¨ãã§ããã éåæ¢ I/O ã使ãã«ã¯ã fcntl(2)
       F_SETFL æä½ã使ã£ã¦ã O_NONBLOCK ãªã¼ãã³ãã¡ã¤ã«ç¶æãã©ã°ãæå¹ã«ããã

       ãã¤ãã«ããæä¾ãããéä¿¡ãã£ãã«ã¯ããã¤ãã¹ããªã¼ã ãã§ããã
       ã¡ãã»ã¼ã¸å¢çã®æ¦å¿µã¯ãªãã

       ãã¤ãã®æ¸ãè¾¼ã¿å´ãåç§ãã¦ãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã ãã¹ã¦ã¯ã‐
       ã¼ãºãããå¾ã§ããã®ãã¤ããã read(2)  ãè¡ããã¨ããå ´åã end-of-file
       (ãã¡ã¤ã«æ«å°¾) ãè¦ãã (read(2)  㯠0 ãè¿ã)ã ãã¤ãã®èª‐
       ã¿åºãå´ãåç§ãã¦ãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã ãã¹ã¦ã¯ãã¼ãºãããå¾ã§ã write(2)
       ãè¡ãã¨ãå¼ã³åºãåããã»ã¹ã« SIGPIPE ã·ã°ãã«ãéãããã å¼ã³åºãåãã‐
       ã»ã¹ããã®ã·ã°ãã«ãç¡è¦ãã¦ããã¨ãã«ã¯ã write(2)  ã¯ã¨ã©ã¼ EPIPE ã§å¤±æããã
       pipe(2)  㨠fork(2)  ã使ç¨ããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ã close(2)
       ãé©åã«ä½¿ã£ã¦ä¸å¿è¦ãªãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã®è¤è£½ã ã¯ã‐
       ã¼ãºãã¹ãã§ããããããããã¨ã§ãå¿è¦ãªæã«ç¢ºå®ã« end-of-file ã SIGPIPE/EPIPE
       ãééãããããã«ãªãã

       ãã¤ãã«ã¯ lseek(2)  ãè¡ããã¨ã¯ã§ããªãã

   ãã¤ãã®å®¹é
       ãã¤ãã®å®¹éã«ã¯ä¸éãããã ãã¤ãããã«ã®å ´åã write(2)  ã¯åæ‐
       ¢ããã失æããããããã©ã¡ãã«ãªãã㯠O_NONBLOCK ãã©ã°ãã»ããããã¦ãããã©ããã«ä¾åãã
       (ä¸è¨åç§)ã å®è£ã«ããããã¤ãã®å®¹éã®ä¸éã¯ç°ãªãã
       ã¢ããªã±ã¼ã·ã§ã³ã¯ç¹å®ã®å®¹éãåæã«ãã¹ãã§ã¯ãªãã æ¸ãè¾¼ã¿å´ã®ããã»ã¹ãåæ‐
       ¢ããã¾ã¾ã«ãªããªãããã èªã¿åºãå´ã®ããã»ã¹ã¯ãã¼ã¿ãå©ç¨å¯è½ã«ãªã£ããã§ããã ãããã«
       èªã¿åºããè¡ãããã«ãã¢ããªã±ã¼ã·ã§ã³ãè¨è¨ãã¹ãã§ããã

       ãã¼ã¸ã§ã³ 2.6.11 ããåã® Linux ã§ã¯ãã¤ãã®å®¹éã¯ã·ã¹ãã ã®ãã¼ã¸ãµã¤ãº
       ã¨åãã§ãã£ã (ä¾ãã° i386 ã§ã¯ 4096 ãã¤ã)ã Linux 2.6.11
       以éã§ã¯ããã¤ãã®å®¹é㯠65536 ãã¤ãã§ããã

   PIPE_BUF
       POSIX.1-2001 ã§ã¯ã PIPE_BUF ãã¤ã以ä¸ã® write(2)  㯠atomic
       ã«è¡ããããã¨ãã¤ã¾ããã¤ãã¸ã®åºåãã¼ã¿ã®æ¸ãè¾¼ã¿ã¯
       é£ç¶ããã·ã¼ã±ã³ã¹ã¨ãã¦è¡ããããã¨ãå¿é ã¨ãã¦ãã (MUST)ã PIPE_BUF
       ãã¤ãããå¤ãã®ãã¼ã¿ãæ¸ãè¾¼ã¿å ´å㯠atomic ã¨ã¯ãªããªãã ã¤ã¾ããã¤ãã¸ã®ä»ã®ãã‐
       ã»ã¹ã«ãããã¼ã¿ã®æ¸ãè¾¼ã¿ãéã«å¥ã å¯è½æ§ãããã POSIX.1-2001 ã®ä»æ§ã§ã¯ã
       PIPE_BUF ã¯æå°ã§ã 512 ãã¤ãã§ãããã¨ãè¦æ±ããã¦ãã (Linux ã§ã¯ PIPE_BUF ã¯
       4096 ãã¤ãã§ãã)ã æ£ç¢ºãªåä½ã¯ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã nonblocking
       (O_NONBLOCK) ãã©ããããã¤ãã¸ã®æ¸ãè¾¼ã¿ãè¤æ°ããè¡ããããã©ãããããã³
       æ¸ãè¾¼ã¿ãè¡ããã¤ãæ° n ã«ãã決å®ãããã

       O_NONBLOCK ç¡å¹, n <= PIPE_BUF
              n ãã¤ãå¨é¨ã®æ¸ãè¾¼ã¿ã atomic ã«è¡ãããã n
              ãã¤ãåãããã«æ¸ãè¾¼ãä½å°ããªãå ´å㯠write(2)  ã¯åæ¢ (block)
              ãããã¨ãããã

       O_NONBLOCK æå¹, n <= PIPE_BUF
              ãã¤ãã« n ãã¤ããæ¸ãè¾¼ãä½å°ãããå ´åã¯ã n ãã¤ãå¨é¨ãããã«æ¸ãè¾¼ã¾ããã
              ä½å°ããªãå ´åã¯ã write(2) ã¯å¤±æãã errno ã« EAGAIN ãã»ãããããã

       O_NONBLOCK ç¡å¹, n > PIPE_BUF
              æ¸ãè¾¼ã¿ã¯ atomic ã¨ã¯ãªããªãã write(2)
              ã«æ¸¡ããããã¼ã¿ã®éã«ãä»ã®ããã»ã¹ã«ãã write(2) ããããã¼ã¿ãå¥ããã¨ãããã
              write(2)  㯠n ãã¤ãã®æ¸ãè¾¼ã¿ãå®äºããã¾ã§åæ¢ããã

       O_NONBLOCK æå¹, n > PIPE_BUF
              ãã¤ãããã«ã®å ´åã write(2)  ã¯å¤±æãã errno ã« EAGAIN ãã»ãããããã
              ãã以å¤ã®å ´åã1 ãã¤ãä»¥ä¸ n ãã¤ã以ä¸ã®ãã¼ã¿ãæ¸ãè¾¼ã¾ãã
              (ã¤ã¾ããä¸é¨åã ãæ¸ãè¾¼ã¾ãããå ´åãããå¾ã)ã å¼ã³åºãå㯠write(2)
              ã®è¿ãå¤ãåç§ããå®éã«ä½ãã¤ãæ¸ãè¾¼ã¾ããã®ãã確èªãã¹ãã§ããã
              ã¾ããæ¸ãè¾¼ã¿ã«æåãããã¼ã¿ããä»ã®ããã»ã¹ãæ¸ãè¾¼ãã ãã¼ã¿ã
              éã«å¥ããã¨ãããã

   ãªã¼ãã³ãã¡ã¤ã«ç¶æãã©ã°
       ãªã¼ãã³ãã¡ã¤ã«ç¶æãã©ã°ã®ãã¡ããã¤ãã FIFO ã«å¯¾ãã¦æå³ãæã¤ã®ã¯ O_NONBLOCK
       㨠O_ASYNC ã ãã§ããã

       ãã¤ãã®èªã¿åºãå´ã« O_ASYNC ãã©ã°ãã»ããããã¨ããã¤ãã«æ°ããªå¥åãããã¨ã·ã°ãã«
       (ããã©ã«ãã§ã¯ SIGIO) ãçæããã (詳細㯠fcntl(2)  ãåç§)ã Linux ã§ã¯ã
       ãã¤ã㨠FIFO ã«å¯¾ãã O_ASYNC ã¯ã«ã¼ãã« 2.6 以éã§ã®ã¿ãµãã¼ãããã¦ããã

   移æ¤ã«é¢ãã注æ
       ããã¤ãã®ã·ã¹ãã  (Linux ã§ã¯ãªã) ã§ã¯ããã¤ãã¯åæ¹åã§ããã
       ã¤ã¾ããã¤ãã®ä¸¡ç«¯éã§ãã¼ã¿ã両æ¹åã«éä¿¡ãããã¨ãã§ããã POSIX.1-2001
       ã§ã¯ããã¤ãã¯ä¸æ¹åã®éä¿¡ã ãã«å¯¾å¿ãã¦ããã°ããã
       移æ¤ãèæ®ããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãåæ¹åãã¤ãã®ä»çµã¿ã åæã«ãã¹ãã§ã¯ãªãã

é¢é£é ç®
       dup(2), fcntl(2), open(2), pipe(2), poll(2), select(2), socketpair(2),
       stat(2), mkfifo(3), epoll(7), fifo(7)

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



Linux                             2005-12-08                           PIPE(7)