进程学习5--父进程wait回收子进程
来源:互联网 发布:数据库表显示使用中 编辑:程序博客网 时间:2024/05/20 19:46
概述
wait的工作原理
(1)子进程结束时,系统向其父进程发送SIGCHILD信号
(2)父进程调用wait函数后阻塞
(3)父进程被SIGCHILD信号唤醒然后去回收僵尸子进程
(4)父子进程之间是异步的,SIGCHILD信号机制就是为了解决父子进程之间的异步通信问题,让父进程可以及时的去回收僵尸子进程。
(5)若父进程没有任何子进程则wait返回错误
正文
函数
函数原型wait
pid_t wait(int *status);
参数
参数status用来返回子进程结束时的状态,父进程通过wait得到status后就可以知道子进程的一些结束状态信息。
WIFEXITED、WIFSIGNALED、WEXITSTATUS这几个宏用来获取子进程的退出状态。
WIFEXITED宏用来判断子进程是否正常终止(return、exit、_exit退出)
WIFSIGNALED宏用来判断子进程是否非正常终止(被信号所终止)
WEXITSTATUS宏用来得到正常终止情况下的进程返回值的。
返回值
返回本次回收的子进程的PID号
测试代码
int main(void){ pid_t Mypid =-1; Mypid = fork(); if(Mypid>0)/*父进程*/ { pid_t Mypid2 = -1; int status = -1; Mypid2 = wait(&status);/*阻塞*/ printf("son is recycled Mypid2 = %d\r\n",Mypid2); printf("exit true? %d\n", WIFEXITED(status)); printf("exit false? %d\n", WIFSIGNALED(status)); printf("exit val %d.\n", WEXITSTATUS(status)); } else if(Mypid == 0)/*子进程*/ { printf("son progress.Mypid = %d\r\n",getpid()); return 22; } return 0;}/************运行结果**************** son progress.Mypid = 10558 son is recycled Mypid2 = 10558 exit true? 1 exit false? 0 exit val 22.************************************/
参考
朱老师Linux教程文档
阅读全文
0 0
- 进程学习5--父进程wait回收子进程
- 父进程wait回收子进程
- 【Linux】回收子进程—wait/waitpid
- Linux进程全解7——父进程wait / waitip回收子进程
- wait()函数,作用:阻塞等待任意子进程,回收子进程8kb物理内存
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程与子进程 wait, WIFEXITED, WEXITSTATUS 函数
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程等待子进程结束 waitpid wait
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 父进程 子进程
- 父进程子进程
- WebRoot与WEB-INF目录下的资源的不同
- android XUtils3框架的基本使用
- Playonlinux下Tim的安装
- 3DES 和 ECB CBC 加密方式
- byte、short、int、long
- 进程学习5--父进程wait回收子进程
- android 之 ListView相关
- React Native实战项目
- React-native外部引入样式
- android 按键
- centos 安装pip 报错
- 经典快速排序法,利用递归将数据排序
- 表示数值的字符串(Java实现)
- Bluemix 之 IBM Watson Conversation 自然对话实践