wait3 (waitpid alias) returns -1 with errno set to ECHILD when it should not

TLDR: you are currently relying on unspecified behaviour of signal(2); use sigaction (carefully) instead. Firstly, SIGCHLD is strange. From the manual page for sigaction; POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)). Nevertheless, the historical … Read more

Example of waitpid() in use?

Syntax of waitpid(): pid_t waitpid(pid_t pid, int *status, int options); The value of pid can be: < -1: Wait for any child process whose process group ID is equal to the absolute value of pid. -1: Wait for any child process. 0: Wait for any child process whose process group ID is equal to that … Read more

Bash: wait with timeout

Both your example and the accepted answer are overly complicated, why do you not only use timeout since that is exactly its use case? The timeout command even has an inbuilt option (-k) to send SIGKILL after sending the initial signal to terminate the command (SIGTERM by default) if the command is still running after … Read more