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 ...
随机推荐
- iOS之 FBMemoryProfiler FB的循环引用检测工具
经过两天的google终于搞定了FBMemoryProfiler这个开源检测循环引用的工具.中间的曲折也是让人头疼,言归正传直接说一下这个memoryProfiler github:https://g ...
- redis如何执行redis命令
Redis 命令 Redis 命令用于在 redis 服务上执行操作.所以我们必须要启动Redis服务程序,也就是redis安装目录下的redis-server.exe,你可以双击执行,也可以打开cm ...
- rsa && sha1 js code
jsbn.js /* * Copyright (c) 2003-2005 Tom Wu * All Rights Reserved. * * Permission is hereby granted, ...
- 试用 Nexus OSS 3.0 的docker仓库 (二)
试用 Nexus OSS 3.0 的docker仓库 (一) : http://www.cnblogs.com/wzy5223/p/5410990.html 三. 创建docker私有仓库,docke ...
- Java开发人员最常犯的10个错误
这个列表总结了10个Java开发人员最常犯的错误. Array转ArrayList 当需要把Array转成ArrayList的时候,开发人员经常这样做: List<String> list ...
- oracle忘记sys/system/scott用户密码了,如何重置oracle密码?
今天用到的oracle数据库,但是发现以前设置的密码,忘记了,怎么输入都不对,所以从网上找了一下资料,解决了,然后整理分享给大家. 一.遇到的问题: 1..忘记除SYS.SYSTEM用户之外的用户的登 ...
- my_log
//功能:定时向文件中写入时间,如果进程终止,下次打开同一个文件会继续之前的编号书写 #include<stdio.h> #include<time.h> int main(i ...
- C++ 中 typename
声明template参数时, 前缀关键字class和typename可以互换; 使用关键字typename标识嵌套从属类型名称, 但不需在基类列表和成员初始化列表内使用. 从属名称(dependent ...
- webkit浏览器常见开发问题
前段时间有人问我一个简单的问题,html如何创建解析的? 我讲了一大堆,什么通过DocumentLoader, CachedResourceLoader, CacheResource, Resourc ...
- (转)linux下vi命令大全
http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n ...