目录

fork

描述 (Description)

此函数使用fork()系统调用强制执行新进程。 任何共享套接字或文件句柄都跨进程重复。 你必须确保等待你的孩子,以防止形成“僵尸”过程。

语法 (Syntax)

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

fork

返回值 (Return Value)

此函数在成功时将fork和子进程ID返回到父成功0到子进程时返回undef。

例子 (Example)

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

#!/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
↑回到顶部↑
WIKI教程 @2018