AI智能
改变未来

fork() 创建子进程(二):父子进程wait()简单同步


动机

父子进程利用

exit()

wait()

进行简单同步控制。

示例1:简单同步控制

代码示例

#include <stdio.h>#include <stdlib.h>#include <sys/types.h>int main() {pid_t child1;pid_t child2;if ((child1=fork()) == -1)  {	// 创建失败printf(\"Fork Error.\\n\");exit(1);}if (child1 == 0)  {// 子进程1sleep(1);printf(\"************** Now it is in first child process.I am %d\\n\",getpid());printf(\"************** I am first child process. I\'m dying. I am %d\\n\",getpid());exit(0); // 注意下,注释与否,执行情况的差异。}else  {// 父进程printf(\"Now it is in parent process.I am %d\\n\",getpid());printf(\"I have first child %d.\\n\",child1);printf(\"I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.\\n\");wait();  // 注意下,注释与否,执行情况的差异。printf(\"my first child %d. died\\n\",child1);if ((child2=fork()) == -1)  {	// 创建失败printf(\"Fork Error.\\n\");exit(1);}if (child2 == 0)  {// 子进程2sleep(1);printf(\"************** Now it is in second child process.I am %d\\n\",getpid());printf(\"************** I am second child process. I\'m dying. I am %d\\n\",getpid());exit(0); // 注意下,注释与否,执行情况的差异。}else {// 父进程printf(\"Now it is in parent process.I am %d\\n\",getpid());printf(\"I have second child %d.\\n\",child2);printf(\"I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.\\n\");wait();  // 注意下, 注释与否,执行情况的差异。printf(\"my second child %d. died\\n\",child2);printf(\"I am parent process. I\'m dying. I am %d\\n\",getpid());}}printf(\"******* I am process. I\'m dying. I am %d\\n\",getpid());return 0;}

运行结果

[tj@tj bin]$ ./fork_child_waitNow it is in parent process.I am 17936I have first child 17937.I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.************** Now it is in first child process.I am 17937************** I am first child process. I\'m dying. I am 17937my first child 17937. diedNow it is in parent process.I am 17936I have second child 17938.I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.************** Now it is in second child process.I am 17938************** I am second child process. I\'m dying. I am 17938my second child 17938. diedI am parent process. I\'m dying. I am 17936******* I am process. I\'m dying. I am 17936[tj@tj bin]$

结果分析

屏幕上的输出是父进程和子进程的同步协调交替输出。

结果

2,3,4

行是

父进程

输出,之后,

父进程

就因为执行第

27

行的

wait

而陷入阻塞。
结果

5,6

行是

子进程1

输出,之后,

子进程1

就因为执行第

20

行的

exit

而结束,同时唤醒了

父进程


结果

7

行是

父进程

子进程1

唤醒后,执行第

27

行的

wait

语句后的第

28

行输出。

结果

8,9,10

行是

父进程

输出,之后,

父进程

就因为执行第

47

行的

wait

而陷入阻塞。
结果

11,12

行是

子进程2

输出,之后,

子进程2

就因为执行第

40

行的

exit

而结束,同时唤醒了

父进程


结果

13

行是

父进程

子进程2

唤醒后,执行第

47

行的

wait

语句后的第

48

行输出。

结果

14,15

行是

父进程

输出。

示例2:没有同步控制

代码示例

#include <stdio.h>#include <stdlib.h>#include <sys/types.h>int main() {pid_t child1;pid_t child2;if ((child1=fork()) == -1)  {	// 创建失败printf(\"Fork Error.\\n\");exit(1);}if (child1 == 0)  {// 子进程1sleep(1);printf(\"************** Now it is in first child process.I am %d\\n\",getpid());printf(\"************** I am first child process. I\'m dying. I am %d\\n\",getpid());//exit(0); // 注意下,注释与否,执行情况的差异。}else  {// 父进程printf(\"Now it is in parent process.I am %d\\n\",getpid());printf(\"I have first child %d.\\n\",child1);printf(\"I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.\\n\");//wait();  // 注意下,注释与否,执行情况的差异。printf(\"my first child %d. died\\n\",child1);if ((child2=fork()) == -1)  {	// 创建失败printf(\"Fork Error.\\n\");exit(1);}if (child2 == 0)  {// 子进程2sleep(1);printf(\"************** Now it is in second child process.I am %d\\n\",getpid());printf(\"************** I am second child process. I\'m dying. I am %d\\n\",getpid());//exit(0); // 注意下,注释与否,执行情况的差异。}else {// 父进程printf(\"Now it is in parent process.I am %d\\n\",getpid());printf(\"I have second child %d.\\n\",child2);printf(\"I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.\\n\");//wait();  // 注意下, 注释与否,执行情况的差异。printf(\"my second child %d. died\\n\",child2);printf(\"I am parent process. I\'m dying. I am %d\\n\",getpid());}}printf(\"******* I am process. I\'m dying. I am %d\\n\",getpid());return 0;}

运行结果

[tj@tj bin]$ ./fork_child_no_waitNow it is in parent process.I am 18035I have first child 18036.I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.my first child 18036. diedNow it is in parent process.I am 18035I have second child 18037.I\'ll wait for the termination of my child. Before that, I\'m going to go into a blocking state.my second child 18037. diedI am parent process. I\'m dying. I am 18035******* I am process. I\'m dying. I am 18035[tj@tj bin]$ ************** Now it is in first child process.I am 18036************** I am first child process. I\'m dying. I am 18036******* I am process. I\'m dying. I am 18036************** Now it is in second child process.I am 18037************** I am second child process. I\'m dying. I am 18037******* I am process. I\'m dying. I am 18037

结果分析

屏幕上的输出是父进程和子进程的没有同步协调的交替输出。

结果

2,3,4

行是

父进程

输出,
结果

5

行是

父进程

输出,但其实子进程1现在还活着。

结果

6,7,84

行是

父进程

输出,
结果

9

行是

父进程

输出,但其实子进程2现在还活着。

结果

10,11

行是

父进程

输出,之后,父进程结束。

结果

12

行由于

父进程

结束,所以回到系统提示符

[tj@tj bin]$

输出,之后的输出,是

子进程1

的输出。

结果

13,14

行是

子进程1

输出,然后,

子进程1

结束。

结果

15,16,17

行是

子进程2

输出,然后,

子进程2

结束。

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » fork() 创建子进程(二):父子进程wait()简单同步