C program for interprocess communication using pipes


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg
INTERPROCESS COMMUNICATION
USING PIPES

AIM:
          To implement the concept of interprocess communication using pipes using c program.

ALGORITHM:

1.      create the pipe and create the process.
2.      get the input in the main process and pass the output to the child process using pipe.
3.      perform the operation given in the child process and print the output.
4.      stop the program.


PROGRAM:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
main()
{
 int p1[2],p2[2],p3[2],p4[2];
 int i,j=0,k=0,l=0;
 char r[10],s[10],t[10],u[10];
 printf("\t PROCESS 1.ENTER THE STRING");
 scanf("%s",r);
 pipe(p1);
 pipe(p2);
 write(p1[1],r,sizeof(r));
 write(p2[1],r,sizeof(r));
 int a=fork();
 if(a==0)
 {
  printf("\n\t PROCESS 2:it splits the given string\n");
  read(p1[0],r,sizeof(r));
  int n=strlen(r);
  for(i=0;i<n/2;i++)
  {
    s[i]=r[i];
  }
  for(i=n/2;i<=n;i++)
  {
   t[j++]=r[i];
  }
  pipe(p3);
  pipe(p4);
  write(p3[1],s,sizeof(s));
  write(p4[1],t,sizeof(t));
  int b=fork();
  if(b==0)
  {
   printf("p4 %d\t",getpid());
   printf("p2 %d\n",getppid());
   read(p3[0],s,sizeof(s));
   printf("\t PROCESS 4:sub string \t %s \t",s);
   printf("no of char=%d \n",strlen(s));
  }
  else
  {
    int c=fork();
    if(c==0)
  {
   printf("p5 %d\t",getpid());
   printf("p2 %d\n",getppid());
   read(p4[0],t,sizeof(t));
   printf("\t PROCESS 5:sub string \t %s \t",t);
   printf("no of char=%d \n",strlen(t));
  }
  else
  {
    wait();
    printf("p2 %d\t",getpid());
    printf("p1 %d\n",getppid());
  }   }}
else
{
  wait();
  int d=fork();
  if(d==0)
   {
     printf("p3 %d\t",getpid());
     printf("p1 %d\n",getppid());
     read(p2[0],r,sizeof(r));
     for(i=strlen(r)-1;i>=0;i--)
     {
       u[l++]=r[i];
     }
     for(i=0;i<strlen(r);i++)
     {
       if(u[i]==r[i])
       k++;
       else
       continue;
    }
     if(k==strlen(r))
     printf("\t PROCESS 3: the given string is palindrome\n");
     else
     printf("\t PROCESS 3: the given string is not palindrome\n");
    }
    else
    {
      printf("p1 %d\t",getpid());
      printf("kernal %d\t\n",getppid());
    }
 }}


OUTPUT:

            Process 1: enter the string  ARUN
            Process 2: it splits the string
            P4 8137 p2 8136
            Process 3: sub string a r no of char=2
            P5 8138 p2 8136
            Process 4; substring u n no of char=2
            P2 8136 p1 8132
            P3 8139 p1 8132
            Process 3: the given string is not palindrome
            Process 1: enter the string  MADAM
            Process 2: it splits the string
            P4 8137 p2 8136
            Process 4: sub string m a no of char=2
            P5 8138 p2 8136
            Process 5: sub string dam no of char=3
            P2 8136 p1 8132
            P3 8139 p1 8132
            Process 3: the given string is palindrome

0 comments:

Post a Comment