pipe.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. @node Pipes and FIFOs, Sockets, File System Interface, Top
  2. @c %MENU% A simple interprocess communication mechanism
  3. @chapter Pipes and FIFOs
  4. @cindex pipe
  5. A @dfn{pipe} is a mechanism for interprocess communication; data written
  6. to the pipe by one process can be read by another process. The data is
  7. handled in a first-in, first-out (FIFO) order. The pipe has no name; it
  8. is created for one use and both ends must be inherited from the single
  9. process which created the pipe.
  10. @cindex FIFO special file
  11. A @dfn{FIFO special file} is similar to a pipe, but instead of being an
  12. anonymous, temporary connection, a FIFO has a name or names like any
  13. other file. Processes open the FIFO by name in order to communicate
  14. through it.
  15. A pipe or FIFO has to be open at both ends simultaneously. If you read
  16. from a pipe or FIFO file that doesn't have any processes writing to it
  17. (perhaps because they have all closed the file, or exited), the read
  18. returns end-of-file. Writing to a pipe or FIFO that doesn't have a
  19. reading process is treated as an error condition; it generates a
  20. @code{SIGPIPE} signal, and fails with error code @code{EPIPE} if the
  21. signal is handled or blocked.
  22. Neither pipes nor FIFO special files allow file positioning. Both
  23. reading and writing operations happen sequentially; reading from the
  24. beginning of the file and writing at the end.
  25. @menu
  26. * Creating a Pipe:: Making a pipe with the @code{pipe} function.
  27. * Pipe to a Subprocess:: Using a pipe to communicate with a
  28. child process.
  29. * FIFO Special Files:: Making a FIFO special file.
  30. * Pipe Atomicity:: When pipe (or FIFO) I/O is atomic.
  31. @end menu
  32. @node Creating a Pipe
  33. @section Creating a Pipe
  34. @cindex creating a pipe
  35. @cindex opening a pipe
  36. @cindex interprocess communication, with pipes
  37. The primitive for creating a pipe is the @code{pipe} function. This
  38. creates both the reading and writing ends of the pipe. It is not very
  39. useful for a single process to use a pipe to talk to itself. In typical
  40. use, a process creates a pipe just before it forks one or more child
  41. processes (@pxref{Creating a Process}). The pipe is then used for
  42. communication either between the parent or child processes, or between
  43. two sibling processes.
  44. The @code{pipe} function is declared in the header file
  45. @file{unistd.h}.
  46. @pindex unistd.h
  47. @deftypefun int pipe (int @var{filedes}@t{[2]})
  48. @standards{POSIX.1, unistd.h}
  49. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  50. @c On Linux, syscall pipe2. On HURD, call socketpair.
  51. The @code{pipe} function creates a pipe and puts the file descriptors
  52. for the reading and writing ends of the pipe (respectively) into
  53. @code{@var{filedes}[0]} and @code{@var{filedes}[1]}.
  54. An easy way to remember that the input end comes first is that file
  55. descriptor @code{0} is standard input, and file descriptor @code{1} is
  56. standard output.
  57. If successful, @code{pipe} returns a value of @code{0}. On failure,
  58. @code{-1} is returned. The following @code{errno} error conditions are
  59. defined for this function:
  60. @table @code
  61. @item EMFILE
  62. The process has too many files open.
  63. @item ENFILE
  64. There are too many open files in the entire system. @xref{Error Codes},
  65. for more information about @code{ENFILE}. This error never occurs on
  66. @gnuhurdsystems{}.
  67. @end table
  68. @end deftypefun
  69. Here is an example of a simple program that creates a pipe. This program
  70. uses the @code{fork} function (@pxref{Creating a Process}) to create
  71. a child process. The parent process writes data to the pipe, which is
  72. read by the child process.
  73. @smallexample
  74. @include pipe.c.texi
  75. @end smallexample
  76. @node Pipe to a Subprocess
  77. @section Pipe to a Subprocess
  78. @cindex creating a pipe to a subprocess
  79. @cindex pipe to a subprocess
  80. @cindex filtering i/o through subprocess
  81. A common use of pipes is to send data to or receive data from a program
  82. being run as a subprocess. One way of doing this is by using a combination of
  83. @code{pipe} (to create the pipe), @code{fork} (to create the subprocess),
  84. @code{dup2} (to force the subprocess to use the pipe as its standard input
  85. or output channel), and @code{exec} (to execute the new program). Or,
  86. you can use @code{popen} and @code{pclose}.
  87. The advantage of using @code{popen} and @code{pclose} is that the
  88. interface is much simpler and easier to use. But it doesn't offer as
  89. much flexibility as using the low-level functions directly.
  90. @deftypefun {FILE *} popen (const char *@var{command}, const char *@var{mode})
  91. @standards{POSIX.2, stdio.h}
  92. @standards{SVID, stdio.h}
  93. @standards{BSD, stdio.h}
  94. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  95. @c popen @ascuheap @asucorrupt @acucorrupt @aculock @acsfd @acsmem
  96. @c malloc dup @ascuheap @acsmem
  97. @c _IO_init ok
  98. @c _IO_no_init ok
  99. @c _IO_old_init ok
  100. @c _IO_lock_init ok
  101. @c _IO_new_file_init @asucorrupt @acucorrupt @aculock @acsfd
  102. @c _IO_link_in @asucorrupt @acucorrupt @aculock @acsfd
  103. @c the linked list is guarded by a recursive lock;
  104. @c it may get corrupted with async signals and cancellation
  105. @c _IO_lock_lock dup @aculock
  106. @c _IO_flockfile dup @aculock
  107. @c _IO_funlockfile dup @aculock
  108. @c _IO_lock_unlock dup @aculock
  109. @c _IO_new_proc_open @asucorrupt @acucorrupt @aculock @acsfd
  110. @c the linked list is guarded by a recursive lock;
  111. @c it may get corrupted with async signals and cancellation
  112. @c _IO_file_is_open ok
  113. @c pipe2 dup @acsfd
  114. @c pipe dup @acsfd
  115. @c _IO_fork=fork @aculock
  116. @c _IO_close=close_not_cancel dup @acsfd
  117. @c fcntl dup ok
  118. @c _IO_lock_lock @aculock
  119. @c _IO_lock_unlock @aculock
  120. @c _IO_mask_flags ok [no @mtasurace:stream, nearly but sufficiently exclusive access]
  121. @c _IO_un_link @asucorrupt @acucorrupt @aculock @acsfd
  122. @c the linked list is guarded by a recursive lock;
  123. @c it may get corrupted with async signals and cancellation
  124. @c _IO_lock_lock dup @aculock
  125. @c _IO_flockfile dup @aculock
  126. @c _IO_funlockfile dup @aculock
  127. @c _IO_lock_unlock dup @aculock
  128. @c free dup @ascuheap @acsmem
  129. The @code{popen} function is closely related to the @code{system}
  130. function; see @ref{Running a Command}. It executes the shell command
  131. @var{command} as a subprocess. However, instead of waiting for the
  132. command to complete, it creates a pipe to the subprocess and returns a
  133. stream that corresponds to that pipe.
  134. If you specify a @var{mode} argument of @code{"r"}, you can read from the
  135. stream to retrieve data from the standard output channel of the subprocess.
  136. The subprocess inherits its standard input channel from the parent process.
  137. Similarly, if you specify a @var{mode} argument of @code{"w"}, you can
  138. write to the stream to send data to the standard input channel of the
  139. subprocess. The subprocess inherits its standard output channel from
  140. the parent process.
  141. In the event of an error @code{popen} returns a null pointer. This
  142. might happen if the pipe or stream cannot be created, if the subprocess
  143. cannot be forked, or if the program cannot be executed.
  144. @end deftypefun
  145. @deftypefun int pclose (FILE *@var{stream})
  146. @standards{POSIX.2, stdio.h}
  147. @standards{SVID, stdio.h}
  148. @standards{BSD, stdio.h}
  149. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @ascuplugin{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  150. @c Although the stream cannot be used after the call, even in case of
  151. @c async cancellation, because the stream must not be used after pclose
  152. @c is called, other stdio linked lists and their locks may be left in
  153. @c corrupt states; that's where the corrupt and lock annotations come
  154. @c from.
  155. @c
  156. @c pclose @ascuheap @ascuplugin @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
  157. @c _IO_new_fclose @ascuheap @ascuplugin @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
  158. @c _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
  159. @c _IO_acquire_lock dup @aculock
  160. @c _IO_flockfile dup @aculock
  161. @c _IO_file_close_it @ascuheap @ascuplugin @asucorrupt @aculock @acucorrupt @acsfd @acsmem
  162. @c _IO_file_is_open dup ok
  163. @c _IO_do_flush @asucorrupt @ascuplugin @acucorrupt
  164. @c _IO_do_write @asucorrupt @acucorrupt
  165. @c new_do_write @asucorrupt @acucorrupt
  166. @c _IO_SYSSEEK ok
  167. @c lseek64 dup ok
  168. @c _IO_SYSWRITE ok
  169. @c write_not_cancel dup ok
  170. @c write dup ok
  171. @c _IO_adjust_column ok
  172. @c _IO_setg dup @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  173. @c _IO_wdo_write @asucorrupt @ascuplugin @acucorrupt
  174. @c _IO_new_do_write=_IO_do_write dup @asucorrupt @acucorrupt
  175. @c *cc->__codecvt_do_out @ascuplugin
  176. @c _IO_wsetg dup @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  177. @c _IO_unsave_markers @ascuheap @asucorrupt @acucorrupt @acsmem
  178. @c _IO_have_backup dup ok
  179. @c _IO_free_backup_area dup @ascuheap @asucorrupt @acucorrupt @acsmem
  180. @c _IO_SYSCLOSE @aculock @acucorrupt @acsfd
  181. @c _IO_lock_lock dup @aculock
  182. @c _IO_close=close_not_cancel dup @acsfd
  183. @c _IO_lock_unlock dup @aculock
  184. @c _IO_waitpid=waitpid_not_cancel dup ok
  185. @c _IO_have_wbackup ok
  186. @c _IO_free_wbackup_area @ascuheap @asucorrupt @acucorrupt @acsmem
  187. @c _IO_in_backup dup ok
  188. @c _IO_switch_to_main_wget_area @asucorrupt @acucorrupt
  189. @c free dup @ascuheap @acsmem
  190. @c _IO_wsetb @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  191. @c _IO_wsetg @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  192. @c _IO_wsetp @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  193. @c _IO_setb @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  194. @c _IO_setg @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  195. @c _IO_setp @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
  196. @c _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
  197. @c _IO_release_lock dup @aculock
  198. @c _IO_funlockfile dup @aculock
  199. @c _IO_FINISH @ascuheap @ascuplugin @asucorrupt @acucorrupt @aculock @acsfd @acsmem
  200. @c _IO_new_file_finish @ascuheap @ascuplugin @asucorrupt @acucorrupt @aculock @acsfd @acsmem
  201. @c _IO_file_is_open dup ok
  202. @c _IO_do_flush dup @ascuplugin @asucorrupt @acucorrupt
  203. @c _IO_SYSCLOSE dup @aculock @acucorrupt @acsfd
  204. @c _IO_default_finish @ascuheap @asucorrupt @acucorrupt @aculock @acsfd @acsmem
  205. @c FREE_BUF @acsmem
  206. @c munmap dup @acsmem
  207. @c free dup @ascuheap @acsmem
  208. @c _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
  209. @c _IO_lock_fini ok
  210. @c libc_lock_fini_recursive ok
  211. @c libc_lock_lock dup @asulock @aculock
  212. @c gconv_release_step ok
  213. @c libc_lock_unlock dup @asulock @aculock
  214. @c _IO_have_backup ok
  215. @c _IO_free_backup_area @ascuheap @asucorrupt @acucorrupt @acsmem
  216. @c _IO_in_backup ok
  217. @c _IO_switch_to_main_get_area @asucorrupt @acucorrupt
  218. @c free dup @ascuheap @acsmem
  219. @c free dup @ascuheap @acsmem
  220. The @code{pclose} function is used to close a stream created by @code{popen}.
  221. It waits for the child process to terminate and returns its status value,
  222. as for the @code{system} function.
  223. @end deftypefun
  224. Here is an example showing how to use @code{popen} and @code{pclose} to
  225. filter output through another program, in this case the paging program
  226. @code{more}.
  227. @smallexample
  228. @include popen.c.texi
  229. @end smallexample
  230. @node FIFO Special Files
  231. @section FIFO Special Files
  232. @cindex creating a FIFO special file
  233. @cindex interprocess communication, with FIFO
  234. A FIFO special file is similar to a pipe, except that it is created in a
  235. different way. Instead of being an anonymous communications channel, a
  236. FIFO special file is entered into the file system by calling
  237. @code{mkfifo}.
  238. Once you have created a FIFO special file in this way, any process can
  239. open it for reading or writing, in the same way as an ordinary file.
  240. However, it has to be open at both ends simultaneously before you can
  241. proceed to do any input or output operations on it. Opening a FIFO for
  242. reading normally blocks until some other process opens the same FIFO for
  243. writing, and vice versa.
  244. The @code{mkfifo} function is declared in the header file
  245. @file{sys/stat.h}.
  246. @pindex sys/stat.h
  247. @deftypefun int mkfifo (const char *@var{filename}, mode_t @var{mode})
  248. @standards{POSIX.1, sys/stat.h}
  249. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  250. @c On generic Posix, calls xmknod.
  251. The @code{mkfifo} function makes a FIFO special file with name
  252. @var{filename}. The @var{mode} argument is used to set the file's
  253. permissions; see @ref{Setting Permissions}.
  254. The normal, successful return value from @code{mkfifo} is @code{0}. In
  255. the case of an error, @code{-1} is returned. In addition to the usual
  256. file name errors (@pxref{File Name Errors}), the following
  257. @code{errno} error conditions are defined for this function:
  258. @table @code
  259. @item EEXIST
  260. The named file already exists.
  261. @item ENOSPC
  262. The directory or file system cannot be extended.
  263. @item EROFS
  264. The directory that would contain the file resides on a read-only file
  265. system.
  266. @end table
  267. @end deftypefun
  268. @node Pipe Atomicity
  269. @section Atomicity of Pipe I/O
  270. Reading or writing pipe data is @dfn{atomic} if the size of data written
  271. is not greater than @code{PIPE_BUF}. This means that the data transfer
  272. seems to be an instantaneous unit, in that nothing else in the system
  273. can observe a state in which it is partially complete. Atomic I/O may
  274. not begin right away (it may need to wait for buffer space or for data),
  275. but once it does begin it finishes immediately.
  276. Reading or writing a larger amount of data may not be atomic; for
  277. example, output data from other processes sharing the descriptor may be
  278. interspersed. Also, once @code{PIPE_BUF} characters have been written,
  279. further writes will block until some characters are read.
  280. @xref{Limits for Files}, for information about the @code{PIPE_BUF}
  281. parameter.