Featured Post

Friday, December 30, 2016

C program to demonstrate the use of exit and wait system call


/* C program that accepts two small numbers(<50) as arguments and sums the two in a child process. The sum should be returned by the child to the parent as its exit status and the parent should print the sum.                                       */

                           /*sum.c*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
  int childpid;
  int n1,n2,n3;
  if(argc<3)
  {
     printf("\n give two numbers at command prompt to add");
     exit(1);
  }
  n1=atoi(argv[1]);
  n2=atoi(argv[2]);
  if(fork()==0)
  {
     n3=n1+n2;
     exit(n3);   /* returns n3 value as exit status to parent process*/
  }
  else
  {
     wait(&n3);  /* parent process suspends execution until child returns exit status*/
     printf("\n addition of %d and %d is %d",n1,n2,n3);
  }
}

Output:
          /home/kumar/~$ cc sum.c
          /home/kumar/~$ ./a.out 2 3

          addition of 2 and 3 is 5


C program to demonstrate the use of exit and wait system callfork() system call, wait() system call, exec() system call, exit() system call, fork,wait,exit,exec system calls. creating child process/* C program that accepts two small numbers(<50) as arguments and sums the two in a child process. The sum should be returned by the child to the parent as its exit status and the parent should print the sum.                                       */

No comments:

Post a Comment