π’ Understanding the status
Bits
This status is a 16-bit integer returned by the kernel to describe how a child process changed state. It can represent:
-
Normal termination (via
exit()
orreturn
) - Killed by a signal
- Stopped by a signal
- Continued (after being stopped)
π§± Bit Layout for Each Case
Let's read this from least significant bit (bit 0) to most (bit 15):
β 1. Normal Termination
Bits 0β7: Always zero
Bits 8β15: Exit status (0β255)
So the value looks like:
0x00SS (where SS is the exit status byte)
- Detected by
WIFEXITED(status)
macro. -
WIFSIGNALED(status)
β false
β οΈ 2. Killed by Signal
Bits 0β6: Signal number (non-zero)
Bit 7: Core dump flag
Bits 8β15: Unused (0)
Example: if the process was killed by SIGSEGV (11)
and dumped core:
Signal = 11 β bits 0-6 = 0001011
Core = 1 β bit 7 = 1
status = 00000000 10001011 β 0x008B
-
WIFSIGNALED(status)
checks that:- Bits 0β6 β 0 (so signal present)
- Bits 8β15 = 0 (not a stopped or continued signal)
If so β returns true
Then:
-
WTERMSIG(status)
β reads bits 0β6 β 11 -
WCOREDUMP(status)
β reads bit 7
β 3. Stopped by Signal
Bits 0β7: 0x7F (decimal 127)
Bits 8β15: Stop signal
- Looks like:
0xSS7F
whereSS
= signal - Detected by
WIFSTOPPED(status)
-
WIFSIGNALED(status)
β false
βΆοΈ 4. Continued
status = 0xFFFF
- Used with
waitpid(..., WCONTINUED)
- Detected by
WIFCONTINUED(status)
-
WIFSIGNALED(status)
β false
β Summary
Status Type | Bit Pattern Summary | Detected by | Related Macros |
---|---|---|---|
Normal exit | bits 0β7 = 0, bits 8β15 = exit code | WIFEXITED(status) |
WEXITSTATUS(status) |
Killed by signal | bits 0β6 = signal, bit 7 = core dump flag | WIFSIGNALED(status) |
WTERMSIG(status) , WCOREDUMP(status)
|
Stopped | bits 0β7 = 0x7F, bits 8β15 = stop signal | WIFSTOPPED(status) |
WSTOPSIG(status) |
Continued | status == 0xFFFF | WIFCONTINUED(status) |
Top comments (0)