当前位置:首页 » Perl » Perl fork()函数

Perl fork()函数

perl fork()函数,fork()函数学习例子,fork()函数实例代码,fork()函数在线教程等

语法

fork


定义和用法

使用fork()系统调用fork一个新进程。任何共享的套接字或文件句柄是重复整个过程。你必须确保你等待你的子进程,以防止形成“僵尸”进程。

返回值

  • undef - 开一个fork进程失败

  • 返回子进程ID-如果父进程成功 ,0 - 如果子进程成功

例子

以下是用法...

#!/usr/bin/perl

$pid = fork();
if( $pid == 0 ){
   print "This is child process\n";
   print "Chile process is existing\n";
   exit 0;
}
print "This is parent process and child ID is $pid\n";
print "Parent process is existing\n";
exit 0;

#This will produce following result
#by www.gitbook.net

This is parent process and child ID is 16417
Parent process is existing
This is child process
Chile process is existing