Tuesday, October 22, 2013

Concurrent ping with fork (PERL)

#!/usr/bin/perl
use strict;
use warnings;
 
print "Starting main program\n";
my @childs;
  
for ( my $count = 1; $count <= 500; $count++)
{
 my $pid = fork();
 if ($pid)
 {
  #parent
  print "pid is $pid, parent $$\n";
  push(@childs, $pid);
 
 }
 elsif ($pid == 0)
 {
  #child
  system("ping 128.0.10.127");
  exit 0;
 }
 else
 {
  die "couldnt fork: $!\n";
 }
}
foreach (@childs)
{
 my $tmp = waitpid($_, 0);
 print "done with pid $tmp\n";
}
print "End of main program\n";

No comments: