描述

该函数使用fork()系统调用分叉一个新进程。所有共享Socket或文件句柄在进程之间都是重复的。您必须确保等待孩子,以防止形成"僵尸"进程。

语法

以下是此函数的简单语法-

fork

返回值

如果分叉失败,则此函数返回undef;如果分叉失败,则将子进程ID返回给父进程;如果分叉成功,则返回子进程ID。

以下是显示其基本用法的示例代码-

#!/usr/bin/perl

$pid=fork();
if( $pid == 0 ) {
   print "This is child process\n";
   print "Child 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 is parent process and child ID is 18641
Parent process is existing
This is child process
Child process is existing

参考链接

https://www.learnfk.com/perl/perl-fork.html