所以我有以下C代码:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int i = 0, n;
n = 5;
pid_t pid;
printf("i=%d Right before the loop\n", i, getpid(), getppid());
for (i = 0; i < n; i++){
pid = fork();
if (pid <= 0){
printf("something happens in loop #%d. pid = %d\n", i, pid);
break;
}
printf("End of loop #%d\n", i);
}
printf("i=%d My process ID = %d and my parent's ID = %d\n", i, getpid(), getppid());
return 0;
}
我只有一个问题:为什么
printf("i=%d My process ID = %d and my parent's ID = %d\n", i, getpid(), getppid());
像在循环内一样被执行多次?我试图通过很多方法弄清楚,但我找不到原因。