Thursday, 4 December 2014

Program to implement fork system call in UNIX


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
if(fork())
printf("parent=%d bash=%d\n",getpid(),getppid());
else
if(fork())
printf("child4==%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child3=%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child2=%d parent=%d\n",getpid(),getppid());
else
printf("child1=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex26.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=4954 bash=2817
madan@madan-Lenovo-G570:~/madan/osconcepts$ child4==4955 parent=1144
child3=4956 parent=4955
child2=4957 parent=1144
child1=4958 parent=4957