开发者

How does parent process get the termination status through wait from a child process which calls _exit

开发者 https://www.devze.com 2023-03-21 03:47 出处:网络
I have read the following statement. The status argument given to _exit() defines the termination status of

I have read the following statement.

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().

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

Question

If _exit doesn't return, how does the pare开发者_如何转开发nt process can get the termination status from the child process through the wait?


When the child terminates, the kernel keeps some information about it. Among this information is the return code.

Meanwhile, the parent hasn't terminated. By calling wait or waitpid it simply asks the kernel "Hey, you know that child of mine? What's his status?".


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

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);

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);

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

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

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.


Inside the _exit(int status) function, the process notifies the parent process via SIGCHLD that that it is about to terminate, and the low-order 8 bits of status are made available to the parent. There are three ways in which the exit status is handled

  1. The parent process may indicate that it's not interested in the return value, and in this case the exit status is discarded, and child process terminates.
  2. The parent process may be in a wait(), then the status is communicated to the parent, and the child process terminates.
  3. The parent process is not in wait(), then the child process turns into a zombie process, whose purpose is to wait for the parent to make a call to wait() to retrieve the exit status.


_exit() doesn't return means that when a process calls _exit(), that call never returns from it (in this case, because the process terminates).

_exit() is a system call, and takes an argument. The kernel saves that argument in an internal structure. The kernel has a lightweight structure for processes that have already exited but have not yet been waited-for (zombies). When a process waits for its children via wait() (another system call), the kernel retrieves that value, and forgets about the zombie.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号