How can I make Perl wait for child processes started in the background with system()?

前端 未结 3 2214
醉梦人生
醉梦人生 2021-02-14 10:41

I have some Perl code that executes a shell script for multiple parameters, to simplify, I\'ll just assume that I have code that looks like this:

for $p (@a){
           


        
3条回答
  •  离开以前
    2021-02-14 11:00

    Using fork/exec/wait isn't so bad:

    my @a = (1, 2, 3);
    for my $p (@a) {
       my $pid = fork();
       if ($pid == -1) {
           die;
       } elsif ($pid == 0) {
          exec '/bin/sleep', $p or die;
       }
    }
    while (wait() != -1) {}
    print "Done\n";
    

提交回复
热议问题