當前位置:首頁 » 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