10_6检测子进程状态变化的处理函数。所以确实有这种子进程 错误
来源:互联网 发布:unity3d回合制游戏 编辑:程序博客网 时间:2024/04/30 17:51
因为当已经进入信号处理函数里,说明在子进程里已经执行了_exit(0),在信号处理函数里注册信号SIGCLD的处理函数时,虽然会检测是否有需要的子进程,可是结果没有,因为已经执行_exit(0);所以不会死循环。书中错。
一、源代码:
cat -n 10_6.c
1 #include "apue.h"
2 #include <sys/wait.h>
3
4 static void sig_cld(int);
5
6 int main()
7 {
8 pid_t pid;
9 if ((pid = fork()) <0 ){
10 err_sys("fork error");
11 }
12 if (signal(SIGCHLD,sig_cld) == SIG_ERR){
13 err_sys("signal SIGCLD handler establish error");
14 }
15
16 //if ((pid = fork()) <0 ){
17 // err_sys("fork error");
18 //}
19 if (pid == 0){
20 sleep(2);
21 _exit(0);
22 }
23
24 pause();
25 exit(0);
26 }
27
28 static void sig_cld (int signalno)
29 {
30 pid_t pid;
31 int status;
32 printf("signal SIGCHLD received.\n");
33 if (signal(SIGCHLD,sig_cld) == SIG_ERR){
34 err_sys("signla SIGCLD handler establish error.");
35 }
36
37 if ((pid = wait(&status)) < 0){
38 err_sys("wait child error.");
39 }
40
41 printf("pid = %d\n",pid);
42
43 }
二、编译及执行结果
:gcc -Wall -gdb3 -o handler_sigcld 10_6.c
cc1: warning: `-gdb3' not supported by this configuration of GCC
In file included from apue.h:132,
from 10_6.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
10_6.c: In function `sig_cld':
10_6.c:41: warning: int format, pid_t arg (arg 2)
./handler_sigcld
signal SIGCHLD received.
pid = 24916
- 10_6检测子进程状态变化的处理函数。所以确实有这种子进程 错误
- 检测子进程的结束返回状态
- Linux并发(子进程退出状态的处理)
- Linux并发(子进程退出状态的处理)
- Linux 进程--父进程查询子进程的退出状态
- Linux 进程--父进程查询子进程的退出状态
- 4.3.3.3 master_status_event函数:父进程读status_fd管道,更新其记录的子进程状态
- 创建子进程函数
- 父进程异常退出时, 杀死所以子进程
- 10_26 当调用system函数时子进程与父进程对信号的处理,尤其是SIGCHLD
- 子进程在复制父进程的信号处理方式
- 子进程在复制父进程的信号处理方式
- 僵死进程、子进程退出的异步善后处理
- 子进程的创建
- 子进程的创建
- 子进程的个数
- 子进程
- 子进程
- 工厂类理论复习
- 隐式Intent
- 截取两个字符串中间的值
- OC-002.OC中如何创建一个类
- Android 属性动画(Property Animation)
- 10_6检测子进程状态变化的处理函数。所以确实有这种子进程 错误
- 互联网时代的用户阅读习惯是什么样的?
- Apache JMeter--压力测试工具
- 【Struts2】(7)ModelDriven和类型转换器
- 整合大量开源库温习基础项目(四)伸缩实现OverscrollScale以及MaterialLoadingProgressBar
- CSS3颜色格式和透明度
- 创建数据库,并定义ContentProvider接口
- 【剑指offer】之二叉树深度
- PCA主成分分析