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

Re: Waiting for a process without using pid



On Tue, Sep 21, 2010 at 9:14 PM, Anonymous bin Ich <ichbinanon@xxxxxxxxx> wrote:
> On 09/20/2010 10:20 PM, PJ Weisberg wrote:
>>
>> On Mon, Sep 20, 2010 at 7:55 AM, Bart Schaefer
>> <schaefer@xxxxxxxxxxxxxxxx> Âwrote:
>>
>>> Perhaps I was trying to make my solution too general; i.e., I didn't
>>> want to care how many children were started or whether their PIDs had
>>> been remembered. ÂBut if you always have exactly two children, why not
>>> this?
>>>
>>> Â Âcoproc read -E
>>> Â Âtrap "print -p" CHLD
>>>
>>> Â Â./child.sh&
>>> Â Âpid1=$!
>>> Â Â./child.sh&
>>> Â Âpid2=$!
>>>
>>> Â Âread -p
>>> Â Âkill $pid1>&/dev/null
>>> Â Âkill $pid2>&/dev/null
>>
>> You *can* rewrite that to not care how many children there are.
>>
>> for thing in $stuff; do
>> Â Â ./child.sh $thing
>> Â Â set -A pids $pids $!
>> done
>>
>> for child in $pids; do
>> Â Â kill $child>&/dev/null
>> done
>
> Ok, I have modified child.sh as following:
>
> #!/bin/sh
> if [ $# -gt 0 ]; then
> Â Âgotsig=0
> Â Âdeadchild=0
> Â Âtrap "exitfunc" INT HUP TERM
> Â Âtrap "exitdeadchildfunc" CHLD
> Â Âexitfunc () {
> Â Â Â Âecho $$: Got sig...
> Â Â Â Âgotsig=1
> Â Â}
> Â Âexitdeadchildfunc () {
> Â Â Â Âecho $$: SIG is CHLD...
> Â Â Â Âdeadchild=1
> Â Â}
> Â Âecho $$: Sleeping for $1 seconds
> Â Âsleep $1 &
> Â Âwait
> Â Âif [ $gotsig -ne 0 ]; then
> Â Â Â Âif [ $deadchild -ne 0 ]; then
> Â Â Â Â Â Âecho $$: sleep in for $1 seconds already stopped (no $!)
> Â Â Â Âelse
> Â Â Â Â Â Âecho $$: Stopping sleep for $1 seconds in $!
> Â Â Â Â Â Âkill $!
> Â Â Â Âfi
> Â Âelse
> Â Â Â Âecho $$: Slept for $1 seconds
> Â Âfi
> else
> Â Âecho No args
> Â Âexit 1;
> fi
>
>
> So, two normal cases are:
>
> % ./parent.sh
> 6680: Launching 2 child processes
> 6682: Sleeping for 1 seconds
> 6683: Sleeping for 5 seconds
> 6682: SIG is CHLD...
> 6682: Slept for 1 seconds
> 6680: Waking coprocess
> Woken
> Finishing 6680
> 6680: got HUP
> 6680: Waking coprocess
> 6683: Got sig...
> 6683: Stopping sleep in 6685 for 5 seconds
> % ./parent.sh
> 6686: Launching 2 child processes
> 6688: Sleeping for 1 seconds
> 6689: Sleeping for 5 seconds
> 6688: SIG is CHLD...
> 6688: Slept for 1 seconds
> 6686: Waking coprocess
> Woken
> Finishing 6686
> 6686: got HUP
> 6686: Waking coprocess
> 6689: Got sig...
> 6689: SIG is CHLD...
> 6689: sleep in 6691 for 5 seconds already stopped
> %
>
> And one abnormal case is:
> % ./parent.sh
> 6620: Launching 2 child processes
> 6622: Sleeping for 1 seconds
> 6623: Sleeping for 5 seconds
> 6622: SIG is CHLD...
> 6622: Slept for 1 seconds
> 6620: Waking coprocess
> 6620: Waking coprocess
> % 6623: SIG is CHLD...
> 6623: Slept for 5 seconds
>
> %
>
> Now what is happening? Also, anyway do debug it (set -x isn't very helpful
> since all processes write simultaneously)
>

Ok, I don't know what was the problem but I solved it by removing
coproc altogether:
% cat parent.sh
#!/usr/bin/zsh

trap "myexitfunc" CHLD
trap "print $$: got HUP; exit 0" HUP
function myexitfunc () {
    print "$$: Killing self"
    kill -HUP -$$
}

setopt HUP
print "$$: Launching 2 child processes"
./child.sh 1 0&
./child.sh 5 0&
wait

exit 0

%

Actually, if I remove setopt HUP and replace print with echo, it works
with /bin/sh too :)

Regards



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