Perl fork 函式



說明

此函式使用 fork( ) 系統呼叫生成一個新程序。程序之間會複製所有的共享套接字或檔案控制代碼。您必須確保等待您的子程序,以防出現“殭屍”程序。

語法

以下為此函式的簡要語法 −

fork

返回值

fork 失敗則此函式返回 undef,成功後返回子程序 ID 給父程序,0 給成功後的子程序。

示例

以下是顯示該函式基本用法的示例程式碼 −

#!/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
perl_function_references.htm
廣告
© . All rights reserved.