我有了这个程序,它创建一个孤立的过程,但我不太明白的控制流程是控制流量,从而由它给出的输出:这个节目
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t p;
/* create child process */
p=fork();
if(p==0) {
/* fork() returns Zero to child */
sleep(10);
}
printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
/*parent/child waits for 20 secs and exits*/
sleep(20);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());
return 0;
}
输出:
[email protected]:~/ds/unix$ ./a.out
The child process pid is 2575 parent pid 1922
The child process pid is 2576 parent pid 2575
Process 2575 is done its Parent pid 1922...
[email protected]:~/ds/unix$
Process 2576 is done its Parent pid 1...
所以它首先生成'子进程......',然后睡10秒。并再次执行'子进程等等'。再次看到事情发生'父母完成....'
请帮助我,即使这个问题看起来很愚蠢。
请解释一下这个程序是如何工作的? – 2014-10-08 00:34:41
这个程序的哪部分你不明白? – 2014-10-08 00:36:12
我不明白,执行程序1st给出if语句后的第一行,然后睡10秒,再次创建一个新的孩子睡10秒,然后以类似的方式执行第二个printf语句。 – 2014-10-08 00:40:20