父进程如何通过子进程调用_exit的等待来获得终止状态 [英] How does parent process get the termination status through wait from a child process which calls _exit

查看:315
本文介绍了父进程如何通过子进程调用_exit的等待来获得终止状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下声明.

赋予_exit()的status参数定义了终止状态 流程,当流程的父流程可用时 调用wait().

The status argument given to _exit() defines the termination status of the process, which is available to the parent of this process when it calls wait().

_exit()始终成功终止进程(即 _exit() 永远不会返回).

A process is always successfully terminated by _exit() (i.e., _exit() never returns).

问题

如果_ 退出没有返回,则父进程如何获得终止状态 从子进程通过 wait ?

If _exit doesn't return, how does the parent process can get the termination status from the child process through the wait?

推荐答案

无论何时进程退出(无论是否通过调用_exit(int Exit_Status)),内核都会向其父级发送SIGCHLD函数.父母可以

Whenever a process exits (whether or not by calling _exit(int Exit_Status) ) the kernel sends SIGCHLD function to its parent. the parent may either

     1. Ignore the incoming signal
     2. Catch it by installing a signal handler

具体来说,父级可以通过调用wait()或waitpid()函数来捕获退出状态.在这种情况下,LSB可用于父级.具体地,状态可以如下获悉

Specifically the parent may catch the exit status by calling the wait()or waitpid() function. In this case the LSB is made available to the parent. Specifically the status may be learnt as follows

    int status;
    wpid = waitpid(child_pid, &status, WUNTRACED);

由于只有最后8位可用,因此通过按位进行255运算来掩盖高位是合乎逻辑的.系统定义的宏会为您完成

Since only the last 8 bits are available it will be logical to mask the upper bit by doing a bitwise and operation with 255. A system defined macro does this for you

   WEXITSTATUS(status);

因此,为了获得子状态-您可以在waitpid语句之后使用

Thus in order to get the child status - you may use after the waitpid statement

   printf("child exited, status=%d\n", WEXITSTATUS(status));

忽略SIGCHLD可能会导致创建僵尸(失效)进程.为SIGCHLD设置SA_NOCLDWAIT标志不会产生僵尸,因为内核会收获僵尸.但是,该代码不是可移植的,因此最好使用wait系统调用.

Ignoring the SIGCHLD may lead to creation of zombie (defunct) process(es). Setting SA_NOCLDWAIT flag for SIGCHLD does not produce a zombie as the kernel reaps them. However, the code is not portable and its better to use wait system call.

这篇关于父进程如何通过子进程调用_exit的等待来获得终止状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆