mknod is a powerful command with which you can create block or character special files. If you view the man page, you’ll see that you can use it to create block device links and character device links. If you don’t know what these are then don’t worry. The purpose of this tutorial is to explore the FIFO (First In First Out) feature.
A FIFO literally does what it says on the box. The first piece of data to go in is the first piece of data to go out.
The usage of the command is:
Usage: /bin/mknod [OPTION]… NAME TYPE [MAJOR MINOR]
Where MAJOR and MINOR are for the special devices mentioned above.
We will use:
/bin/mknod /tmp/test p
ns3:/var/www# ls -al /tmp/test
prw-r–r– 1 root root 0 2008-10-17 11:44 /tmp/test
We can now see that /tmp/test is created with the ‘p’ attribute (as opposed to - for a file, l for a symbolic link, c for a character device, etc)
The fifo is also “blocking”. This means that if you enter data into the fifo, the process will wait by default until the data is pulled out again.
Lets write a simple shell script to illustrate the workings of the fifo:
Open up two windows, in window 1:
staging:~# mknod ./fifo p
staging:~# while (true); do echo “FIFO Received: ” `cat ./fifo`; done
FIFO Received:Â As you can see
FIFO Received:Â This is a really simple way
FIFO Received:Â To use a FIFO
FIFO Received:
FIFO Received:Â This is a block Terminated by _EOT_ Lets see what this does
In window 2:
staging:~# echo “As you can see” >fifo
staging:~# echo “This is a really simple way” >fifo
staging:~# echo “To use a FIFO” >fifo
staging:~# cat >> fifo <<_EOT_
> This is a block
> Terminated by _EOT_
> Lets see what this does
> _EOT_
staging:~#
FIFOs are most often used programmatically, see the following great guide to using mknod and FIFOs in C.
http://www.ecst.csuchico.edu/~beej/guide/ipc/fifos.html
Tags: /bin/mknod, beej, block, C, cat, character, device, echo, fifo, first in first out, Linux, major, man, man page, minor, mknod, shell script, special files