Can anyone explain why the line containing "here" is executed 5 times and how exactly the program runs because I don't seem to understand how I get this output
Output:
12958: 0 here
12959: 0
12958: 0 here
12958: 1 here
12960: 1
12958: 0 here
12958: 1 here
Code:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(){
int i;
for(i=0; i<2; i++){
printf("%d: %d here\n", getpid(), i);
if(fork()==0){
printf("%d: %d\n", getpid(), i);
exit(0);
}
}
for(i=0; i<2; i++){
wait(0);
}
return 0;
}
Edit: because I'm running windows on my computer I used this website link to check the code, could that be a problem?