std (posix): support for `wait()` (any process termination)
With the implementation of std.posix.waitpid(pid:, flags:), the use of -1 will crash as unreachable (due to errno of CHILD) if there's no terminated children instead of allowing it to be defined behaviour and return a pid of 0.
The documentation of the waitpid syscall states:
The waitpid() system call suspends execution of the calling thread
until a child specified by pid argument has changed state. By
default, waitpid() waits only for terminated children, but this
behavior is modifiable via the options argument, as described
below.
For example, the nginx worker process SIGCHLD handler uses something like (source):
for ( ;; ) {
pid = waitpid(-1, &status, WNOHANG);
if (pid == 0) {
return;
}
// ... rest omitted
}
The reasoning for using wait(flags:) as name is due to the documented behaviour of:
The wait() system call suspends execution of the calling thread
until one of its children terminates. The call wait(&wstatus) is
equivalent to:
waitpid(-1, &wstatus, 0);
This change allow daemons/monitor processes to respawn their forks when they exit by querying all currently-terminated processes, and ignore/continue when there aren't any.
I'm currently using this PR's change for a company project, functioning correctly indicated by these logs (tested on both Linux and macOS, still need verification for other POSIX-compliant operating systems):
~/redacted-company/MultiMedia$ zig-out/bin/managedmultimedia foo bar --ifsf awd
start worker processes
MultiMedia: start worker process 29200
Event-Proxy: start worker process 29201
signal 20 received
Event-Proxy (29201): exited with status 0
MultiMedia (29200): exited with status 0
The usage within a SIGCHLD handler, for example:
while (std.posix.wait(flags)) |result|
on_exit(result.pid, result.status);