我有了這個程序,它創建一個孤立的過程,但我不太明白的控制流程是控制流量,從而由它給出的輸出:這個節目
#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