Re: Wait for background processes to complete
- From: grocery_stocker <cdalten@xxxxxxxxx>
- Date: Thu, 17 Jan 2008 18:02:16 -0800 (PST)
On Jan 16, 9:37 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth grocery_stocker <cdal...@xxxxxxxxx>:
On Jan 13, 10:09 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
<snip>
<snip>$SIG{CHLD} = sub {
my ($pid, @died);
push @died, $pid while $pid = waitpid -1, WNOHANG;
delete @kids{@died};
};
<snip>if ($pid) {
$kids{$pid} = 1;
return;
}
sub finish {
waitpid $_, 0 for keys %kids;
%kids = ();
}
I really don't grasp the significance of having $kids{$pid} equal 1.
Can some enlighten me o this?
It's one of the standard idioms for using a hash as a set. Every time we
create a child, we add an entry to the hash; every time one dies on its
own, we delete its entry. Then at the end we can use keys %pids to
retrieve the list of pids we still need to wait for. The only thing that
matters about %kids are its keys: we never use the values, so they can
be set to anything. I prefer using 1 since then the values are all true;
you can get slightly better memory use with
$kids{$pid} = ();
which inserts the key but doesn't create a value for it at all, but then
you have to test with exists, which I find annoying. Since in this case
I don't test for existance of keys at all, this doesn't matter: using 1 is
just a habit.
Ben
Okay, why would you have to test for exists if
$kids{$pid} = ();
just creates something like undef.
.
- Follow-Ups:
- Re: Wait for background processes to complete
- From: Peter J. Holzer
- Re: Wait for background processes to complete
- References:
- Wait for background processes to complete
- From: pgodfrin
- Re: Wait for background processes to complete
- From: pgodfrin
- Re: Wait for background processes to complete
- From: Ben Morrow
- Re: Wait for background processes to complete
- From: grocery_stocker
- Re: Wait for background processes to complete
- From: Ben Morrow
- Wait for background processes to complete
- Prev by Date: Re: incorrect errno/perror with IO::socket->new
- Next by Date: FAQ 8.11 How do I decode encrypted password files?
- Previous by thread: Re: Wait for background processes to complete
- Next by thread: Re: Wait for background processes to complete
- Index(es):
Relevant Pages
|
|