Perl fork 函数

  • 描述

    fork 使用fork()系统调用派生一个新进程。任何共享的套接字或文件句柄都会在进程之间复制。您必须确保等待您的子进程,以防止形成“僵尸”进程。
  • 句法

    以下是此函数的简单语法-
    
    fork
    
  • 返回值

    如果fork进程失败,将返回undef;如果成功,将返回子进程ID为父进程;如果成功,将返回子进程ID。
  • 示例

    以下是显示其基本用法的示例代码-
     
    $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 19678
    Parent process is existing
    This is child process
    Child process is existing