You are on page 1of 2

FIFO Named pipes: mkfifo, mknod

Posted on February 14, 2008 Pipes are commonly used for interprocess communication. But the major disadvanta e of pipes is that they can be used only by one process !there are readers and "riters "ithin the same process# or the processes "hich share the same file descriptor table !normally the processes and the child processes or threads created by them#. $hus pipes have this bi limitation% they cannot pass information bet"een unrelated processes. $his is because they do not share the same file descriptor table. But if names are iven to the pipes, then one "ould be able to read or "rite data to them just li&e a normal file. $he processes need not even share anythin "ith each otherF'F( !First 'n First (ut# are also called named pipes. $he main features of F'F( are 1. 't implements F'F( feature of the pipes 2. $hey can be opened just li&e normal files usin their names ). *ata can be read from or "ritten to the fifo

Working with FIFO in a Shell


Creating a FIFO
mkfifo

creates fifo+ the named pipes ,yntamkfifo [options] fifo_name

.-ample
$ mkfifo fifo

$here is one more "ay by "hich "e can F'F( usin m&nod. m&nod is used to create bloc& or character special files.
$ mknod [OPTION]... NAME TYPE

$o create a F'F( fifo1


$ mknod fifo1 p

"here p coressponds to file type % pipe !remember F'F( is a named pipe#.

Reading/ Writing data from/to a FIFO


/et0s open t"o terminals 'n the first terminal
$ cat > fifo

"e are e-perimentin "ith the F'F($his is second line. 1fter openin the fifo in the second terminal for readin usin cat, you "ill notice the above t"o lines displayed there. 2o" open the second terminal and o to the directory containin the F'F( 3fifo0
$ cat fifo

"e are e-perimentin "ith the F'F($his is second line. 1fter openin the fifo in the second terminal for readin 2o" &eep on "ritin to the first terminal. 4ou "ill notice that every time you press enter, the coresspondin line appears in the second terminal. Pressin 5$6/7* in the first terminal terminates "ritin to the fifo. $his also terminates the second process because readin from the fifo no" enerates a 8B6(9.2 P'P.: si nal. $he default action for this is to terminate the process. /et us no" see the details of the file 3fifo0
$ ls l fifo p!" ! ! 1 #se! #se! $ %e& 1' 1$($) fifo

$he p in the be innin denotes that it is a pipe. /et0s see more details about the pipe usin stat
$ stat fifo %ile( *fifo+,i-e( $ .locks( $ IO .lock( '$/0 fifo 1e2ice( fd$$340'506d Inode( 11')'/7 8inks( 1 Access( 9$0''4p!" ! ! : ;id( 9 $4 #se!: <id( 9 $4 #se!: Access( =$$6 $= 1' 1$($)('/.$$$$$$$$$ >$)7$ Modif?( =$$6 $= 1' 1$($)('/.$$$$$$$$$ >$)7$ @3anAe( =$$6 $= 1' 1$($)('/.$$$$$$$$$ >$)7$

'f you notice carefully, F'F(s just li&e a normal file possess all the details li&e inode number, the number of lin&s to it, the access, modification times, si;e and the access permissions.

You might also like