pages tagged chardevyakkinghttp://yakking.branchable.com/tags/chardev/yakkingikiwiki2016-02-03T12:00:15ZFIFO, fifo, it's off to work we go!http://yakking.branchable.com/posts/fifos/Richard Maw2016-02-03T12:00:15Z2016-02-03T12:00:06Z
<p>A <a href="http://man7.org/linux/man-pages/man7/fifo.7.html">fifo(7)</a>, or "named pipe" is a character device,
that when is opened by one process for reading,
and another for writing,
allows inter-process communication.</p>
<p>This is similar to how you can use a <a href="http://man7.org/linux/man-pages/man7/pipe.7.html">pipe(7)</a>,
but does not require both processes to be started by a common ancestor.</p>
<p>This can be demonstrated in two terminals.
In the first run:</p>
<pre><code>$ mkfifo test.fifo
$ cat test.fifo
</code></pre>
<p>In the second run:</p>
<pre><code>$ cat >test.fifo
</code></pre>
<p>Now when you enter lines of text into the second terminal,
they will be output on the first.</p>
<p>fifos also have another trick up their sleeves,
as when a process opens a fifo,
it will block until the other end has been opened.</p>
<p>Using this <a href="http://yakking.branchable.com/posts/fifos/ephemeral-launch">ephemeral-launch</a> script,
we can start an echo server on a random port,
and have a client block until the server is ready,
and also report which port should be used.</p>
<p>In one terminal run:</p>
<pre><code>$ mkfifo test.fifo
$ ephemeral-launch --port-file test.fifo cat
</code></pre>
<p>In a second terminal run:</p>
<pre><code>$ port="$(cat test.fifo)"
$ nc 127.0.0.1 "$port"
</code></pre>
<p>Now when you enter lines of text into the second terminal,
it will echo them back.</p>
<p>Because the processes block on the open,
the <code>cat test.fifo</code> in the second terminal will block
until the <code>ephemeral-launch</code> process has bound the port
and written it out to the fifo.</p>