Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Waiting for a process without using pid
- X-seq: zsh-users 15422
- From: Anonymous bin ich <ichbinanon@xxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Waiting for a process without using pid
- Date: Wed, 22 Sep 2010 12:09:18 +0530
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=k+w0DoiCIu88pCHlrcFpoJznO8SbbaHhcYbTr81ckoc=; b=gKDgyIrnQjmFctNQN2GUaccp9aQjg2S/rrcG2Jy1ny3aYc8hclDC4GNHhANSsYRo1a UeKX6E5H7uA6OJ/SHbYmX6vIakFPY7xmmwDNd+uA8AwqsMQZosAHVYT3iJ8ouAyL5cxG hjyVOwoCxv0hkrsR5Qx7AYtnwfCE1O6N1smm4=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=WFt46phhTnDHk/GcpjJJwRYaDci7BnekE/FOyeLUYpnmPlpzvZHnJDskM0pFY0tX9k sr2PmklQLqf/3R/8fseI92JT6l8shxQu469tvzZlebWVEL20/BgmIHTY/rIvdGx+J8vu 0D9ZFcmTGIKXgASKbx0XC/s+iBz4mRwDGhk1Y=
- In-reply-to: <4C98D2D0.9030901@xxxxxxxxx>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <AANLkTinyDC-OoGBzuisMKUg+OqeeqG=HKZrNDuJZL+sD@xxxxxxxxxxxxxx> <100916072654.ZM29712@xxxxxxxxxxxxxxxxxxxxxx> <4C950347.9060109@xxxxxxxxx> <100918124147.ZM31152@xxxxxxxxxxxxxxxxxxxxxx> <AANLkTimY1T1n9Nq7ePkNdSbJ9zhPobwzwM5SfNYb8qfB@xxxxxxxxxxxxxx> <100920075521.ZM30584@xxxxxxxxxxxxxxxxxxxxxx> <AANLkTikpY9u90sfKn4Ew0jc4CTq=PQB3=VdJoGs2Bccf@xxxxxxxxxxxxxx> <4C98D2D0.9030901@xxxxxxxxx>
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