Linux wait函数详解
wait和waitpid出现的原因
SIGCHLD
--当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止)
--子进程退出时,内核将子进程置为僵尸状态,这个进程成为僵尸进程,它只保留最小的一些内核数据结构,以便父进程查询子进程的退出状态
--父进程查询子进程的退出状态可以用wait/waitpid函数
wait获取staus后检测处理
宏定义 描述
WIFEXITED(status) 如果进程子进程正常结束,返回一个非零值
WEXITSTATUS(status) 如果WIFEXITED非零,返回子进程退出码
WIFSIGNALED(status) 子进程因为捕获信号而终止,返回非零值
WTERMSIG(status) 如果WIFSIGNALED非零,返回信号代码
WIFSTOPPED(status) 如果进程被暂停,返回一个非零值
WSTOPSIG(status) 如果WIFSTOPPED非零,返回信号代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h> int main(int arg,char *args[])
{
pid_t pid=fork();
if(pid==-)
{
printf("fork() failed ! error message:%s\n",strerror(errno));
return -;
}
if(pid>)
{
int status=;
printf("父进程\n");
wait(&status);
if(WIFEXITED(status))//WIFEXITED宏的释义: wait if exit ed
{
printf("子进程返回信息码:%d\n",WEXITSTATUS(status));
}else if(WIFSIGNALED(status))
{
printf("子进程信号中断返回信息码:%d\n",WTERMSIG(status));
}else if(WIFSTOPPED(status))
{
printf("子进程暂停返回信息码:%d\n",WSTOPSIG(status));
}else
{
printf("其他退出信息!\n");
}
}else if(pid==)
{
printf("i am child !\n");
abort();
//exit(100);
}
printf("game is over!\n");
return ;
}
wait()函数成功返回等待子进程的pid,失败返回-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h> int main(int arg, char *args[])
{
pid_t pid = ;
int i = , ret = ;
for (i = ; i < ; i++)
{
pid = fork();
if (pid == -)
{
printf("fork() failed ! error message:%s\n", strerror(errno));
return -;
}
if (pid == )
{
printf("child haved run!\n");
exit();
}
}
while ()
{
//wait()函数的返回值是子进程的pid
ret = wait(NULL);
printf("子进程pid=%d\n", ret);
if (ret == -)
{
//父进程wait()函数阻塞过程中,有可能被别的信号中断,需要做异常处理
if (errno == EINTR)
{
continue;
}
break;
}
}
printf("game is over!\n");
return ;
}
waitpid
函数功能:用来等待某个特定进程的结束
函数原型:
pid_t waitpid(pid_t pid, int *status, int options);
参数:
status如果不为空,会把状态信息写到它指向的位置
options允许改变waitpid的行为,最有用的一个选项是WNOHANG,它的作用是防止waitpid把调用者的执行挂起
返回值:成功返回等待子进程的pid,失败返回-
Linux wait函数详解的更多相关文章
- linux select函数详解
linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...
- linux select函数详解【转】
转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...
- Linux fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control 头文件: #include <unistd.h> #include ...
- Linux system函数详解
system 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型 int system(cons ...
- linux system()函数详解
system(3) - Linux man page Name system - execute a shell command Synopsis #include <stdlib.h> ...
- linux getopt函数详解
getopt(分析命令行参数) 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const ...
- Linux C popen()函数详解
表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...
- linux内核中send与recv函数详解
Linux send与recv函数详解 1.简介 #include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t n ...
- Linux环境fork()函数详解
Linux环境fork()函数详解 引言 先来看一段代码吧, 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include ...
随机推荐
- 常用jar包用途
jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期管理的常用方法. jaxrp ...
- HTML Basic Document and UML
HTML Basic Document <html> <head> <title>Document name goes here</title> < ...
- 3、软件评测师要阅读的书籍 - IT软件人员书籍系列文章
软件评测师在项目组的作用也是非常大的.微软的做法是一个软件工程师搭配两个软件评测师,这样能够减少软件系统存在的问题.但是,笔者发现,软件评测在这些年的发展还是比较缓慢的,除了人力进行的测试外,就是软件 ...
- C# 获取 新浪微博登录之后的 完整的Cookie
程序说明: 1.此项目 包含两个项目, 一个 Winform WinGetMocroblogCookie 用于手动 登录 新浪微博 其中涉及到的技术有: 使用webbrowser 获取HttpOnly ...
- BIEE 目录迁移(文件夹)方式
文件夹迁移方式一(归档--取消归档): 归档:analytics中选择目录,定位至指定文件夹,更多中选择归档,保存为 .catalog文档: 释放归档: 进入目录管理器,离线方式登陆, ...
- Mongodb Manual阅读笔记:MongoDB教程
Mongodb教程的说明,可以当手册用 Getting Started Install MongoDB on Linux Systems Install MongoDB on Red Hat Ente ...
- js 百度地图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 常用的js跳转页面方法实现汇总
1.window.location.href方式 <script language="javascript" type="text/javascript" ...
- 烂泥:apache性能测试工具ab的应用
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种设置不当所暴露出的问题. 性能测试 ...
- Linux磁盘管理之创建磁盘分区05
一.磁盘基础知识 磁盘安装在计算机上后,在系统读取到硬盘后并不能直接使用,必须经过分区.格式化才能够正确使用.这一次主要是针对磁盘分区进行简单总结,存储设备类型:U盘.光盘.软盘.硬盘.磁带. 硬盘接 ...