DEV Community

Cover image for Understanding the exit status Bits
Zobair Najdaoui
Zobair Najdaoui

Posted on

Understanding the exit status Bits

Image description

πŸ”’ 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:

  1. Normal termination (via exit() or return)
  2. Killed by a signal
  3. Stopped by a signal
  4. 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)
Enter fullscreen mode Exit fullscreen mode

So the value looks like:

0x00SS  (where SS is the exit status byte)
Enter fullscreen mode Exit fullscreen mode
  • 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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Looks like: 0xSS7F where SS = signal
  • Detected by WIFSTOPPED(status)
  • WIFSIGNALED(status) β†’ false

▢️ 4. Continued

status = 0xFFFF
Enter fullscreen mode Exit fullscreen mode
  • 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)