Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 218395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:44:48+00:00 2026-05-11T18:44:48+00:00

I have been trying to write a bare-bones ping scanner using Perl for internal

  • 0

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 first attempt was taking pretty much the same time since there was only one child process active at any given time.

I read up on child processes in the perlipc document and also in the Perl Cookbook and came up with the second version:

##Install the CHLD SIG handler
$SIG{CHLD} = \&REAPER;
sub REAPER {
    my $childPID;
    while (( $childPID = waitpid(-1, WNOHANG)) > 0) {
        print "$childPID exited\n";
    }
    $SIG{CHLD} = \&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->new();    #TCP
        say "$currIP up" if $pingConn->ping($currIP);
        $pingConn->close(); 

        #Nothing else to do
        exit;
    }
}

say "Finished scanning $ipRange/24";

When I scan my internal network the output is:

$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

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.

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:44:48+00:00Added an answer on May 11, 2026 at 6:44 pm

    It looks like your parent process is finishing before the children (and therefore never getting a chance to reap them). Try this instead:

    #!/usr/bin/perl
    
    use 5.010;
    use strict;
    use warnings;
    
    use Net::Ping;
    
    my @ipSubset = (192, 168, 10);
    
    my $i = 0;
    my @pids;
    for my $octet (1 .. 254) {
        my $currIP = join ".", @ipSubset[0 .. 2], $octet;
    
        die "Could not fork()\n" unless defined (my $pid = fork);
    
        #parent saves chlidren's pids and loops again
        if ($pid) {
            push @pids, $pid;
            next;
        } 
    
        #child process
        my $pingConn = Net::Ping->new;
        say "$currIP up" if $pingConn->ping($currIP);
        $pingConn->close(); 
        exit;
    }
    
    #wait on the children
    for my $pid (@pids) {
        waitpid $pid, 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update Looks like Apple made an IETF draft proposal, and… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer Are you managing your context with an HttpHandler or HttpModule?… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer dataset[country] = {label: countryName, data: theDataObject}; with var country =… May 11, 2026 at 11:30 pm

Related Questions

I have been trying to write a C program which generates all possible permutations
I have been trying to write a small app with its own option windows.
I am new to programming. I have been trying to write a function in
I am fairly new to c# and am trying to write a n-tiered web

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.