+ -
当前位置:首页 → 问答吧 → 如何在perl中并行的执行两个程序?

如何在perl中并行的执行两个程序?

时间:2010-12-22

来源:互联网

本帖最后由 flywithclouder 于 2010-12-22 09:21 编辑

我想在perl中执行多个外部程序,如下:
  1. system(“test.exe args”);
  2. system(“test2.exe args”);
复制代码
但在perl中这两句话是顺序执行的,其实这两个程序完全独立,为提高效率我想并行执行,

请问该如何实现?

作者: flywithclouder   发布时间: 2010-12-22

fork进程同步再exec

作者: maxxfire   发布时间: 2010-12-22

本帖最后由 Cu_fans 于 2010-12-22 11:27 编辑
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;

  4. my @arr = ("test.exe args", "test2.exe args");

  5. my @pidt;
  6. for my $i(0 .. $#arr-1)
  7. {
  8.         $pidt[$i] = fork();
  9.         if ($pidt[$i] < 0)
  10.         {
  11.                 print  "fork err","\n";
  12.                 exit(-1);
  13.         }
  14.         elsif ($pidt[$i] == 0)
  15.         {
  16.                 system($arr[$i]);
  17.         }
  18. }
复制代码
把perl写成了C, 正宗的perl中对多进程是如何处理的?

作者: Cu_fans   发布时间: 2010-12-22