On 4/26/17 7:57 PM, Vincent Lefevre wrote: > On 2017-04-25 21:32:56 -0400, Chet Ramey wrote: >> There is no requirement that a trap be executed each time a particular >> signal is generated. There is no requirement that the system keep a >> count of instances of a particular pending signal; only that the signal >> is pending. SIGCHLD isn't different from any other signal in this >> regard. > > In such a case, how can you know the number of remaining processes?
You can't, really. There is no guarantee that the shell will receive exactly one SIGCHLD for each child that exits, because there is no requirement that the kernel queue signals. It's easy to imagine the following scenario: process blocks SIGCHLD child exits, kernel adds SIGCHLD to pending signal mask child exits, kernel adds SIGCHLD to pending signal mask process unblocks SIGCHLD kernel sends SIGCHLD to process Even if the process only reaps one of the two available exited children, the kernel is not going to re-deliver SIGCHLD. And virtually every shell out there is going to reap as many exited processes as it can each time it calls waitpid() or equivalent. The best you can do as a script writer is rely on whatever guarantees the shell gives you. Bash guarantees that a SIGCHLD trap will be called once for each child that it reaps when job control is enabled, but Posix doesn't. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/