INTER PROCESS COMMUNICATION – PIPE( )

 

Inter process communication (IPC): 

A  process can be of two types:

      Independent process.

      Co-operating process.

An independent process is not affected by the execution of other processes while a co operating process can be affected by other executing processes.

Inter process communication (IPC) is a mechanism which allows processes to communicate with each other and synchronize their actions. The communication between these processes can be seen as a method of co-operation between them. Processes can communicate with each other through both:

      Shared Memory

      Message passing

Shared memory & Message Passing


 

 

Inter process communication (IPC) is used for exchanging data between multiple threads in one or more processes or programs. The Processes may be running on single or multiple computers connected by a network. The full form of IPC is Inter-process communication.

It is a set of programming interface which allow a programmer to coordinate activities among various program processes which can run concurrently in an operating system. This allows a specific program to handle many user requests at the same time.

Since every single user request may result in multiple processes running in the operating system, the process may require to communicate with each other. Each IPC protocol approach has its own advantage and limitation, so it is not unusual for a single program to use all of the IPC methods.


Pipes

Pipe is widely used for communication between two related processes. This is a half-duplex method, so the first process communicates with the second process. However, in order to achieve a full- duplex, another pipe is needed.

 

Message Passing:

It is a mechanism for a process to communicate and synchronize. Using message passing, the process communicates with each other without resorting to shared variables.

  Send (message)- message size fixed or variable

  Received (message)

 

Message Queues

A message queue is a linked list of messages stored within the kernel. It is identified by a message queue identifier. This method offers communication between single or multiple processes with full-duplex capacity.

 

PIPES

   8MPipe is a communication medium between two or more related or interrelated processes. It can be    either within one process or a communication between the child and the parent processes. Communication is achieved by one process writing into the pipe and other reading from the pipe. To achieve the pipe system call, create two files, one to write into the file and another to read from the file.

Pipe within one process



This system call would create a pipe for one-way communication i.e., it creates two descriptors.

Descriptor pipedes[0] is for reading and pipedes[1] is for writing. Whatever is written into pipedes[1] can be read from pipedes[0].

#include <sys/types.h> #include <sys/stat.h> #include <fontl.h>

int open(const char *pathname, int flags);


Even though the basic operations for file are read and write, it is essential to open the file before performing the operations and closing the file after completion of the required operations. Usually, by default, 3 descriptors opened for every process, which are used for input (standard input - stdin), output (standard output - stdout) and error (standard error stderr) having file descriptors 0, 1 and 2 respectively.

Uni directional pipe:

Algorithm

      Step 1 - Create a pipe using pipe() system call.

      Step 2 – Send a message to the one end of the pipe.

      Step 3 – Retrieve the message from the other end of the pipe and write it to the standard output.

 

Expected Output:

hello, world #1

hello, world #2

   hello, world #3

 

Implementing command line pipe using exec() family of function:

Follow the steps to transfer the output of a process to pipe:

(i)  Close the standard output descriptor

(ii)         Use the following system calls, to take duplicate of output file descriptor of the pipe int dup(int fd); int dup2(int oldfd, int newfd);

(iii)  Close the input file descriptor of the pipe

(iv)  Now execute the process.

Follow the steps to get the input from the pipe for a process:

(i)  Close the standard input descriptor

(ii)  Take the duplicate of input file descriptor of the pipe using dup() system call

(iii)  Close the input file descriptor of the pipe

(iv)  Now execute the process

 

Named pipe

 

Named pipe (also known as FIFO) is one of the inter process communication tool. The system for FIFO is as follows

int mkfifo(const char *pathname, mode_t mode);

 

mkfifo() makes a FIFO special file with name pathname. Here mode specifies the FIFO's permissions. The permission can be like : O_CREAT|0644 Open FIFO in read-mode (O_RDONLY) to read and write-mode (O_WRONLY) to write.


 

Expected Output:

 

Writing to pipe - Message 1 is Hi Reading from pipe - Message 1 is Hi Writing to pipe - Message 2 is Hello Reading from pipe - Message 2 is Hello

2.         Program to write and read two messages through the pipe using the parent and the child processes.

Algorithm

 

Step 1 - Create a pipe.

Step 2 - Create a child process.

Step 3 – Parent process writes to the pipe.

Step 4 - Child process retrieves the message from the pipe and writes it to the standard output.

Step 5 – Repeat step 3 and step 4 once again.

Expected Output:

 

Parent Process - Writing to pipe - Message 1 is Hi Parent Process - Writing to pipe - Message 2 is Hello Child Process - Reading from pipe - Message 1 is Hi Child Process - Reading from pipe – Message 2 is Hello

Two-way Communication Using Pipes

Following are the steps to achieve two-way communication -

Step 1 - Create two pipes. First one is for the parent to write and child to read, say as pipe l. Second one is for the child to write and parent to read, say as pipe2.

Step 2 - Create a child process.

Step 3 – Close unwanted ends as only one end is needed for each communication.

Step 4 - Close unwanted ends in the parent process, read end of pipel and write end of pipe2.

Step 5 – Close the unwanted ends in the child process, write end of pipel and read end of pipe2.



2way Communication using Pipes

3.         Achieving two-way communication using pipes Algorithm

Step 1 - Create pipel for the parent process to write and the child process to read.

Step 2 - Create pipe2 for the child process to write and the parent process to read.

Step 3 – Close the unwanted ends of the pipe from the parent and child side.

Step 4 - Parent process to write a message and child process to read and display on the screen.

Step 5 – Child process to write a message and parent process to read and display on the screen.

 

Expected Output:

In Parent: Writing to pipe 1 - Message is Hi

In Child: Reading from pipe 1 - Message is Hi In Child: Writing to pipe 2 – Message is Hello

In Parent: Reading from pipe 2 – Message is Hello

 

Practice:
Q. Write the output of the following program

#include<stdio.h> #include<unistd.h> #include <sys/wait.h> int main()

int p[2]; char buff[25]; if(fork()==0)

printf("Child : Writing to pipe n"); write(p[1],"Welcome”,8); printf("Child Exiting\n");

else wait(NULL);

printf("Parent : Reading from pipe \n"); read(p[0],buff,8);

printf("Pipe content is : %s\n",buff);

return 0;

Output:

 

Post a Comment

Previous Post Next Post