Wiki : hacking:using_fifos
 

Using FIFOs

Normally when redirecting output between processes pipes are by far the easiest option:

$ cat foo.txt | grep bar

But some programs don't accept data from the standard input. In that case you need to use FIFOs:

$ mkfifo /tmp/my-fifo

As almost everything on Unix, FIFOs are files. It means that for the program it'll be a file like any other. FIFOs are queues, First-In-First-Out, just like you would expect from a pipe, but the program will think it's reading from a file.

Suppose your program my_program is not ready to read from stdin and it's syntax is:

my_program [options] filename > results 2> log

If the filename is the output of another program you can't just pipe it like:

$ cat filename | my_program > results 2> log

But you can use a FIFO and pretend it's just a file:

$ mkfifo /tmp/my_fifo
$ cat filename > /tmp/my_fifo &
$ my_program /tmp/my_fifo > results 2> log

Note that I've put the first program (cat) running in background because both programs must be running at the same time just like when using pipes but in this case there is no such direct connection. When the first program (cat) gets to the end of the output or breaks a EOF will be sent to the FIFO and the second process will break as if it had reached the end of the file.



 
hacking/using_fifos.txt · Last modified: 05 09 2007 19:15 (external edit)
 
Recent changes RSS feed Creative Commons License Driven by DokuWiki