Re: Wait for background processes to complete



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>
$SIG{CHLD} = sub {
my ($pid, @died);
push @died, $pid while $pid = waitpid -1, WNOHANG;
delete @kids{@died};
};
<snip>
if ($pid) {
$kids{$pid} = 1;
return;
}
<snip>
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.
.



Relevant Pages

  • Re: Wait for background processes to complete
    ... 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. ... matters about %kids are its keys: we never use the values, ...
    (comp.lang.perl.misc)
  • Re: lost key of authenticity
    ... The Belarc Advisor at www.belarc.com ... Download and run it; you will then be provided a list af PIDs, CD keys, etc. ...
    (microsoft.public.windowsxp.accessibility)
  • Re: Carry On... Ten Year-Old Girl!
    ... I should know since I *was* one of those kids ... pre-programmed/patched for the lead sound. ... playing and it appears she just uses the upper keys of the lower ... Actually, if you study neuroanatomy a bit, it becomes clear that usage of the plural, "brains" is quite accurate. ...
    (alt.guitar.amps)
  • Re: strategy for data entry in multiple tables
    ... I sometimes use composite keys to prevent duplicates in a table, ... > what appears to be excess entry is necessary for data validation and for ... > tables be opened as subforms within the junction table form? ... >> the bird info...just the info necessary to determine whether the bird ...
    (microsoft.public.access.forms)
  • Re: Carry On... Ten Year-Old Girl!
    ... I should know since I *was* one of those kids ... on the lower keyboard split instrument-wise and programmed ... pre-programmed/patched for the lead sound. ... playing and it appears she just uses the upper keys of the lower ...
    (alt.guitar.amps)