#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/resource.h>
int t;
int a=20,b=10,sum,sub,mul;
int child1()
{
printf("child1 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process1:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process1, time delay:%d\n",t);
sleep(t);
sum=a+b;
return sum;
printf("child1 process is exiting..\n");
}
int child2()
{
printf("child2 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process2:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process2, time delay:%d\n",t);
sleep(t);
sub=a-b;
return sub;
printf("child2 process is exiting..\n");
}
int child3()
{
printf("child3 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process3:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process3, time delay:%d\n",t);
sleep(t);
mul=a*b;
return mul;
printf("child3 process is exiting..\n");
}
int parent()
{
int status;
printf(" in parent process...\n");
wait(&status);
status=status>>8;
printf("first termionating is:%d\n",status);
printf("parent process is exiting...\n");
}
main()
{
if(fork())
{
if(fork())
{
if(fork())
parent();
else
printf("multiplication=%d\n",child3());
}
else
printf("substraction=%d\n",child2());
}
else
printf("addition=%d\n",child1());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex16.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent process...
child3 id:4407 parent id:4404
in child process3:
in child process3, time delay:9
child2 id:4406 parent id:4404
in child process2:
child1 id:4405 parent id:4404
in child process1:
in child process1, time delay:1
in child process2, time delay:4
addition of two numbers=30
first termionating is:27
parent process is exiting...
madan@madan-Lenovo-G570:~/madan/osconcepts$ substraction=10
multiplication=200