The process created:
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<wait.h>intMain () {intPid=fork ();//Create Child process if(pid==0) {printf ("d:%d\t%d\n", Getppid (), Getpid ());return 0;//Child process End } intPid2=fork ();//Create Child process if(pid2==0) {printf ("c:%d\t%d\n", Getppid (), Getpid ()); Waitpid (Pid2,null,0);return 0;//Child process End } intpid3=Fork (); if(pid3==0)//Create Child process{printf ("b:%d\t%d\n", Getppid (), Getpid ());return 0;//Child process End} printf ("Parent Process ID Current process id\n"); printf ("a:%d\t%d\n", Getppid (), Getpid ()); Waitpid (Pid,null,0); Waitpid (Pid2,null,0); Waitpid (Pid3,null,0);//wait for the child process to end; return 0;}
The results are as follows:
parent Process ID current process ida:3195 6051 D: 6051 6052 C: 6051 6053 B: 6051 6054
The process created:
#include <stdio.h>#include<unistd.h>#include<wait.h>#include<stdlib.h>intMain () {intPid=fork ();intpid3; if(pid==0) {printf ("c:%d\t%d\n", Getppid (), Getpid ());return 0; } intPid2=Fork (); if(pid2==0) {PID3=Fork (); if(pid3==0) {printf ("d:%d\t%d\n", Getppid (), Getpid ());return 0; } Else{printf ("b:%d\t%d\n", Getppid (), Getpid ());return 0; }} printf ("Parent Process ID Current process id\n"); printf ("a:%d\t%d\n", Getppid (), Getpid ()); Waitpid (Pid,null,0); Waitpid (Pid2,null,0); Waitpid (Pid3,null,0); return 0; }
The results are as follows:
parent Process ID current process ida:3195 6071 C: 6071 6072 B: 6071 6073 D: 6073 6074
Creating child processes with fork