一、進程處理函數
1、進程啟動函數
函數名 eval
調用語法 eval(string)
解說 將string看作Perl語句執行。
正確執行后,系統變量$@為空串,如果有錯誤,$@中為錯誤信息。
例子 $print = "print (/"hello,world//n/");";
eval ($print);
結果輸出 hello, world
函數名 system
調用語法 system(list)
解說 list中第一個元素為程序名,其余為參數。
system啟動一個進程運行程序并等待其結束,程序結束后錯誤代碼左移八位成為返回值。
例子 @proglist = ("echo", "hello,world!");
system(@proglist);
結果輸出 hello, world!
函數名 fork
調用語法 procid = fork();
解說 創建程序的兩個拷貝--父進程和子進程--同時運行。子進程返回零,父進程返回非零
值,此值為子程序的進程ID號。
例子 $retval = fork();
if ($retval == 0) {
# this is the child process
exit; # this terminates the child process
} else {
# this is the parent process
}
結果輸出 無
函數名 pipe
調用語法 pipe (infile, outfile);
解說 與fork合用,給父進程和子進程提供通信的方式。送到outfile文件變量的信息可以
通過infile文件變量讀取。步驟:
1、調用pipe
2、用fork將程序分成父進程和子進程
3、一個進程關掉infile,另一個關掉outfile
例子 pipe (INPUT, OUTPUT);
$retval = fork();
if ($retval != 0) {
# this is the parent process
close (INPUT);
print ("Enter a line of input:/n");
$line = <STDIN>;
print OUTPUT ($line);
} else {
# this is the child process
close (OUTPUT);
$line = <INPUT>;
print ($line);
exit (0);
}
結果輸出 $
program
Enter a line of input:
Here is a test line
Here is a test line
$
函數名 exec
調用語法 exec (list);
解說 與system類似,區別是啟動新進程前結束當前程序。常與fork合用,當fork分成兩個
進程后,子進程用exec啟動另一個程序。
例子
結果輸出
函數名 syscall
調用語法 syscall (list);
解說 調用系統函數,list第一個元素是系統調用名,其余為參數。
如果參數是數字,就轉化成C的整型數(type int)。否則傳遞字符串的指針。詳見UNIX的幫助Perl文檔。
使用syscall必須包含文件syscall.pl,即:
require ("syscall.ph");
例子
結果輸出
2、進程終止函數
函數名 die
調用語法 die (message);
解說 終止程序并向STDERR輸出錯誤信息。message可以為字符串或列表。如果最后一個參
數不包含換行符,則程序文件名和行號也被輸出。
例子 die ("Cannot open input file");
結果輸出 Cannot open input file at myprog line 6.
新聞熱點
疑難解答