Page 1 of 1

Named pipes - FIFO pipes.

Posted: 21 Jun 2014, 15:34
by viking60
We have that regular pipes typically

Code: Select all

cat /var/log/somelog.log |less
It will show you the log per page. The named pipes or FIFO (First in First Out) pipes are a container ready to receive your instructions and is a file so you have to create it:

Code: Select all

mkfifo berserk_pipe

To put this file in receiver mode you can simply do a

Code: Select all

cat berserk_pipe

Now this terminal will be "frozen" waiting for messages.
Open another terminal next to the frozen one and type:

Code: Select all

echo "Hi Hola Hallo!!" >>berserk_pipe


The message will turn up in the other receiving terminal and it will go back to the normal prompt
Image

To make the receiving terminal remain in that "listening mode" you can start it with:

Code: Select all

tail -f berserk_pipe

Image
As you can see the left terminal will wait for any command or message. You can send regular commands too:

Code: Select all

ls >> berserk_pipe

Image
It can be useful when exchanging data between different users etc.

You can also use this in combination with SSH

Code: Select all

ssh berserk@10.0.0.12 "echo hallo >> berserk_pipe"


Remember berserk_pipe is just a file and you can have a look at it with:

Code: Select all

ls -l berserk_pipe

the output will be something like this:

Code: Select all

prw-r--r-- 1 berserk berserk 0 juni  21 16:15 berserk_pipe

Note the p in front it indicates that it is a pipe.

If you have started the pipe with tail -f you need to stop it with CTRL+C
The pipe file can be removed with a regular rm.

So what can this be used for? Use your imagination :-D I will come up with a practical example later...

Re: Named pipes - FIFO pipes.

Posted: 21 Jun 2014, 16:20
by viking60
You can use FIFO pipes to control your mplayer.

Code: Select all

mkfifo ~/mplayer-control

then start the concert with whatever file you want to play:

Code: Select all

 mplayer -slave -input file=/home/berserk/mplayer-control FileToPlay.mp3


From another terminal you can start and stop the music with:

Code: Select all

echo "pause" >~/mplayer-control


Works like a charm and how could you ever live without it :whistle: