当子进程先于父进程退出时,如果父进程没有调用wait和waitpid函数,子进程就会进入僵死状态。

pid_t wait(int *status);

pid_t waitpid(pid_t pid, int *status, int options);

----------------------------------------------------------------------------------------------------------

The wait() system call suspends execution of the calling process until one of its children terminates. The call wait(&status) is equivalent to:

waitpid(-1, &status, 0);

wait函数使父进程暂停执行,直到他的一个子进程结束为止。返回值为结束的子进程的进程ID

参数status存放子进程的退出码,即子进程main函数的返回值,或者子进程exit函数的参数。

如果status不是一个空指针,状态信息将被写入它指向的变量。

在手册中有解读进程退出状态的宏,举例:

WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

----------------------------------------------------------

The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but
this behavior is modifiable via the options argument, as described below.

参数说明:

pid:指明要等待的子进程的进程ID

< -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid.

-1 meaning wait for any child process.

0 meaning wait for any child process whose process group ID is equal to that of the calling process.

> 0 meaning wait for the child whose process ID is equal to the value of pid.

status:

options:允许用户改变waitpid的行为:

The value of options is an OR of zero or more of the following constants:

WNOHANG return immediately if no child has exited.

WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified.

WCONTINUED (since Linux 2.6.10)
also return if a stopped child has been resumed by delivery of SIGCONT.

waitpid提供了一个wait函数的非阻塞版本,有时候希望取得一个子进程的状态,但不想使父进程阻塞,把options设置为WNOHANG

waitpid(*,  *,WNOHANG );他可以使得调用者不阻塞。

如果没有任何子进程的进程调用了wait函数,会立即出错返回。

测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> int main()
{
int exit_code;
int k;
char* msg;
pid_t pid; pid = fork(); if (pid < )
{
exit(-);
}
else if ( == pid)
{
k = ;
msg = "child is running";
exit_code = ;
printf("child's pid:%d\n", getpid());
}
else
{
exit_code = ;
} if ( pid != )//父进程执行
{
int stat_val;
pid_t pid; pid = wait( &stat_val );
printf("child process has exited,pid=%d\n", pid); if ( WIFEXITED(stat_val) )
{
printf("child exited with code:%d\n", WEXITSTATUS(stat_val));
}
else
{
printf("child process exit abnormally\n");
}
}
else//子进程执行
{
while(k-- > )
{
puts( msg );
sleep();
}
} exit(exit_code);
}

说明:

父进程调用wait之后会挂起,直到子进程结束为止。宏WEXITSTATUS获取子进程的退出码

等待进程结束wait,waitpid的更多相关文章

  1. Linux 等待进程结束 wait() 和 waitpid()

    若子进程先于父进程结束时,父进程调用wait()函数和不调用wait()函数会产生两种不同的结果: --> 如果父进程没有调用wait()和waitpid()函数,子进程就会进入僵死状态. -- ...

  2. 等待进程结束函数中的BUG

    偶然发现一个BUG,有一个函数是这样写的: void WaitProcExit(DWORD dwPid) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACC ...

  3. linux 进程学习笔记-等待子进程结束

    <!--[if !supportLists]-->Ÿ <!--[endif]-->等待子进程结束 pid_t waitpid(pid_t pid, int *stat_loc, ...

  4. 父进程等待子进程结束 waitpid wait

    我们一直在强调一个概念就是进程是一个程序执行的实例,是内核在虚拟概念下创建的实体,它实例化的体现在用户态就是程序代码和代码使用的变量(存储空间),在内核态就是内核为我们每个进程所保存的数据结构(状态信 ...

  5. Linux下利用fork()创建子进程并使父进程等待子进程结束

    int status; pid_t t = fork(); if(t){     waitpid(t, &status, 0); }else{     system("vi temp ...

  6. shell命令管道未读完阻塞了子进程,与等待其结束的父进程死"锁"。

    在exec执行一个子进程,我们希望使用管道取得子进程在重定向后的标准输出上的结果,同时等待子进程的结束.那么是等待子进程结束后才取管道数据,还是边取数据边等待子进程结束呢? 这里有一个调试的例子.u0 ...

  7. 进程——wait与waitpid、僵尸进程与孤儿进程

    僵尸进程:子进程终止了,但是父进程没有回收子进程的资源PCB.使其成为僵尸进程 孤儿进程:父进程先与子进程结束了,使得子进程失去了父进程,这个时候子进程会被1号进程init进程领养,成为孤儿进程 为了 ...

  8. 转载:进程退出状态--waitpid status意义

    最近遇到一个进程突然退出的问题,由于没有注册signalhandler所以没有捕捉到任何信号. 但是从log中看到init waitpid返回的status为0x008b,以前对status不是很了解 ...

  9. Python进阶----进程间数据隔离, join阻塞等待, 进程属性, 僵尸进程和孤儿进程, 守护进程

    Python进阶----进程间数据隔离, join阻塞等待, 进程属性, 僵尸进程和孤儿进程, 守护进程 一丶获取进程以及父进程的pid 含义:    进程在内存中开启多个,操作系统如何区分这些进程, ...

随机推荐

  1. 20145333茹翔 Exp8 Web基础

    20145333茹翔 Exp8 Web基础 实验问题回答 (1)什么是表单 表单是一个包含表单元素的区域,表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输入信息的元素,表单在 ...

  2. 2018-2019-1 20189218《Linux内核原理与分析》第八周作业

    编译链接的过程 编译就是把文本形式源代码翻译为机器语言形式的目标文件过程. 链接是把目标文件.操作系统的启动代码和用到的库文件进行组织最终形成可执行代码的过程. 对于GCC来说,编译源代码并最终形成可 ...

  3. Linux文件时间详解ctime、mtime、atime【转】

    本文转载自:http://blog.csdn.net/doiido/article/details/43792561 Linux系统文件有三个主要的时间属性,分别是 ctime(change time ...

  4. HDU 3404 Switch lights(Nim积)题解

    题意:在一个二维平面中,有n个灯亮着并告诉你坐标,每回合需要找到一个矩形,这个矩形xy坐标最大的那个角落的点必须是亮着的灯,然后我们把四个角落的灯状态反转,不能操作为败 思路:二维Nim积,看不懂啊, ...

  5. Java8新特性:Function接口和Lambda表达式参考

    Lambda基本:https://blog.csdn.net/wargon/article/details/80656575 https://www.cnblogs.com/hyyq/p/742566 ...

  6. java代码实现highchart与数据库数据结合完整案例分析(二)---折线图

    作者原创:未经博主允许不许转载 在上一篇的博客中,展示和分析了如何做一个饼状图,有疑问可以参考上一篇博客. 现在分析和展示折线图的绘制和案例分析, 先展示效果图: 与饼状图不同的是,折线图展现更多的数 ...

  7. hdoj-1005-Number Sequences

    题目:Number Sequences 代码: #include<stdlib.h> #include<iostream> #include<cstdio> #in ...

  8. MVC ---- Linq查询

    Linq查询:编译后,会生成对应的标准查询运算符!所以说,Linq只是类似与Sql的一种更加友好的语法而已: public class LinqDemo{ public static void Tes ...

  9. codevs 1200 同余方程 逆元

    题目描述 Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入描述 Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空 ...

  10. Redis 5种数据结构及其使用场景举例--STRING

    String 数据结构是简单的 key-value 类型,value 不仅可以是 String,也可以是数字(当数字类型用 Long 可以表示的时候encoding 就是整型,其他都存储在 sdshd ...