Friday, February 3, 2012

Pipes : Named and UnNamed

In Unix/Linux world, pipes allow separate processes to communicate without having been designed explicitly to work together.

There are 2 kinds of pipes : unnamed and named. They are well explained in the following links

Difference between unnamed pipes and named pipes

Unnamed pipe

  1. These are created by the shell automatically.
  2. They exist in the kernel.
  3. They can not be accessed by any process,including the process that creates it.
  4. They are opened at the time of creation only.
  5. They are unidirectional.
  6. Unnamed pipes may be only used with related processes (parent/child or child/child having the same parent).
Eg:

ls | less

Here, both the processes are related - have same parent(the bash shell process is the parent of both ls and less command). It is unidirectional in the sense that output of ls is sent as input of less command

Named Pipe ( also called FIFO, First In FIrst Out)

  1. They are created programatically using the command mkfifo or mknod command.
  2. They exist in the file system with a given file name.
  3. They can be viewed and accessed by any two unrelated processes. ls cmd shows "p" in the permission bits for a named pipe.
  4. They are not opened while creation.
  5. They are Bi-directional.
  6. A process writing a named pipe blocks until there is a process that reads that data.
  7. Broken pipe error occurs when the writing process closes the named pipe while another reading process reads it.
Eg:

$ mkfifo nampipe

$ ls -l nampipe
prw-rw-r-- 1 foo foo 0 Jan 13 20:38 nampipe

So a pipe with name, nampipe, is created now.

Let us see how it works.

In one console, type the following command

$ ls -l > nampipe

The above command shall hang. This is because the other end of the pipe, nampipe, is not yet connected, and so the kernel suspends the above  first process until the second process opens the pipe.

Now let us run the second process, which opens the other end of the pipe, nampipe, in another console


$ cat < nampipe
total 636
drwxrwxr-x  4 foo foo   4096 Jan  5 22:17 a
-rwxrwxr-x  1 foo foo   9509 Jan  5 22:11 a.out
-rw-rw-r--  1 foo foo  10240 Jan  3 19:50 archive.tar

See, here first process and second process are unrelated.  The first process and second process are run in different shell consoles(hence, both process have different parent ids).


Differences between files and pipes 


  1. pipes have the bounded size - the maximum capacity of a pipe is referenced by the constant PIPE_BUF (usually limited to 5120 bytes.) 
  2. sequential access – one can only read or write from and to the pipe, the pointer of the current position can not be moved (lseek is unacceptable) 
  3. write appends data to the input of a pipe while read reads any data from output of a pipe but: 


  • data that is read are removed from the pipe 
  • if the pipe is empty and at least one descriptor for reading is open, then the read is blocked until some data are written to the pipe or until the pipe descriptor is closed  
  • the process is blocked if it wants to write and the pipe is full

No comments:

Post a Comment