简体   繁体   中英

Reading a file line-by-line fork/exec - Perl

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

Your description of the problem ("it seems to read random lines") is atrociously vague.

Your code has numerous problems, many of which prevent it from even compiling.

Yet, I believe I can still answer your question:

There's no guarantee that the children will end in the order in which they were started.

You'll get the output of the first 5 childrenin any order (even intermixed), then you'll get the output of the next 5 children in any order (even intermixed), etc.

If you want to output the results in a specific order, you will need to collect the output of the children and reprint it after reordering the output based on the order in which the child that produced it was launched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM