Featured Post

Friday, June 24, 2016

Inter process communication using pipes



Pipe:
    Inter process communication can be achieved using pipes. A pipe takes integer array of size two. At one end of the pipe write operation is performed and at the other end read operation is performed.
Make sure that read operation fails in pipe when there is no writer writing to the pipe and write operation fails if there is no corresponding reader read to read the data from the pipe.

Pipe can be categorized in to two types
i) anonymous or unnamed pipes
ii)named pipes(FIFO)

let us know about first anonymous pipe.

syntax:
                     int pfds[2];
                     int pipe(pfds);
On success pipe return two file descriptors for two ends of the pipe.

/* Inter process communication using pipes*/

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
int main()
{
  int pfds[2];
   char buf[20];
    if(pipe(pfds)==-1)
     {
       perror("pipes");
       exit(1);
     }
  printf("wirting to file descriptor %d\n",pfds[1]);
  write(pfds[1],"ashok",6);
  printf("reading from the file descriptor %d\n",pfds[0]);
  read(pfds[0],buf,6);
  printf("read data is %s",buf);
}

ouput:

writing tt file descriptor 4
reading from the file descriptor 3
read data is ashok


IPC using pipes,named pipes,read , write open system calls.inter process communication using pipes , IPC using named pipes.IPC mechanisms, interprocess communication using shared memory message queues semaphores .

No comments:

Post a Comment