Perl 中的 fork函数

首页 / Perl入门教程 / Perl 中的 fork函数

描述

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

语法

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

无涯教程网

fork

返回值

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

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

链接:https://www.learnfk.comhttps://www.learnfk.com/perl/perl-fork.html

来源:LearnFk无涯教程网

#!/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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

程序员进阶攻略 -〔胡峰〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

职场求生攻略 -〔臧萌〕

Redis核心技术与实战 -〔蒋德钧〕

React Hooks 核心原理与实战 -〔王沛〕

大数据经典论文解读 -〔徐文浩〕

网络排查案例课 -〔杨胜辉〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

好记忆不如烂笔头。留下您的足迹吧 :)