wait系列
转自 http://blog.csdn.net/todd911/article/details/15028511
1.wait函数和waitpid函数
当一个进程正常或异常终止时,内核就向其父进程发送SIGCHLD信号。因为子进程终止是个异步事件,所以这种信号也是内核向父进程发的异步通知。父进程可以选择忽略信号,或者提供一个该信号的处理函数,对于这种信号默认动作是忽略它。调用wait或waitpid的进程可能会发生什么情况:
- 如果其所有子进程都在运行,则阻塞。
- 如果一个子进程已经终止,正等待父进程获取其终止状态,则取得该子进程的终止状态后返回。
- 如果它没有任何子进程,则立即出错返回。
#include <sys/wait.h>
pid_t wait(int *statloc); //如果成功返回进程ID,0,如果出错返回-1.
pid_t waitpid(pid_t pid, int *statloc, int options); //如果成功返回进程ID,0(使用WNOHANG选项),如果出错返回-1.
这两个函数的区别是:
- 在一个子进程终止前,wait使其调用者阻塞,而waitpid有一个选项,可使调用者不阻塞。
- waitpid并不等待在其调用之后的第一个终止子程序,它有若干个选项,可以控制它所等待的进程。
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h> void pr_exit(int status){
printf("status = %d\n", status);
if(WIFEXITED(status)){
printf("normal terminaton, exit status = %d\n", WEXITSTATUS(status));
}else if(WIFSIGNALED(status)){
printf("abnormal termination, signal number = %d%s\n",WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status)?"(core file generated)" : "");
#else
"");
#endif
}else if(WIFSTOPPED(status)){
printf("child stopped, signal number = %d\n", WSTOPSIG(status));
}
} int main(void){
pid_t pid;
int status; if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
exit();
}
if(wait(&status) != pid){
perror("wait");
return -;
}
pr_exit(status); if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
abort();
}
if(wait(&status) != pid){
perror("wait");
return -;
}
pr_exit(status); if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
status /= ;
}
if(wait(&status) != pid){
perror("wait");
return -;
}
pr_exit(status); return ;
}
status = 1792
normal terminaton, exit status = 7
status = 134
abnormal termination, signal number = 6(core file generated)
status = 136
abnormal termination, signal number = 8(core file generated)
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h> int main(void){
pid_t pid;
int status; if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
sleep();
_exit();
} while(waitpid(pid,&status,WNOHANG) == ){
printf("no terminated process.\n");
sleep();
}
printf("child terminated.\n");
return ;
}
no terminated process.
no terminated process.
no terminated process.
no terminated process.
no terminated process.
child terminated.
#include <sys/wait.h>
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
//若成功则返回0,出错则返回-1.
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h> int main(void){
pid_t pid, pid2;
siginfo_t info; if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
sleep();
_exit();
}
while((pid2 = waitid(P_PID,pid,&info,WNOHANG)) == -){
printf("no terminated process %d.\n",pid2);
sleep();
} return ;
}
no terminated process -1.
no terminated process -1.
no terminated process -1.
no terminated process -1.
no terminated process -1.
no terminated process -1.
yan 5148 0.0 0.0 0 0 pts/2 Z 14:10 0:00 [a.out] <defunct>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
pid_t wait3(int *statloc, int options, struct rusage *rusage);
pid_t wait4(pid_t pid, int *statloc, int option, struct rusage *rusage);
//两个函数返回值,若成功,则返回进程ID,若出错,则返回-1.
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h> int main(void){
pid_t pid;
if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid == ){
if((pid = fork()) < ){
perror("fork");
return -;
}else if(pid > ){
exit();
}else{
sleep();
printf("second child,parent pid = %d\n",getppid());
exit();
}
} if(waitpid(pid,NULL,) != pid){ //回收第一次fork的子进程
perror("waitpid");
return -;
}
return ;
}
yan@yan-vm:~/apue$ second child,parent pid = 1
wait系列的更多相关文章
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- Angular杂谈系列1-如何在Angular2中使用jQuery及其插件
jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 03.SQLServer性能优化之---存储优化系列
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 概 述:http://www.cnblogs.com/dunitian/p/60413 ...
- Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数
上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...
- Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数
上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...
- Angular2入门系列教程4-服务
上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- 【疯狂造轮子-iOS】JSON转Model系列之一
[疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...
随机推荐
- linux中部署jenkins(war包)及jenkins忘记登录账号密码
未登录状态 登录状态 一:部署jenkins(war包) 1.直接下载war包jenkins.war,下载地址https://jenkins.io/download 2.将下载的war包放到服务器上t ...
- MySQL SELECT表达式的执行顺序是从左往右依次执行
例子如下:(确保这几个变量都是初次使用,因为mysql的用户自定义变量会在整个连接session中存在) ,,; +--------+-------+---------+-------+ | +--- ...
- (五)web服务中的异常处理
一.服务端发布服务 package com.webservice; import javax.jws.WebParam; import javax.jws.WebResult; import java ...
- Eigen 学习笔记
1. 初始化 //外部指针初始化 ]={...}; ] = ...; kernels[].mu = Vector3d(_mu0); kernels[].sigma_inv = Matrix3d(_s ...
- vscode 基本知识以及如何配置 C++ 环境
参考: 在用VSCode? 看完这篇文章, 开发效率翻倍!最后一条厉害了~ Visual Studio Code(VS code)你们都在用吗?或许你们需要看一下这篇博文 按下 ctrl+K,再按下 ...
- TypeScript入门七:TypeScript的枚举
关于枚举 数字枚举 字符串枚举 异构枚举 计算的和常量成员 运行时的枚举与反向映射 常量枚举与外部枚举 一.关于枚举 枚举:一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计 ...
- JavaScript 和 Java 是完全不同的语言
JavaScript 和 Java 是完全不同的语言这个是定论,两者是概念http://www.gzaos.com还是设计都不同. JavaScript 在 1995 年由 Brendan Eich ...
- js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息
文件名的传递 ---全路径获取 $('#file').change(function(){ $('#em').text($('#file').val()); }); 文件名的传递 ---只获取文件名 ...
- Button控件的三种点击事件
①在布局文件中指定onClick属性的方法设置点击事件 ②使用匿名内部类的方法设置点击事件 ③实现Activity实现OnClickListen接口的方式设置点击事件 linear.xml文件 < ...
- 文本三剑客之grep及正则表达式
1.grep 1. 什么是grep.egrep和fgrep Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来(匹配到的标红).grep全称是Glo ...