I do most of my work on FreeBSD and haven't access to a Linux box. This is important because you reinstall the handler. That is an *old* SysV thing. Are you sure it is necessary? I had endless troubles using code like this until I stopped reinstalling the handler.

Second, why are you using a handler? It seems to me you are not ( at least in this example ) gaining anything by installing a handler. You want the parent to handle reaping the children. Signal handlers are really only useful when you want something to happen outside the scope of the parent process and that "something" needs to happen immediately. I do not gather you are working like that. If a child waits a few seconds for you to reap it, will there be any harm?

This snippet is from a code that spawns N children and gives them something to do from a job queue. When a child finishes, ie exits, I simply spawn another child off. Note the loop at the end to wait for the last N children to finish.

while( @jobq ) { if ( $counter < $num_children ) { &SPAWN( $ref ); } else { do { sleep 5; $dpid = waitpid( -1, 1 ); } until $dpid; last if ( $dpid == -1 ); # No children left printf "%s has finished, spawning next child (%d left)\n", $tr +acker{$dpi d}, scalar @jobq; &SPAWN( $ref ); } } do { sleep 5; $dpid = waitpid( -1, 1 ); printf("%s has finished\n", $tracker{$dpid}) if(defined( $tracker{ +$dpid} ) ); } until $dpid == -1;

mikfire


In reply to RE: POSIX and sigaction by mikfire
in thread POSIX and sigaction by ncw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.