I wonder if someone could help me understand how fork and wait works. I may do the right thing, but I may also have missed something fundamental here.

Consider the following HTTP::Daemon code (simplified from my scenario), pretty much snipped from the synopsis, with forking added.

my $d = HTTP::Daemon->new( LocalPort => $self->port(), ReuseAddr => 1, ); $SIG{CLD} = $SIG{CHLD} = 'IGNORE'; while (my $c = $d->accept()) { next if(fork()); $SIG{CLD} = $SIG{CHLD} = 'DEFAULT'; #Reset child signal handler while (my $r = $c->get_request()) { # ... do stuff here, including a system() call } $c->close; undef($c); exit(0); }

At first, the fork left zombies. So I Googled for that, and figured I need a wait() or IGNORE handler. So I added that. The zombies went away.

But, instead I got problems with a system() call. It failed and $! was "No child processes". Huh?

My guess is that now it's the child signal handler that's messing this up, so I reset it to 'DEFAULT' after the fork. This seems to solve the problem and the system() works fine again.

Is this correct? Or did I just manage to fix the symptom instead of the problem?


/J


In reply to Understanding fork + wait by jplindstrom

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.