例1:
#!/usr/bin/perl
#
my $pid = fork();
if (!defined($pid))
{
print ("Fork process failured!\n");
exit();
}
if ($pid)
{
# This is the child process.
sleep(1);
print ("exit child after 10 seconds wait!\n");
exit();
}
else
{
# This is the parent process.
print ("exit parent!\n");
例2:
define(my $pid=fork) or die "cannot fork: $!";
unless ($pid){
##子进程在此
exec "date";
die "cannot exec date : $!";
}
##父进程在此
waitpid($pid, 0);
##wait/waitpid 函数用于回收子进程,只有在server模式下才有必要调用,你进程创建子进程后,在子进程退出时,它有义务负责回收,否则会造成很多僵尸进程。
wait与waitpid区别:
wait是阻塞的, 一直在等待..
waitpid带wnohang参数,可用于非阻塞
阅读(1011) | 评论(0) | 转发(0) |