进程管理
上一篇 /
下一篇 2008-11-17 21:45:13
/ 个人分类:perl
- 文件版本: V1.0
- 开发商: 本站原创
- 文件来源: 本地
- 界面语言: 简体中文
- 授权方式: 免费
- 运行平台: Win9X/Win2000/WinXP
通过perl直接启动其它程序。51Testing软件测试网"?A4__c/o"`}'Wj
!](ew yk~
|0system函数
x2C/vk)_~1L?th0system "date"; 启动unix系统的date命令。51Testing软件测试网M-KB
kr*]1G4z&D
子进程会运行date命令,它将继承perl的标准输入,标准输出和标准错误。
(q.E$Hs(n7]U0O0h9b/p L]0system 'ls -l $HOME'; 注意是用单引号,因为$HOME是shell变量,否则,shell就看不到美元符号。表明要替换的符号。
7R-Y(o k BWn E0system "long_time_command&"; 把长时间运行的程序放在后台。51Testing软件测试网
P3_b hu*n
system 'for i in *; do echo ==$1 ==; cat $i; done'; 可以写脚本51Testing软件测试网&\C2_7H_v$g*o
51Testing软件测试网htFS| Bj;c
避免shell
EhD!H.K \D0调用system操作符时带多个参数,此时shell就不会卷入。如:51Testing软件测试网$[y.j[-}2~ E
system "tar","cvf",$aaa,@bbb; 第一个命令是tar,其余的参数会一个一个传递给它。
l1_/Ug(Z bhm'ev+Q0system的退出状态基于子进程的退出状态。在unix中0表示正常,非0表示出错。51Testing软件测试网ww,?Q%sl!?l!I.P
unless (system "date") { 返回0表示成功51Testing软件测试网am#N8^u_
print "we gave you a date,ok!\n";
d1lMb-Mp7b0 }
9~!Z:a!Y$Ol
D*{
v051Testing软件测试网qA@$M]"y
exec函数
b!lT/F+XY7J{0与system差不多,system会生成一个子进程,exec是让perl进程本身去处理所要求的动作。51Testing软件测试网2vv&H3BA!|z9h/[s
一般用system就可以了。
P\M\$K0
q8PRndV.a0环境变量
q(J7`y6}y-~ [0当你启动一个新进程时,环境变量就被继承下来了。在perl中,通过特殊的%ENV散列得到环境变量,散列中每个键表示一个环境变量。在你的程序刚开始执行时,%ENV就从父进程(通常是shell)继承而来。修改这个散列就改变了环境变量,它又会被新进程继承。$ENV {'PATH'} = "/home/mydir/bin:$ENV{'PATH'}"; 设置新的环境变量,增加了一个路径
4{'E?
S}6x9|0delete $ENV{"IFS"}; 删除“IFS”这个路径51Testing软件测试网Pa$eb4@$?[T
my $make_result = system "make"; 在新环境变量中执行程序51Testing软件测试网)[d2aspvA Xw:\v'g
51Testing软件测试网#HX,E!UC;r3M
使用反引号捕获输出51Testing软件测试网U'{ F+AO
当使用system and exec时,所启动命令的输出都被送到perl的标准输出上。有时我们需捕获这些输出。
[h}1bK"K%Q})q0my $now = `date`;51Testing软件测试网)G-[zW%]g$U
print "the time is now $now."; 已经有换行符,不用加\n。51Testing软件测试网W;X a3}*q%s
与shell差不多。但它把行尾去掉,而perl的输出包含\n。所以要得到同样的效果,需加上chomp操作。
#Gh0p0_5Iqw1hg051Testing软件测试网p0A1J9qMZ/J
在列表上下文中使用反引号51Testing软件测试网Pj,FA3sJrn
my $who_text = `who`; 标量上下文,得到一个长字符串。 51Testing软件测试网:_$ka$z?zpt o
my @who_lines = `who`; 列表上下文,得到一个按行分开的数据。
Ex7Oz M0
+R0|3w$I3No5e@
e0文件句柄形式进程
P6wj|&j \0perl可以启动一个处理活动状态的子进程。启动一个并发子进程的语法是把命令当做“文件名”用在open调用中,在命令之前或之后加一个竖线,这是一个“管道”字符,因些,这通常被称为管道打开(piped open)。51Testing软件测试网C,f
B+z*[
V n#[
open DATE, "date|" or die "cannot pipe from date:$!";
"lSak/a)c%L|&t6a+CO0竖线在右边,其标准输出与文件句柄DATE连接,就像shell中的date | your_program。51Testing软件测试网eCh2a,L7S2~7\(FO
f
open MAIL, "|mail merlyn" or die "cannot pipe to mail:$!";51Testing软件测试网 A?2~L7T
竖线在左边,命令的标准输入文件句柄MAIL连接,就像shell中的your_program | mail。
Yr@y#r6[P1K0命令启动后是个独立于perl的进程。
m#mI%wI`!r0
!B5wFu(eR7br0要读取一个为读而打开的文件句柄,我们只需进行普通的读:
,U6[^3i:~F3I/|O+\_0my $now = <DATE>;
HXJ`Kj/E?0要想给邮件进程发送数据,一人简单的“带文件句柄的打印”就可以了:
L$d1CjY+[0print MAIL "the time is now $now.";
Tdz`*PE&E$o0W0
Ppao8Bc:T[0用fork进行深入和复杂的工作
9q['R7D9Km,g0用低级系统调用实现system "date";命令。
]$n {%r(s7_9D~0defined (my $pid = fork ) or die "cannot fork:$!";
{{7M?.Kdj$L@3q0unless ($pid) {51Testing软件测试网Q0bS)]x
exec "date";
Xd%xJz0 die "cannot exec date:$!";51Testing软件测试网7qFzF{+Jq
}
H.?"Xy6b m2Hv ^0waitpid($pdi.0);51Testing软件测试网a%f(a&T;o6@T
6h$xmNvf?P'K0发送和接收信号51Testing软件测试网;RL;I{5\M.v
向4201发送一个SIGINT。51Testing软件测试网5uAxY\
kill 2, 4201 or die "cannot signal 4201 with SIGINT:$!";51Testing软件测试网[&iprQb1j
你也可用“INT”替代这里的2,因为2号信号就是SIGINT。
^"U F}3b"OTG0
x'IJ:V5F'H2|7LA0信号0表示,看看我能不能发一个个信号,但我并不想现在发送。因此可用以进程探测。
:pCgJ;e@ZN0unless (kill 0,$pid) {
.IKm6g%j"R+x0 warn "$pid has gone away!";51Testing软件测试网d&UC%L+YMELk){!y
}
相关阅读:
- Use the Net::SMTP module to send an email through an ms exchange smtp server (intothestorm, 2008-11-12)
- Use perl to simulate tail command (intothestorm, 2008-11-12)
- Use the Net::Telnet module (intothestorm, 2008-11-12)
- Use the Net::FTP module (intothestorm, 2008-11-12)
- Silent installation-版本维护安装的福音 (intothestorm, 2008-11-12)
- checkRelease.pl (intothestorm, 2008-11-12)
- ftp.pl (intothestorm, 2008-11-12)
- silentlyInstallPanINM.pl (intothestorm, 2008-11-12)
- interactivelyInstallPanINM.pl (intothestorm, 2008-11-12)
- email.pl (intothestorm, 2008-11-12)
收藏
举报
TAG:
Perl
perl