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

4

2

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 parent process can get the termination status from the child process through the wait?

Joke answered 19/7, 2011 at 15:11 Comment(0)
S
3

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.

Spelaean answered 19/7, 2011 at 16:29 Comment(0)
P
3

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?".

Pontormo answered 19/7, 2011 at 15:18 Comment(0)
S
3

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.

Spelaean answered 19/7, 2011 at 16:29 Comment(0)
K
2

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.
Kimberlykimberlyn answered 19/7, 2011 at 15:23 Comment(0)
C
1

_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.

Clairclairaudience answered 19/7, 2011 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.