Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't my Perl program reaping children processes after fork?
    primarykey
    data
    text
    <p>I have been trying to write a bare-bones ping scanner using Perl for internal use. Since it scans a 24-bit CIDR network the script takes too long to run if it runs in a single thread. I have tried adding fork functionality to speed up the process but my <a href="http://pastebin.com/fc457b0b" rel="nofollow noreferrer">first attempt</a> was taking pretty much the same time since there was only one child process active at any given time.</p> <p>I read up on child processes in the <a href="http://perldoc.perl.org/perlipc.html" rel="nofollow noreferrer">perlipc</a> document and also in the <a href="http://oreilly.com/catalog/9780596003135/" rel="nofollow noreferrer">Perl Cookbook</a> and came up with the second version:</p> <pre><code>##Install the CHLD SIG handler $SIG{CHLD} = \&amp;REAPER; sub REAPER { my $childPID; while (( $childPID = waitpid(-1, WNOHANG)) &gt; 0) { print "$childPID exited\n"; } $SIG{CHLD} = \&amp;REAPER; } my $kidpid; for (1 .. 254) { my $currIP = join ".", (@ipSubset[0,1,2], $_); die "Could not fork()\n" unless defined ($kidpid = fork); if ($kidpid) { #Parent process #Does nothing } else { #Child process my $pingConn = Net::Ping-&gt;new(); #TCP say "$currIP up" if $pingConn-&gt;ping($currIP); $pingConn-&gt;close(); #Nothing else to do exit; } } say "Finished scanning $ipRange/24"; </code></pre> <p>When I scan my internal network the output is:</p> <pre><code>$perl pingrng2.pl 192.168.1.1 192.168.1.2 up 5380 exited 192.168.1.102 up 192.168.1.100 up 5478 exited 5480 exited Finished scanning 192.168.1.1/24 </code></pre> <p>As can be seen in the result, the threads which do a successful scan print the "up" message, exit cleanly and are reaped by the parent process. The other 251 threads meanwhile are left dangling attached to '/sbin/init' as can be seen by a quick 'ps -ef' listing. If I add a 'print "Child: $currIP ending\n"' in the child processing block just before the exit statement I get the output from the remaining 251 processes on my terminal "after" my perl script has exited. </p> <p>What's going on here? I thought that the $SIG{CHLD} subroutine coupled with the waitpid loop would reap all the child processes and ensure that no zombies/dangling processes were left in the system. </p> <p>In the same breath I would also like to be able to run a specific number of child processes at any given time, say for example, 'n' children running concurrently, whenever one exits the parent process starts another child if needed but has no more than 'n' children at any given moment. Is this possible? If yes could I get some pseudo-code to help guide me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload