我有两个代码如何并行运行父命令中的两条命令?
1.
use File::Temp qw(tempfile);
$tmp = new File::Temp(UNLINK => 0);
system("tv_grab_au | tv_sort >> $file");
system("cp $file $HOME/.xmltv/listings.xml");
unlink($file);
2.
while (-e $file) {
sleep 2;
system("tvtime-command DISPLAY_MESSAGE \'Updating TV Guide. Please wait this might take a several minutes...\'");
}
我想这2个代码运行tv_grab_au XMLTV采集(更新电视指南)同时结合,而且,发送命令到tvtime以显示消息'更新电视指南。请等待这可能需要几分钟......',每两秒钟,直到$文件存在。
我试试这个:
use strict;
use warnings;
use File::Temp qw(tempfile);
my $file = new File::Temp(UNLINK => 0);
use POSIX qw(:sys_wait_h);
$|++;
defined(my $pid = fork) or die "Couldn't fork: $!";
if (!$pid) {
system("tv_grab_huro | tv_sort >> $file");
unlink($file);
}
else {
while (! waitpid($pid, WNOHANG)) {
system("tvtime-command DISPLAY_MESSAGE \'Updating TV Guide. Please wait this might take a several minutes...\'");
sleep 2;
}
}
感谢。
'fork'给孩子返回“0”。如果fork失败,它只会返回'undef'。 – mob 2012-01-05 22:42:57
你是对的,我会进行编辑。 – 2012-01-09 20:31:31