In Perl, how can I block for a bunch of system calls to finish?

前端 未结 3 1971
-上瘾入骨i
-上瘾入骨i 2020-12-09 12:14

I\'m doing a bunch of system calls that I want to run in parallel:

system(\" sleep 5 && echo step 1 done &\");
system(\" sleep 3 && echo          


        
相关标签:
3条回答
  • 2020-12-09 12:50

    what about running each one of the system call from a different thread and join on the threads

    0 讨论(0)
  • 2020-12-09 13:00

    The easiest way to do this is probably to fork a new child process for each system call, and then wait for them to finish. Here's a simplified example:

    my @commands = ( "sleep 5 && echo step 1 done",
                     "sleep 3 && echo step 2 done",
                     "sleep 7 && echo step 3 done" );
    
    my @pids;
    foreach my $cmd( @commands ) {
        my $pid = fork;
        if ( $pid ) {
            # parent process
            push @pids, $pid;
            next;
        }
    
        # now we're in the child
        system( $cmd );
        exit;            # terminate the child
    }
    
    wait for @pids;   # wait for each child to terminate
    
    print "all done.\n";
    
    0 讨论(0)
  • 2020-12-09 13:08

    Fork a child process to perform each job, and in the parent, wait for those processes to finish before exiting.

    See perldoc perlfork, perldoc -f fork and perldoc -f waitpid.

    (The exact code for this is left as an exercise for the reader -- everyone should have to write this from scratch at least once, to understand the details involved. There are lots of examples on this site as well.)

    0 讨论(0)
提交回复
热议问题