Skip to main content

Inter Process Communication(IPC)- Pipes

Interprocess communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities among different program processes that can run concurrently in an operating system. This allows a program to handle many user requests at the same time. Since even a single user request may result in multiple processes running in the operating system on the user's behalf, the processes need to communicate with each other. The IPC interfaces make this possible. Each IPC method has its own advantages and limitations so it is not unusual for a single program to use all of the IPC methods.
pipe
In computer programming, especially in UNIX operating systems, a pipe is a technique for passing information from one program process to another. Unlike other forms of interprocess communication (IPC), a pipe is one-way communication only. Basically, a pipe passes a parameter such as the output of one process to another process which accepts it as input. The system temporarily holds the piped information until it is read by the receiving process.
For two-way communication between processes, two pipes can be set up, one for each direction. A limitation of pipes for interprocess communication is that the processes using pipes must have a common parent process (that is, share a common open or initiation process and exist as the result of a fork system call from a parent process).
A pipe is fixed in size and is usually at least 4,096 byte
It is important to note the following about pipes as an IPC mechanism:
1. Unix pipes are buffers managed from within the kernel.
 2. Note that as a channel of communication, a pipe operates in one direction only.
3. Some plumbing (closing of ends) is required to use a pipe.
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. 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 a very insecure mode of communication.
6. Pipes cannot support broadcast. There is one other method of IPC using special files called “named pipes".
As an example, consider a case where one process gets a character string input and communicates it to the other process which reverses strings. Then we have two processes which need to communicate. Next we define a pipe and connect it between the processes to facilitate communication. One process gets input strings and writes into the pipe. The other process, which reverses strings, gets its input (i.e. reads) from the pipe.
Suppose two processes, Process A and Process B, need to communicate, then it is imperative that the process which writes closes its read end of the pipe and the process which read closes its write end of the pipe. Essentially, for a communication from Process A to process B the following should happen. Process A should keep its write end open and close read end of the pipe. Similarly, Process B should keep its read end open and close its write end.
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, Process B, closes its write end.
4. The parent process closes both 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.
We must first open a pipe 
UNIX allows two ways of opening a pipe.
FILE *popen(char *command, char *type) -- opens a pipe for I/O where the command is the process that will be connected to the calling process thus creating the pipe. The type is either "r" - for reading, or "w" for writing. 
popen() returns is a stream pointer or NULL for any errors. 
A pipe opened by popen() should always be closed by pclose(FILE *stream). 
We use fprintf() and fscanf() to communicate with the pipe's stream.
int pipe(int fd[2]) -- creates a pipe and returns two file descriptors, fd[0], fd[1]. fd[0] is opened for reading, fd[1] for writing. 
pipe() returns 0 on success, -1 on failure and sets errno accordingly. 
The standard programming model is that after the pipe has been set up, two (or more) cooperative processes will be created by a fork and data will be passed using read() and write(). 
Pipes opened with pipe() should be closed with close(int fd). 
Implementation
pipe.c
#include <stdio.h>
 #include<ctype.h>
 main()
 { int p_des[2];
 pipe( p_des ); /* The pipe descriptor */
 printf("Input a string \n");
 if ( fork () == 0 )
{   dup2(p_des[1], 1);  /*The dup command replaces the standard I/O channels by pipe descriptors.*/
     close(p_des[0]); /* process-A closing read end of the pipe */
     execlp("./get_str", "get_str", 0); /*** exit(1); ***/
}
 else if ( fork () == 0 )
   { dup2(p_des[0], 0);
   close(p_des[1]); /* process-B closing write end of the pipe */
   execlp("./rev_str", "rev_str", 0); /*** exit(1); ****/
 }
else
{  close(p_des[1]); /* parent closing both the ends of pipe */
   close(p_des[0]);
   wait(0);
   wait(0); }
   fflush(stdout);
 }
/*****************************************/
get_str.c
#include <stdio.h>
 #include<ctype.h>
void get_str(char str[])
 { char c;
int ic;
c = getchar();
 ic = 0;
while ( ic < 10 && ( c != EOF && c != '\n' && c != '\t' ))
{ str[ic] = c; c = getchar(); ic++; }
str[ic] = '\0'; return;
}
/*****************************************/
rev_str.c
 void rev_str(char str1[], char str2[])
 { char c; int ic; int rc;
ic = 0; c = str1[0];
while( ic < 10 && (c != EOF && c != '\0' && c != '\n') )
 { ic++; c = str1[ic]; }
str2[ic] = '\0'; rc = ic - 1; ic = 0;
while (rc-ic > -1)
{ str2[rc-ic] = str1[ic]; ic++; }
return;

 }

Comments

Popular posts from this blog

Server/Client Communication-python

The basic mechanisms of client-server setup are:

A client app send a request to a server app. The server app returns a reply.  Some of the basic data communications between client and server are:
File transfer - sends name and gets a file. Web page - sends url and gets a page. Echo - sends a message and gets it back.  Client server communication uses socket.
             To connect to another machine, we need a socket connection. What's a connection?  A relationship between two machines, where two pieces of software know about each other. Those two pieces of software know how to communicate with each other. In other words, they know how to send bits to each other. A socket connection means the two machines have information about each other, including network location (IP address) and TCP port. (If we can use anology, IP address is the phone number and the TCP port is the extension).  A socket is an object similar to a file that allows a program to accept incoming connections, make o…

Inter Process Communication-Message Queue

Interprocess communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities among different program processes that can run concurrently in an operating system. This allows a program to handle many user requests at the same time. Since even a single user request may result in multiple processes running in the operating system on the user's behalf, the processes need to communicate with each other. The IPC interfaces make this possible. Each IPC method has its own advantages and limitations so it is not unusual for a single program to use all of the IPC methods. Message Based Communication Messages are a very general form of communication. Messages can be used to send and receive formatted data streams between arbitrary processes. Messages may have types. This helps in message interpretation. The type may specify appropriate permissions for processes. Usually at the receiver end, messages are put in a queue. Messages may also be formatted in …