Currently I'm attempting to read a file line by line and fork processes using perl
Basically my perl script "test.pl" executes another script using exec the way it should work is the script "run.sh" should be executed for every 5 lines of the file using the file lines as command line arguments in the exec function then wait for those 5 processes to complete and continue.
Line: 1 - run.sh executed
Line: 2 - run.sh executed
Line: 3 - run.sh executed
Line: 4 - run.sh executed
Line: 5 - run.sh executed - WAIT HERE for "run.sh" to complete then continue
Line: 6 - run.sh executed
Line: 7 - run.sh executed
Line: 8 - run.sh executed
Line: 9 - run.sh executed
Line: 10 - run.sh executed
Unfortunately it does not work as intended and it seems to read random lines as oppose to the lines in sequence I believe this is due to forking the process but am not sure how to resolve it.
This question may be related Fork in Perl not working inside a while loop reading from file but I'm still really confused.
test.txt
5-6
7-1
5-9
1-22
8-55
9-6
3-5
7-1
5-9
1-22
8-55
9-6
3-5
test.pl
$count = 0;
open (MYFILE, 'test.txt');
while (<MYFILE>) {
$data = $_;
$data =~ s/\R//g;
chomp();
my $pid = fork();
if ($pid == -1) {
die;
} elsif ($pid == 0) {
exec './run.sh', "-r $data" or die;
print $count;
}
if( ($count%5) == 0 ){
while (wait() != -1) {}
}
$count ++;
}
close (MYFILE);
run.sh
sleep 5
echo "I was passed the following arg:"
echo $1
echo $2