I wrote a test to find out if I can reliably determine the integer value of the exit code with wait
.
Questions
1. Why is the exit code multiplied by 256?
2. Is exit()
, wait()
, OS, or something else doing multiplication?
Code to reproduce the problem.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> // implementation is correct but irrelevant to the question int compareInt(const void* a, const void* b); int main(void) { pid_t pids[6]; int i; for (i = 0; i < 6; i++) { pid_t pid = fork(); pids[i] = pid; if (pid == 0) { exit(i); } } int codes[6]; do { i--; wait(&codes[i]); } while (i > 0); const size_t num_elem = 6; qsort(codes, num_elem, sizeof(int), compareInt); for (i = 0; i < 5; i++) { printf("%d, ", codes[i]); } printf("%d\n", codes[5]); return 0; }
Outputs: 0, 256, 512, 768, 1024, 1280
As it turned out, I should use wifexited(), wifstopped(), wifsignaled(), wexitstatus(), wtermsig(), or wstopsig()
to determine the exit status.
Also, the same behavior is reproduced in PHP (where I first came across it)
$pids = []; foreach (range(0, 5) as $i) { $pids[] = $pid = pcntl_fork(); if ($pid === 0) { exit($i); } } $exit_codes = []; do { pcntl_wait($exit_codes[]); array_pop($pids); } while (count($pids) > 0); sort($exit_codes); echo implode(', ', $exit_codes) . "\n";
Outputs: 0, 256, 512, 768, 1024, 1280
If that matters, I run Ubuntu 14.04 and man wait
says I have WAIT(2)