Inter Process Communication using Pipe - Sorting - Systems Lab

Program:
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
          
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }
   
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
           
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }


Output:
 nn@ubuntu:~$ gcc pb1.c
nn@ubuntu:~$ ./a.out
enter the limit
5
enter the numnbers5
4
3
2
1
5    4    3    2    1
nn@ubuntu:~$ gcc pb1.c

1 comment:

  1. Great Post!! tekv is an established systems automation group with over 75 combined years in providing end to end solutions, including warehouse automation, conveyors, fulfillment systems, software, and controls warehouse automation.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...