That code is buggy. Here it is in a package with a dummy new function, a warn statement to tell us when the reaper is running, and the line that checks if we're running under Linux commented out (since I'm testing it on Linux):
#!/usr/bin/perl -w use strict; my $nd = NDTest->new; $nd->Bind; warn "My PID is $$\n"; foreach my $i (1..10) { if (!fork()) { # Child sleep(1); exit($i); } sleep(2); } package NDTest; sub new { my $class = shift; my $self = {}; $self->{mode} = 'fork'; bless $self,$class; } sub SigChildHandler { my $self = shift; my $ref = shift; return undef if $self->{'mode'} ne 'fork'; # Don't care for childs +. # return 'IGNORE' if $^O eq 'linux'; # We get zombies on Linux othe +rwise my $reaper; sub { warn "Running reaper.\n"; $$ref = wait; $SIG{'CHLD'} = $reaper; }; } sub Bind ($) { my $self = shift; my $fh; my $child_pid; my $reaper = $self->SigChildHandler(\$child_pid); $SIG{'CHLD'} = $reaper if $reaper; }
That creates 10 children fairly slowly. Let's see how many are reaped:
[sgifford@sghome pa1]$ perl /tmp/t107 My PID is 18286 Running reaper. Use of uninitialized value in scalar assignment at /tmp/t107 line 37.
That doesn't look promising. So what happened to the other 9 processes?
$ ps -ef |grep 18286 sgifford 18286 16547 0 14:02 pts/0 00:00:00 perl /tmp/t107 sgifford 18288 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18289 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18292 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18295 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18298 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18301 18286 0 14:02 pts/0 00:00:00 [perl <defunct>] sgifford 18303 18212 0 14:03 pts/20 00:00:00 grep 18286
Yup. Zombies. Just like you're seeing.

I think that the only problem with this code is a typo. If we add a line below my $reaper;, saying $reaper =, assigning the sub ref to $reaper (which is what I believe the author intended) the problem goes away.

#!/usr/bin/perl -w use strict; my $nd = NDTest->new; $nd->Bind; warn "My PID is $$\n"; foreach my $i (1..10) { if (!fork()) { # Child sleep(1); exit($i); } sleep(2); } package NDTest; sub new { my $class = shift; my $self = {}; $self->{mode} = 'fork'; bless $self,$class; } sub SigChildHandler { my $self = shift; my $ref = shift; return undef if $self->{'mode'} ne 'fork'; # Don't care for childs +. # return 'IGNORE' if $^O eq 'linux'; # We get zombies on Linux othe +rwise my $reaper; $reaper = sub { warn "Running reaper.\n"; $$ref = wait; $SIG{'CHLD'} = $reaper; }; } sub Bind ($) { my $self = shift; my $fh; my $child_pid; my $reaper = $self->SigChildHandler(\$child_pid); $SIG{'CHLD'} = $reaper if $reaper; }
$ perl /tmp/t107 My PID is 18322 Running reaper. Running reaper. Running reaper. Running reaper. Running reaper. Running reaper. Running reaper. Running reaper. Running reaper. Running reaper.

In reply to Re: Re: Re: Net::Daemon and zombies by sgifford
in thread Net::Daemon and zombies by shushu

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.