int main()
{
int i;
int pfork(int);
printf("Hello gud morning \n");
printf(" i : %d ",i);
i=pfork(3);
if(i==0)
{
printf(")Parent \n");
printf("i when 0 \n ");
printf("Getpid : %d getppid : %d\n",getpid(),getppid());
printf("\n");
}
elseif(i==1)
{
printf("Child \n");
printf("i when 1 \n");
printf("Getpid : %d getppid : %d\n",getpid(),getppid());
printf("\n");
}
elseif(i==2)
{
printf("Child \n");
printf("i when 2 \n");
printf("Getpid : %d getppid : %d\n",getpid(),getppid());
printf("\n");
}
else
{
}
}
int pfork(int x)
{
int j;
for(j=1;j<x;j++)
{
if(fork()==0)
{
//sleep(3);return j;
}
}
return 0;
}
Output:
[04mca8@LINTEL pp]$ ./a.out
Hello gud morning
i : 134514128 Child
i when 1
Getpid : 28864 getppid : 28863
i : 134514128 Child
i when 2
Getpid : 28865 getppid : 28863
i : 134514128 )Parent
i when 0
Getpid : 28863 getppid : 5656