Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Trapping SIGCHLD



Found the first part of this sitting in my drafts folder from a year
and a half ago.

On Sat, Mar 26, 2022 at 11:10 AM Philippe Troin <phil@xxxxxxxx> wrote:
>
> There may be a way to achieve keeping a set number of children around,
> by trapping SIGCHLD, but we would completely lose the exit status of
> the command.

Is it necessary to grab the exit status right away, or just to obtain
it eventually?  If the CHLD trap did nothing but decrement a counter
every time a child exited, a new child could start right away,
increment the counter, and then (possibly much) later an explicit
"wait $childpid" could be done.

> There does not seem to be a way to retrieve the exit
> status of a command as soon as SIGCHLD is trapped

It's a bit round-about, but:

watchCHLD() {
  setopt localoptions nonotify
  trap 'print -p wakeup; trap - CHLD' CHLD
  coproc read -E
  read -p
}
waitchild () {
    watchCHLD
    wait $1
}

() { sleep 20; return 23 }  & waitchild $!
print $?

If/when the patch from workers/52365 goes in, it'll be possible to get
the status of a specific PID in the CHLD trap itself.  However, if
multiple children exit at the same time the CHLD handler may be called
only once, so some trickery with $jobstates is necessary.  If this is
still of interest I'll try to work out details.




Messages sorted by: Reverse Date, Date, Thread, Author