Wednesday, October 30, 2013

IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.

/*

TITLE: IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.
*/




#include<stdio.h>
#include<sys/wait.h>

int main()
{
      int pid,status,n1,n2,sum;
      printf("\n parent(user) program is in execution");
      pid=fork();
      printf("\n spanning the child process");
      if(pid==0)
      {
           printf("\n Child is executing :\n");
           printf("\n Enter two no.:\n");
           scanf("%d  %d",&n1,&n2);
           sum=n1+n2;
           printf("the sum is: %d\n",sum);
           printf(" pid of child is: %d\n",getpid());
           printf("pid of parent is: %d\n",getppid());
           printf("\n exiting child \n");

      }
    else
     {
       wait(&status);
              printf("\n parent is executing: \n");
           printf("\n pid of parent is :%d\n",getpid());
           printf("\n pid of parent's parent is: %d\n ",getppid());

         
           printf("\n exiting parent \n");
     }
   return 0;
}
/*
 parent(user) program is in execution
 spanning the child process
 Child is executing :

 Enter two no.:
 parent(user) program is in execution
20
14
the sum is: 34
 pid of child is: 4874
pid of parent is: 4873

 exiting child
 spanning the child process
 parent is executing:

 pid of parent is :4873

 pid of parent's parent is: 4857

 exiting parent
*/

No comments:

Post a Comment