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