zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Trapping SIGCHLD
Date: Sun, 3 Dec 2023 13:58:07 -0800	[thread overview]
Message-ID: <CAH+w=7Z6fAdHsx-M7FXVV7bqQ0DyVXhdD5LJMwh3h8uox=AtPQ@mail.gmail.com> (raw)

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@fifi.org> 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.


                 reply	other threads:[~2023-12-03 21:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Z6fAdHsx-M7FXVV7bqQ0DyVXhdD5LJMwh3h8uox=AtPQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).