后台进程
今天遇到一个perl启动后台进程的问题,就是是在启动命令后面加"&"的进程还是顺序,特做此小实验。
a.pl调度
View Code
#!/usr/bin/perl -w use Data::Dumper; my $cmd1="perl b.pl &"; my $cmd2="perl b.pl &"; my @result=`$cmd1`; my @result1=`$cmd2`; #system($cmd1); #system($cmd2); print Dumper(@result),"\n"; print Dumper(@result1),"\n";
这种方式是确实是顺序执行的,但是用system调试是并行的。
b.pl
View Code
#!/usr/bin/perl -w print $$." begin ".current(),"\n"; sleep 20; print $$." end ".current(),"\n"; sub current { my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); $year += 1900; $mon ++; ($sec,$min,$hour,$mday,$mon,$year) = format_date($sec,$min,$hour,$mday,$mon,$year); return "$year-$mon-$mday $hour:$min:$sec"; } sub format_date { my(@items) = @_; my $pattern = "[-\/ :]+"; foreach my $item (@items) { $item = (length($item)==1) ? "0$item":$item; } return @items; }