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. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...
随机推荐
- react封装通用tab组件
import React, { Component } from 'react' import PropTypes from 'prop-types' import _ from 'lodash' i ...
- Abp SSO
官方的文档有个坑. 首先建立的应该是 .net core MPA版本. 把文档上的startup.cs配置写入 MVC 项目中. 这样测试才能通过.不然,测试项目 var disco = a ...
- 1 Refused to display ‘url’ in a frame because it set 'X-Frame-Options' to 'sameorigin' 怎么解决?
进在开发公司的文件中心组件,提供各个子系统的附件上传下载.预览.版本更新等功能,前端在今天突然给我发一张图,说预览缩略图遇到问题了,然后发了个截图给我: 这很明显是一个跨域问题, X-Frame-Op ...
- windows下监控进程的脚本
相信大家都有这样的需求,某程序(进程)在运行的时候可能挂掉,需要去监控该程序,并在它挂掉的时候重启之,确保该程序能一直运行.比如土net就经常挂,需要监控程序去监控.Linux下面似乎有守护进程的概念 ...
- [书籍翻译] 《JavaScript并发编程》第六章 实用的并发
本文是我翻译<JavaScript Concurrency>书籍的第六章 实用的并发,该书主要以Promises.Generator.Web workers等技术来讲解JavaScript ...
- React/动态绑定class
第一种 字符串拼接 <i className={["iconfont"+" "+item.icon]} ></i> 第二种 有判断条件的 ...
- mysql 创建用户,授权,查询用户等
MySQL创建用户与授权 一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用 ...
- 一步一步教你实现iOS音频频谱动画(一)
如果你想先看看最终效果再决定看不看文章 -> bilibili 示例代码下载 第二篇:一步一步教你实现iOS音频频谱动画(二) 基于篇幅考虑,本次教程分为两篇文章,本篇文章主要讲述音频播放和频谱 ...
- EBS R12.2系统logo的修改
https://blog.csdn.net/lzl1101206656/article/details/74171999 EBS系统logo的修改 转载lzl1101206656 发布于2017-07 ...
- Flutter——CircleAvatar组件(圆形头像组件)
import 'package:flutter/material.dart'; import 'res/listData.dart'; void main() { runApp(MaterialApp ...