zsh-users
 help / color / mirror / code / Atom feed
From: Rory Mulvaney <rorymulv@gmail.com>
To: Rory Mulvaney <rorymulv@gmail.com>
Cc: zsh-users@zsh.org, Wayne Davison <wayned@users.sourceforge.net>,
	 Anthony R Fletcher <arif@mail.nih.gov>
Subject: Re: wait for the next process to finish
Date: Tue, 13 Dec 2011 11:04:44 -0600 (CST)	[thread overview]
Message-ID: <alpine.DEB.2.00.1112131023060.13348@MyComp.localdomain> (raw)
In-Reply-To: <alpine.DEB.2.00.1112130937120.13348@MyComp.localdomain>

On Tue, 13 Dec 2011, Rory Mulvaney wrote:

> On Mon, 12 Dec 2011, Wayne Davison wrote:
> 
> > On Mon, Dec 12, 2011 at 7:46 AM, Anthony R Fletcher <arif@mail.nih.gov>wrote:
> > 
> > > How can I wait for just the next job to finish?
> > >
> > 
> > One thing that may help you is TRAPCHLD.  Sadly, the signal handler doesn't
> > tell you what pid it is reacting to, nor the exit code.
> > 
> > TRAPCHLD() {
> >     echo here
> >     oldpids=($pids)
> >     pids=( )
> >     for p in $oldpids; do
> >         if kill -0 $p 2>/dev/null; then
> >             pids+=$p
> >         else
> >             #wait $p # Sadly, this doesn't work
> >             echo $p exited
> >         fi
> >     done
> > }
> > pids=( )
> > sleep 10 &
> > pids+=$!
> > sleep 20 &
> > pids+=$!
> > (sleep 15; false) &
> > pids+=$!
> > echo $pids
> > wait
> > echo done
> > 
> > It might be nice to set an environment parameter with the pid and status
> > info right before the dotrap(SIGCHLD) call in jobs.c.
> > 
> > ..wayne..
> > 
> 
> To clarify (I think this is fairly simple), you can supply the process id 
> as a parameter to 'wait', and though the $! method seems rather clumsy to 
> retrieve the pid (since you have to retrieve it somehow in the next 
> command after spawning the background process), it seems to work mostly in 
> general.
> 
> So you could do:
> 
> sleep 20000 &
> sleep 20 &
> pid=$!
> wait $pid
> 
> That will just wait for the sleep 20 process to complete while the sleep 
> 20000 process still runs in the background.
> 
> For further reference, I see that $! is documented in sec. 15.5 
> (Parameters set by the shell) in the zsh texinfo manual.
> 

Sorry, now I realized that you want to want to wait for whichever 
background process finishes first.  I know that select (zsh/zselect) is 
used for this type of thing, which blocks until some file descriptor is 
ready for reading or writing.  It would possible but sort of complicated 
to hack that functionality into the case of waiting for pids.  I can 
imagine using coproc to get a file descriptor for each of the background 
processes.

Something like this (requiring a zsh version with anonymous functions):

pids=( )
R=( $RANDOM $RANDOM )
echo $R
coproc () { sleep $(( $R[1]/32767.0*20 )) ; echo $$ }
pids+=$!
# copy the coproc fd
exec 3<&p
coproc () { sleep $(( $R[2]/32767.0*20 )) ; echo $$ }
pids+=$!  
exec 4<&p
zmodload zsh/zselect
zselect -r 3 4
echo $reply

Then one could parse $reply.

I don't know, maybe there's a much easier way to create file descriptors 
for those processes?

-Rory


  parent reply	other threads:[~2011-12-13 17:14 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 15:46 Anthony R Fletcher
2011-12-12 16:37 ` TJ Luoma
2011-12-12 19:41 ` Wayne Davison
2011-12-13  7:15   ` Martin Richter
2011-12-13 16:07   ` Rory Mulvaney
2011-12-13 16:45     ` Jérémie Roquet
2011-12-13 16:59       ` Anthony R Fletcher
2011-12-13 18:08         ` Daniel Shahaf
2011-12-13 16:49     ` Christoph (Stucki) von Stuckrad
2011-12-13 17:31       ` Bart Schaefer
2011-12-13 17:04     ` Rory Mulvaney [this message]
2011-12-13 17:32       ` Anthony R Fletcher
2011-12-13 18:04         ` Jérémie Roquet
2011-12-13 18:20           ` Stephane Chazelas
2011-12-13 19:19             ` Anthony R Fletcher
2011-12-13 10:01 ` Peter Stephenson
2011-12-13 17:10   ` Bart Schaefer
2011-12-13 20:42     ` Rory Mulvaney
2011-12-13 17:45 ` Stephane Chazelas

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=alpine.DEB.2.00.1112131023060.13348@MyComp.localdomain \
    --to=rorymulv@gmail.com \
    --cc=arif@mail.nih.gov \
    --cc=wayned@users.sourceforge.net \
    --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).