Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the child process in perl?

Tags:

fork

perl

I have written a script which includes generation of gcov in parallel.I succeeded in that but it is creating 17 child process at a time.But i want to create only 6 child processes at a time,the 7th child should create after 1child process terminate.

sub gcov_parallel()
  2 {
  3     print "Generating Gcov...\n";
  4     my $kid;
  5     my $pid;
  6     @list = (@iucall,@iurcall_init,@iurcall_term,@iurcall_uti,@nob,@nobcch,@nobcell,@nobrrc,@nobcall,@rnccall,@cellin
    fo,@rnccom,@cellrrm,@uerrm,@uerrc,@uecall,@iupcded);
  7     my $len_list = scalar(@list);
  8     my $maxlen =0;
  9     my $count = 0;
 10     my $process = 0;
 11     $total_components = scalar(@comp_list);
 12
 13     for(my $comp_count=0; $comp_count < $len_list ; ($comp_count=$comp_count+$no_of_machines))
 14     {
 15         #limiting child process to 6
 16         if($process == 6)
 17         {
 18             $pid = wait();
 19             $process=$process-1;
 20         }
 21         else
 22         {
 23             $pid = fork();
 24             if($pid eq 0)
 25             {
 26                 for(my $files_count = 0; $files_count < $no_of_machines; $files_count++)
 27                 {
 28                     $count =  $files_count+$comp_count;
 29                     if($count < $len_list)
 30                     {
 31                         chomp($list[$count]);
 32                         my @list_gcda =`ls $list[$count]/*.gcda | sort`;
&generate_gcov("$list[$count]",@list_gcda);
 34                     }
 35                 }
 36                 wait();
 37                 exit;
 38             }
 39             $process=$process+1;
 40         }
 41     }
 42     do
 43     {
 44         $kid = waitpid(-1, 0);
 45     }while $kid > 0;
 46 }

But i observed while running the script it is skipping files while generating gcov.                                                         
like image 679
Nethra Avatar asked Mar 23 '23 23:03

Nethra


1 Answers

I think you could use Paralel::ForkManager to do this.

There is a good tutorial on PerlMonks about Paralel::ForkManager.

It could be this simple:

my $manager = Parallel::ForkManager->new( 6 );
   foreach my $command (@commands) {
      $manager->start and next;
      system( $command );
      $manager->finish;
   };
like image 150
user1126070 Avatar answered Apr 01 '23 21:04

user1126070