Wait function return value summary

Source: Internet
Author: User

Before studying the wait and WAITPID functions, I was puzzled by the use of macro wifexited to check the resulting process termination state: Typically we exit by calling the exit or _exit function in the program. So the terminating state of the wait and WAITPID functions is just the argument that we passed to exit is not OK?

Later learned that I think simple, because the program exit is not only we show the call exit so simple, there will be abnormal exit and so on, this article on the wait function to get the state to do a summary!

Let's start with a general summary of the wait status, generally we can determine that the following event occurred for the child process through the wait status:

(1) The child process exits normally by passing an shaping parameter to exit (or _exit)

(2) The child process is terminated by a signal

(3) Child process is paused by a signal (specify wuntraced flag when calling Waitpid)

(4) Paused subprocess is restored by signal Sigcont (specify wcontinued flag when calling Waitpid)

The process termination status we're talking about is just the first two wait status (you can see the termination status of a process through $?).

So how does the wait status represent these events? How it is expressed, different platforms have different definitions because POSIX does not have a detailed definition of the implementation, which is why it is recommended to use macros to check the wait status, mainly due to the portability of the program. This article is for the x86 platform 32-bit.

By discovery, although the wait status is of type int, it actually uses only its lower 2 bytes.

The high 8 bits are used to record the normal exit status, which explains why the program exit status is always 0~255.

Low 8 bits are used to record signals.

OK, now let's see how these macros are implemented.

In the/usr/include/i386-linux-gnu/sys/wait.h

In the/usr/include/i386-linux-gnu/bits/waitstatus.h

Next we analyze these macros briefly:

Wifexited/wexitstatus: wifexited (status) is true when the program exits normally, in which case wexitstatus (status) Returns the exit status of the child process.

Wifexited finally can be abbreviated as:

[HTML]View PlainCopy print?
    1. #define WIFEXITED (status) ((status) & 0x7f) = = 0)

When wifexited (status) is a true representation (status & 0x7f) is 0, meaning: program exit is not a signal-led exit, then the normal exit.

Wexitstatus (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WEXITSTATUS (status) ((status) & 0xff00) >> 8)

The low 8 bits are cleared first, then the 8 bits are shifted right, then a high 8-bit value is obtained, that is, the program exits normally.

Wifstopped/wstopsig: wifstopped (status) is true when a child process is returned because it is paused by a signal, in which case Wstopsig (status) Returns the number of the paused subprocess signal.

wifstopped (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WIFSTOPPED (Status) (& 0xff) = = 0x7f)

When the wait status low eight-bit value is 0x7f, it indicates that the child process was returned by a signal pause.

Wstopsig (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WSTOPSIG (status) ((status) & 0xff00) >> 8)

You can see that in this case, the Wstopsig (status) is the same as the Wexitstatus (status) value.

Wifcontinued: When a paused subprocess is returned by a signaled Sigcont, the wifcontinued (status) is true, otherwise false.

wifcontinued (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WIFCONTINUED (status) = = 0xFFFF)

When the wait status is two bytes low, the value is 0XFFF, indicating that a paused child process is awakened by the sigcont signal.

Wifsignaled/wtermsig/wcoredump: wifsignaled (Staus) is true when the program terminates abnormally, in which case Wtermsig (status) returns the signal number of the terminating process. And when the program terminates abnormally, the core file is generated, then the Wcoredump (status) is true and no one is false.

wifsignaled (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WIFSIGNALED (Status) (((Signed char) (Status & 0x7f + 1) >> 1) > 0)

This macro is the most difficult to understand the macro, the following is my simple analysis process, right and wrong also please criticize!

(status) & 0x7f range is 0 ~ 127

(status) & 0x7f + 1 range is 0 ~ 128

Then (signed char) (Status & 0x7f + 1) range is 1 ~ 127 and one-128

This launches: (Signed char) ((status) & 0x7f + 1) >> 1 range is 0 ~ 63 and one-64

It is concluded that when the low 7-bit value of the wait status is 0x7f, the wifsignaled of the "status" is actually excluded from the low 7-bit value of the wait status is 0x7f. Because when the low 7-bit is 0x7f (8th bit 0), the child process is signaled to be paused. In fact, the existing platform signal number has not reached 127.

We know that the signal number starts at 1 (the Kill function has special treatment for signal number 0). Move right one is equivalent to dividing by 2, 1 divided by 2 in the program is equal to 0, all of which add 1 operation is actually very skillful, both using the signal number from 1 to start this feature, but also excludes the value of 127.

This macro can also be implemented as follows:

[HTML]View PlainCopy print?
    1. #define WIFSIGNALED (status) (! wifstopped (status) &&! wifexited (status))

This is good to understand, neither signal suspension, or normal exit, then certainly not be the signal sigcont wake, it must be signaled the situation, haha.


Wtermsig (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WTERMSIG (status) & 0x7f

Well, that doesn't have to be explained.

Wcoredump (status) can be abbreviated as:

[HTML]View PlainCopyprint?
    1. #define WCOREDUMP (status) & 0x80

This is also very well understood, is to detect whether the 8th bit is 1, is 1, then generate a core file, otherwise not generated.

In conclusion, it can be found that the Pr_exit function of Apue is not complete enough, the following gives a comprehensive following:

[CPP]View PlainCopyprint?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/wait.h>
  5. void Pr_exit (const char *msg, int status)
  6. {
  7. if (msg)
  8. printf ("%s", msg);
  9. if (wifexited (status)) {
  10. printf ("Normal termination, exit status =%d\n", Wexitstatus (status));
  11. } Else if (wifsignaled (status)) {
  12. printf ("abnormal termination, signal number =%d (%s)%s\n",
  13. Wtermsig (status), Strsignal (Wtermsig (status)),
  14. #ifdef Wcoredump
  15. Wcoredump (status)?  " (core file generated)": " );
  16. #else
  17. "");
  18. #endif
  19. } Else if (wifstopped (status)) {
  20. printf ("Child stopped, signal number =%d (%s) \ n",
  21. Wstopsig (status), Strsignal (Wstopsig (status));
  22. }
  23. #ifdef wifcontinued
  24. Else if (wifcontinued (status)) {
  25. printf ("Child continued by Sigcont Signal\n");
  26. }
  27. #endif
  28. else {/ * should never happen * /
  29. printf ("What happened-this-child?") (status=%x) \ n ",
  30. (unsigned int) status);
  31. }
  32. }

PS:C Standard to a signed value and negative when the right-shift operation is not implemented, I tried GCC, the result is negative, not a lot of understanding of this piece, please understand.

Reference Links:

http://tsecer.blog.163.com/blog/static/15018172012323975152/

Http://www.cs.virginia.edu/pipermail/splint-discuss/2008-March/001136.html

Reference books:

"The Linux programming Interface"

Wait function return value summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.