您的位置:首页 > 其它

Perl多进程程序的编写

2012-02-16 16:56 302 查看
用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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: