0
我感到困惑的无子进程也正在对执行我执行了下面的程序,但我很困惑它的输出?
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int i=0;
fork();
fork();
fork();
fork();
pid_t pid;
if((pid=fork()) == 0) {
printf("I am the child: %u\n", getpid());
}
else {
printf("I am the parent: %u and my child is: %u\n", getpid(),pid);
}
return 0;
}
能有人帮助我理解这是为什么有这么多的子进程,为什么父ID每次都是不同的。
我得到的输出是:
[email protected]:~/Desktop$ gcc -c fork.c
[email protected]:~/Desktop$ gcc -o fork fork.c
[email protected]:~/Desktop$ ./fork
I am the parent: 11842 and my child is: 11847
[email protected]:~/Desktop$ I am the child: 11847
I am the child: 11849
I am the parent: 11846 and my child is: 11850
I am the child: 11867
I am the child: 11869
I am the parent: 11843 and my child is: 11856
I am the child: 11850
I am the parent: 11848 and my child is: 11857
I am the parent: 11852 and my child is: 11860
I am the parent: 11845 and my child is: 11849
I am the parent: 11854 and my child is: 11866
I am the child: 11859
I am the child: 11856
I am the child: 11857
I am the parent: 11862 and my child is: 11869
I am the parent: 11865 and my child is: 11870
I am the child: 11860
I am the child: 11866
I am the parent: 11855 and my child is: 11867
I am the parent: 11851 and my child is: 11859
I am the parent: 11863 and my child is: 11871
I am the parent: 11858 and my child is: 11872
I am the parent: 11868 and my child is: 11873
I am the parent: 11844 and my child is: 11861
I am the child: 11870
I am the child: 11861
I am the child: 11864
I am the child: 11872
I am the child: 11871
I am the child: 11873
I am the parent: 11853 and my child is: 11864