Write a program that finds the difference between the sphere volume and cube volume.  Create a 3 processes, one parent and two child.  Parent process takes related data from user and sends them to the related child process using pipe. Each child calculates one shape volume and sends the result to the parent process. Then parent process takes calculated volumes’ data from child processes then finds the difference between them. Finally it displays the results on the screen.

......

int main(){

        const int pi=3;

        int pid1, pid2, id1[2],id2[2], radius, side,volume1,volume2,diff, status;

 

        pipe(id1);

        pipe(id2);

 

        //create first child process

        if(pid1==0){

       

 

       

 

  

        }

       

       //create second child process

        if(pid2==0){

 

 

 

 

       

        }

       

       printf(“Enter the side of the cube: ”);

       scanf(“%d”,&side);   

       printf(“Enter the radious of the sphere”);

       scanf(“%d”,&radius);

       // send data to the child processes

 

     

 

                

      //recive data from child processes

 

 

 

  

      //calculate differenece        

 

 

      //display result

      printf(“The diffrence between two volumes is %d \n”,diff);

      wait(&status);       

}

 

Answer

#include <stdlib.h>

#include <stdio.h>

 

int main(){

        const int pi=3;

        int pid1, pid2, id1[2],id2[2];

                       

                        typedef struct{

                            int side;

                                   int control;                            

                                   int volume;

                        }shape;

 

                        shape s1,s2;

 

                        int diff, status;

       

        pipe(id1);           

        pipe(id2);

                       

        //create first child process

                        pid1=fork();

        if(pid1==0){

                                    read(id1[0],s2,sizeof(&s2));

                                    s2.volume=s2.side*s2.side*s2.side;

                                    s2.control=1;

                                    write(id1[1],s2,sizeof(&s2));                                  

        }

                        else{

            //create second child process

                                   pid2=fork();

                                   if(pid2==0){

                                                read(id2[0],s1,sizeof(&s1));

                                                s1.volume=(4/3)*pi*s1.side*s1.side*s1.side;

                                                s1.control=1;

                                    write(id2[1],s1,sizeof(&s1));     

                                   }

                                   else{

       

                                               printf("Enter the side of the cube: \n");

                                               scanf("%d",&s2.side);   

                                               printf("Enter the radius of the sphere \n");

                                               scanf("%d",&s1.side);

                                               // send data to the child processes

                                               printf("send data to the first child\n");

                                               write(id1[1],s2,sizeof(&s2));

                                               printf("send data to the second child\n");

                                               write(id2[1],s1,sizeof(&s1));

                 

                                       //recive data from child processes

                                               printf("recive data from first child \n");

                                               read(id1[0],s2,sizeof(&s2));

                                               printf("recive data from second child \n");

                                               read(id2[0],s1,sizeof(&s1));

                                               printf("controlling...%d,%d\n",s2.control,s1.control);

                                               while(s1.control!=1 || s2.control!=1); 

                                               //calculate differenece          

                                               diff=s2.volume - s1.volume;

                                //display result

                                               printf("The diffrence between two volumes is %d \n",diff);

                                               wait(&status);       

                                               close(id1);

                                               close(id2);

                                   }

                        }

return 0;

}