我有一个通过Net :: Server模块运行TCP侦听器的perl脚本。当远程连接到perl服务器时,远程发送要播放的mp3音乐文件的文件名。当我fork()然后调用system('mpg123 $ filename')时,客户端挂起。如何设置mpg123进程,以便孩子可以关闭连接?
my $pid = fork();
if (defined $pid && $pid == 0)
{
# child process -- never gets to print statement until $cmd is done
system ($cmd);
print STDERR "child launched\n";
exit (0);
}
答案 0 :(得分:1)
Perl的system
在命令完成之前不会返回。您可以将孩子重新安排到
if (defined $pid && $pid == 0)
{
# child process
warn "child launched\n";
exec $cmd or die "$0: exec $cmd: $!";
}
答案 1 :(得分:0)
使用Proc :: Daemon
结束#!/usr/bin/perl -w
use strict;
use Proc::Daemon;
my $dm = Proc::Daemon->new( work_dir=>'/tmp/');
my $pid = $dm->Init( { exec_command => '/usr/bin/find / >/tmp/find.txt', } );
while (1)
{
print "child status :".$dm->Status($pid)."\n";
sleep 2;
if ($dm->Status($pid) eq 0)
{
print "child terminated :".$dm->Status($pid)."\n";
last;
}
}