This is the program I wrote
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<iostream>
#include<wait.h>
int main(void){
std::cout << "My process " << getpid() << std::endl;
int i;
for(i=0;i<2;i++){
int j = fork();
wait(NULL);
std::cout << "Process id :" << getpid() <<" and parent:"<< getppid()<< " and value returned is " << j <<std::endl;
}
return 0;
}
This is the output I get :
My process 5501
Process id :5502 and parent:5501 and value returned is 0
Process id :5503 and parent:5502 and value returned is 0
Process id :5502 and parent:5501 and value returned is 5503
Process id :5501 and parent:2828 and value returned is 5502
Process id :5504 and parent:5501 and value returned is 0
Process id :5501 and parent:2828 and value returned is 5504
Could someone explain the output to me? The intention of the program was that processes would be "visited" in a DFS manner. However, I'm not understanding how the value returned is 5503 in the third line and also why, even though I'm running the loop only twice, is 5504 being created? Thanks in advance.