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

Re: wait for the next process to finish



On Tue, 13 Dec 2011, Bart Schaefer wrote:

> 
> In the realm of ugly hacks, you could run each child as a coprocess
> that ends with "print $?".  Stash away the coprocess file descriptor
> each time you start one, and then when they're all running, start a
> loop polling that list of descriptors with "read -t" until you find
> one from which you can read.  What you read will be the exit status.
> 

Yes, this is what I demonstrated in my second email, but the idea about 
using TRAPCHLD is probably easiest, since you could have your child 
processes print out any info about the process such as the return code, 
redirecting it to a single file descriptor owned by a designated 
information-relay coproc (which merely echo's its stdin to stdout), and 
then parse the output from the coproc in TRAPCHLD, which is apparently one 
of the available trap functions running whenever a signal arrives, since 
SIGCHLD is a signal from signal(7) (I didn't realize that before).

On Tue, 13 Dec 2011, Bart Schaefer wrote:

>
>     TRAPCHLD() {
>        while (( ${#jobstates} < MAXPARALLEL ))
>        do the background task &
>        done
>     }
>
> (Of course you need some sort of other loop-ending condition, but you
> get the idea.)
>
> You don't really even need the trap for this, your original loop will
> work fine as long as you examine ${(k)jobstates} as the list of PIDs
> instead of trying to maintain your own array.
>



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