Example of using named pipes in Linux shell (Bash)

LinuxBashShellPipeNamed Pipes

Linux Problem Overview


Can someone post a simple example of using named pipes in Bash on Linux?

Linux Solutions


Solution 1 - Linux

One of the best examples of a practical use of a named pipe...

From http://en.wikipedia.org/wiki/Netcat:

> Another useful behavior is using netcat as a proxy. Both ports and hosts can be redirected. Look at this example: > > nc -l 12345 | nc www.google.com 80 > > Port 12345 represents the request. > > This starts a nc server on port 12345 and all the connections get redirected to google.com:80. If a web browser makes a request to nc, the request will be sent to google but the response will not be sent to the web browser. That is because pipes are unidirectional. This can be worked around with a named pipe to redirect the input and output. > > mkfifo backpipe > nc -l 12345 0www.google.com 80 1>backpipe

Solution 2 - Linux

Here are the commands:

mkfifo named_pipe
echo "Hi" > named_pipe &
cat named_pipe

The first command creates the pipe.

The second command writes to the pipe (blocking). The & puts this into the background so you can continue to type commands in the same shell. It will exit when the FIFO is emptied by the next command.

The last command reads from the pipe.

Solution 3 - Linux

Open two different shells, and leave them side by side. In both, go to the /tmp/ directory:

cd /tmp/

In the first one type:

mkfifo myPipe
echo "IPC_example_between_two_shells">myPipe

In the second one, type:

while read line; do echo "What has been passed through the pipe is ${line}"; done<myPipe

First shell won't give you any prompt back until you execute the second part of the code in the second shell. It's because the fifo read and write is blocking.

You can also have a look at the FIFO type by doing a ls -al myPipe and see the details of this specific type of file.

Next step would be to embark the code in a script!

Solution 4 - Linux

Creating a named pipe
$ mkfifo pipe_name

On Unix-likes named pipe (FIFO) is a special type of file with no content. The mkfifo command creates the pipe on a file system (assigns a name to it), but doesn't open it. You need to open and close it separately like any other file.

Using a named pipe

Named pipes are useful when you need to pipe from/to multiple processes or if you can't connect two processes with an anonymous pipe. They can be used in multiple ways:

  • In parallel with another process:

    $ echo 'Hello pipe!' > pipe_name &       # runs writer in a background
    $ cat pipe_name
    Hello pipe!
    

    Here writer runs along the reader allowing real-time communication between processes.

  • Sequentially with file descriptors:

    $ # open the pipe on auxiliary FD #5 in both ways (otherwise it will block),
    $ # then open descriptors for writing and reading and close the auxiliary FD
    $ exec 5<>pipe_name 3>pipe_name 4<pipe_name 5>&-
    $
    $ echo 'Hello pipe!' >&3                 # write into the pipe through FD #3
      ...
    $ exec 3>&-                              # close the FD when you're done
    $                                        # (otherwise reading will block)
    $ cat <&4
    Hello pipe!
    ...
    $ exec 4<&-
    

    In fact, communication through a pipe can be sequential, but it's limited to a buffer size of 64 KB.
    It's preferable to use descriptors to transfer multiple pieces of data in order to reduce overhead.

  • Conditionally with signals:

    $ handler() {
    >     cat <&3
    >
    >     exec 3<&-
    >     trap - USR1                        # unregister signal handler (see below)
    >     unset -f handler writer            # undefine the functions
    > }
    $
    $ exec 4<>pipe_name 3<pipe_name 4>&-
    $ trap handler USR1                      # register handler for signal USR1
    $
    $ writer() {
    >     if <condition>; then
    >         kill -USR1 $PPID               # send the signal USR1 to a specified process
    >         echo 'Hello pipe!' > pipe_name
    >     fi
    > }
    $ export -f writer                       # pass the function to child shells
    $
    $ bash -c writer &                       # can actually be run sequentially as well
    $
    Hello pipe!
    

    FD allows data transfer to start before the shell is ready to receive it. Required when used sequentially.
    Signal should be sent before the data to prevent a deadlock if pipe buffer fills up.

Destroying a named pipe

The pipe itself (and its content) gets destroyed when all descriptors to it are closed. What's left is just a name.
To make the pipe anonymous and unavailable under the given name (can be done when the pipe is still open) you could use the rm console command (this is the opposite of mkfifo command):

$ rm pipe_name

Solution 5 - Linux

Terminal 1:

$ mknod new_named_pipe p
$ echo 123 > new_named_pipe
  • Terminal 1 created a named pipe.
  • It wrote data in it using echo.
  • It is blocked as there is no receiving end (as pipes both named and unnamed need receiving and writing ends to it)

Terminal 2:

$ cat new_named_pipe
$ 123
$ 
  • From Terminal 2, a receiving end for the data is added.
  • It read the data in it using cat.
  • Since both receiving and writing ends are there for the new_named_pipe it displays the information and blocking stops

Named pipes are used everywhere in Linux, most of the char and block files we see during ls -l command are char and block pipes (All of these reside at /dev). These pipes can be blocking and non-blocking, and the main advantage is these provides the simplest way for IPC.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrew LeSueurView Question on Stackoverflow
Solution 1 - LinuxBrian ClementsView Answer on Stackoverflow
Solution 2 - LinuxKhaledView Answer on Stackoverflow
Solution 3 - LinuxNicolas MasView Answer on Stackoverflow
Solution 4 - LinuxEvgenKo423View Answer on Stackoverflow
Solution 5 - LinuxChaitanya TetaliView Answer on Stackoverflow