If we look at the example
#include <stdio.h>
#include <unistd.h>
void main(){
int pi_d ;
int pid ;
pi_d = fork();
if(pi_d == 0){
printf("Child Process B:\npid :%d\nppid:%d\n",getpid(),getppid());
}
if(pi_d > 0){
pid = fork();
if(pid > 0){
printf("\nParent Process:\npid:%d\nppid :%d\n",getpid(),getppid());
}
else if(pid == 0){
printf("Child Process A:\npid :%d\nppid:%d\n",getpid(),getppid());
}
}
}
For me, this looks like it would create processes indefinitely because, when we fork a process, a copy of the parent is made. So the program code is cloned.
This means that every new process runs the same code; thus, it calls pi_d = fork()
, and so on.
What I'm missing here?
fork()
call, not from the beginning.