Linux fork 后 wait 获取子进程结束的状态示例

news/2024/7/5 12:05:30

使用 fork 后,可能需要获取 fork 的进程的运行状况,比如有没有异常、崩溃。
wait 在 man 中关键的描述如下:
All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal.

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
    pid_t pid;
    int status;

    printf("before fork\n");
    fflush(stdout);

    if ( (pid = fork()) < 0)
    {
        printf("fork error\n");
    }
    else if (pid == 0)
    {
        printf("after fork, child\n");

        // 4种测试情况
        exit(7); // -> normal termination, exitstatus = 7
        // abort(); // -> abnormal termination, signalstatus = 6 (SIGABRT)
        // int i = 1 / 0; // -> abnormal termination, signalstatus = 8 (SIGFPE)
        // char *p = NULL; *p = 'a'; // -> abnormal termination, signalstatus = 11 (SIGSEGV)
    }

    wait(&status);

    if (WIFEXITED(status))
    {
        printf("normal termination, exitstatus = %d\n", WEXITSTATUS(status));
    }
    else if (WIFSIGNALED(status))
    {
        printf("abnormal termination, signalstatus = %d\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));
    }

    printf("after fork, parent\n");

    return 0;
}

运行效果:


http://www.niftyadmin.cn/n/3390869.html

相关文章

[多图]Ubuntu最新显卡驱动安装方法及3D桌面部分问题解答

本文主要讲解Ubuntu环境下最新显卡驱动的安装方法以及3D桌面部分问题的解决方法&#xff0c;希望对大家有所帮助 目录&#xff1a;&#xff08;1&#xff09;硬件配置问题&#xff1a;&#xff08;2&#xff09;最简单的ATI或Nvidia显卡最新官方驱动安装方法&#xff1a;&…

tomcat连接mysql数据库_tomcat 连接数据库mysql

如图&#xff1a;一般实际中&#xff0c;客户端访问一个网站&#xff0c;最先到达apache/nginx这样的前端&#xff0c;用户不会直接访问到tomcat&#xff0c;通过nginx/apache的访问端口80再次访问后端的tomcat&#xff0c;tomcat会把用户数据存在datebase(数据库)一般实际的项…

Source Insight 3.X utf8 支持插件

继上次SI多标签插件之后,因为公司内部编码改为 utf8 编码, 因此特意做了这个Source Insight 3.X utf8 插件. 下载链接: http://download.csdn.net/detail/chinaeran/9106117 安装说明: 解压msimg32.dll sihook.dll siutf8.dll 到Source Insight 3.X安装目录 msimg32.dll 引导程…

ubuntu配置CPU温度监控

ubuntu配置CPU温度监控 时间&#xff1a;2008-11-21 10:29:33 来源&#xff1a;Linux联盟收集整理 作者&#xff1a; <script type"text/javascript"><!-- google_ad_client "pub-3701573918329010"; /* 300x250, 创建于 08-9-9 */ google_ad_…

mysql从服务器的配置_MySQL主-从服务器的配置

MySQL主-从服务器的配置本文介绍了mysql数据库的主-从服务器的详细配置&#xff0c;实现主-从服务器之间数据的复制。背景&#xff1a;1、主服务器&#xff1a;安装mysql-5.5.20-i386(且数据库中有很多数据)&#xff0c;操作系统&#xff1a;RHEL5&#xff0c;平台&#xff1a;…

Ubuntu Linux 3D桌面完全教程,显卡驱动安装方法

Ubuntu Linux 3D桌面完全教程&#xff0c;显卡驱动安装方法&#xff0c;compiz特效介绍&#xff0c;常见问题解答。 Ubuntu 2009-06-18 18:51 阅读1688 评论2 字号&#xff1a; 大大 中中 小小 [5月20日更新] Ubuntu Linux 3D桌面完全教程&#xff0c;显卡驱动安装方法&a…

魔域mysql_魔域单机版MySQL数据库启动失败解决办法

单机版魔域安装时数据库对象open失败怎么解决&#xff1f;小编刚解除魔域单机。。这几天用了好多版本。。都是启动MYSQL失败。。尝试过N次。。终于知道个法子&#xff0c;一起来看看吧&#xff01;以下教程只适合新手,其他人飘过误喷.刚下载完单机,解压完,就双击一键启动,很激动…

YUV查看器

做图像处理&#xff0c;常常遇到yuv格式&#xff0c;但无法直接查看&#xff0c;非常恼火。 后来网上发现 YUVTools 这个工具&#xff0c;据说非常强大&#xff0c;很好用&#xff0c;决定试试&#xff01; 此工具网站&#xff1a;http://www.sunrayimage.com/ 我的资源中有该…