江湖浪人 发表于 2015-12-27 13:20:30

Perl多进程程序的编写

用fork函数实现多进程。

fork()函数的行为是将目前进程复制一份,用于建立子进程。
其返回值,在子线程中是0,在父线程中是子线程号。
通过判断其返回值来控制各进程的行为。

例程:

my @pids;
my $pid = fork();

if (!defined($pid))
{
    print ("Fork process failured!\n");
    exit();
}
if ($pid == 0)
{
      push @pids, $pid;
  print ("This is a child process.\n");
}
else
{
  foreach (@pids) {
     waitpid($_,0); # 等待子线程
  }   
 print ("This is the parent process. All child processes have finished.\n");
}
页: [1]
查看完整版本: Perl多进程程序的编写