Linux: named pipes and why they are super useful

named pipes or fifo queues are essentially a file where a process can write its output and another process read its input, you might now…

Linux: named pipes and why they are super useful
Photo by T K on Unsplash
Join Medium with my referral link - Konstantinos Patronas
Read every story from Konstantinos Patronas (and thousands of other writers on Medium). Your membership fee directly…

named pipes or fifo queues are essentially a file where a process can write its output and another process read its input, you might now wonder how this differs from the un-named pipe. the “|”, well it doesn't the concept is still the same but it has two advantages over un-named pipes

an un-named pipe needs a syntax of thiscommand1 | command2

This means three things

  1. command2 two (the consumer of data from command1) must be in the same line with command1, you cant start command2 when you want.
  2. command2 must be in the same user session as command1, this means that if you have a scenario where user1 will execute command1 and user2 will consume the data using command2 things will get complicated if you will use un-named pipes.
  3. the un-named pipe will “live” as long as the command executes, cannot be re-used from another command to generate or consume data.

All these problems are solved using named pipes, a named pipe appears as a file in the file system and can be created using mkfifo$ mkfifo test_pipe

It will appear a file in the system, and since ti users permissions commands like chown and chmod can be used.prw-rw-r-- 1 kpatronas kpatronas    0 Οκτ  13 08:34 test_pipe

Now lets do a practical example$ find . -type f > ./test_pipe

It should appear like the program is “hang” but its not, it just blocks the execution of command “find” until a consumer process will use this command to read data from

You can verify this if you search for a process named find, you will not find anything$ ps aux | grep -i find

Lets now see how we can consume data from the pipe!, in another terminal enter$ cat < ./test_pipe

Now find command will be executed and all output will go to test_pipe, then cat command will consume (print in stdout in our case) all the content of the find command.

You can generate/consume data from a pipe even as another user if the pipe has the necessary permissions to do this

Generate data (i generate data as user kpatronas)kpatronas@nostromo:~$ find . -type f > ./test_pipe

Consume data (i consume data as root user)root@nostromo:~# cat < /home/kpatronas/test_pipe

I hope you find this short article useful and help you make good use of some Linux plumbering! :)

Join Medium with my referral link - Konstantinos Patronas
Read every story from Konstantinos Patronas (and thousands of other writers on Medium). Your membership fee directly…