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