#!C:/ActivePerl5.8.4/bin/perl.exe use strict; use warnings; require 5.8.4; #We only run if we're using Perl v5.8.4 $|++; #Unbuffer I/O my @pids=(); my $parentpid=$$; my $pid; print "The parents PID -s $parentpid\n"; my $thread_counter=0; my $testinput = "C:/Logchecks/test/forklist.txt"; my $testoutput = "C:/Logchecks/test/forkresults.csv"; open (IN,"<$testinput") or die("can't open input file $!\n"); open (OUT,">$testoutput") or die ("can't open output file $!\n"); OUTER: for (1..10) { $thread_counter++; $pid=fork(); if ($pid==0) { #child @pids=(); $pid=$$; $parentpid=0; last OUTER; }else{ push @pids,$pid; print "Parent has spawned child $pid\n"; } } if ($parentpid == 0) { #kid my $items=0; while (my $cname= ) { chomp $cname; $items++; print "$cname checked by $$\n"; print OUT "$cname,checked by $$\n"; sleep 1; } print "Thread $$ processed $items items.\n"; exit(0); } else { #parent print "$thread_counter threads started - waiting on completion.\n"; Reaper(); print "Parent: Goodbye(:-)\n"; exit(0); } sub Reaper { while (my $kid = shift(@pids)) { #warn "$$ to reap $kid\n" ; my $reaped = waitpid($kid,0); unless ($reaped != -1) { warn "waitpid $reaped: $?\n" ; } } }