Pipes – Interprocess Communication

By | September 26, 2021

Pipes – Interprocess Communication

  • A pipe is an another technique used for inter process communication (IPC).
  • A pipe is a mechanism by which the output of one process is directed into the input of another process. Thus it provides one way flow of data between two related processes.
  • In pipes, a temporary intermediate file is created into which the first process writes its output (data) and from which the second process reads its input (data) out of the pipe.

Representation of a pipe

  • Although pipe can be accessed like an ordinary file, the system actually manages it as FIFO queue.
  • A pipe file is created using the pipe system call.
  • As shown in figure a pipe has an input end and an output end. One can write into a pipe from input end and read from the output end.
  • A pipe descriptor, therefore has an array that stores two pointers, one pointer is for its input end and the other pointer is for its output end.
  • When a process defines a pipe it gets both addresses as shown in figure

Association of Pipe with a process

Example of Pipes

For example, let us suppose array pp is used to store descriptors. pp[0] stores the write end address and pp[1] stores the read end address.

  • Suppose two processes, Process A and Process B, need to communicate. In such a case, it is important that the process which writes closes its read end of the pipe and the process which reads closes its write end of a pipe. Essentially, for a communication from Process A to Process B the following should happen :

1. Process A should keep its write end open and close the read end of the pipe.

2. Process B should keep its read end open and close its write end.

  • Any process that has access to the descriptors of a pipe can communicate through that pipe. When a parent process creates children, UNIX system automatically duplicate file descriptor of the parent for the children. Thus, the child processes can access and communicate through the pipe.

Pipe as an IPC mechanism

This is accomplished as described is the following steps :

  1. First we have a parent process which declares a pipe in it.
  2. Next we spawn two child processes. Both of these would get the pipe definition which we have defined in the parent. The child processes, as well as the parent, have both the write and read ends of the pipe open at this time.
  3. Next, one child process, say Process A, closes its read end and the other child process, say Process B, closes its write end.
  4. The parent process closes both the write and read ends.
  5. Next, Process A is populated with code to get a string and Process B is populated to reverse a string with the above arrangement the output from Process A is piped as input to Process B.
  • When a pipe is created, it is given a fixed size in bytes.
  • When a process attempts to write into the pipe, the write request is immediately executed if the pipe is not full. However if pipe is full the process is blocked until the state of pipe changes.

Pipes - Interprocess Communication

  • Similarly, a reading process is blocked if it attempts to read more bytes that a currently in the pipe; otherwise the read request is immediately executed.
  • The operating system enforces mutual exclusion; that is, only one process can access pipe at a time.
  • When all the processes close the pipe descriptors, the pipe is closed and the pipe he destroyed. The operating system releases the pipe space.

Types of Pipes

There are two types of pipes : named and unnamed.

  • An unnamed pipe is a temporary file used to send data between two processes: unnamed pipe can work only between related processes (e.g. parent and child). It cannot be used by unrelated processes.
  • A named pipe is a permanent file used for this purpose.

The various features of named and unnamed pipe are compared below :

  1. Unnamed pipes are temporary arrangement or features. They are created in the program and terminated with the termination of the program.
  2. Named pipes are permanent arrangement or features. They are available in the user’s current directory even after the termination of the program, which has created this particular named pipe.
  3. An unnamed pipe has two file descriptors indicated by array subscript 0 and 1, which are used for reading and writing respectively.
  4. While a named pipe has only one file descriptor and like an ordinary file we can open the named pipe in read and write modes.
  5. Named pipes can be created even on the prompt using mknod shell command in “XINN Simple pipes cannot be created like that when we create a simple pipe with the system call “pipe(pp)”, the pipe descriptor pp[0] is used for reading and pp[1] for writing.
  6. Named pipes have ownership attached with them. Also named pipes have mode facility-this mode facility also can be changed on using ‘chmod‘ command. No such things are available for simple pipes.
  7. Named pipes can be used for IPC between any two processes by using the name associated with the pipe. While simple pipes can be used to communicate only between parent and child processes or/and between processes with the same ancestor.
  8. Simple pipes once created need not be opened but named pipes should be opened after creating it and after using it must be closed.

Limitations of Pipes

IPC through pipes has certain limitations as well. These are:

  1. As a channel of communication a pipe operates in one direction only.
  2. Pipes cannot support broadcast i.e. sending message to multiple processes at the same time.
  3. The read end of a pipe reads any way. It does not matter which process is connected to the write end of the pipe. Therefore, this is very insecure mode of communication.
  4. Pipes are useful when both the processes are schedulable and are resident on the same machine. So pipes are not useful for processes across networks.
  5. Some plumbing (closing of ends) is required to create a properly directed pipe.

Leave a Reply

Your email address will not be published. Required fields are marked *