如何让子进程向父进程传输数据
1个回答
展开全部
# create a pipe for IPC
pipe(CHILD_READ,CHILD_WRITE) or die "Can't make pipe!\n";
#-- Parent Process maintain read the input of child
my $IN = IO::Select->new(\*CHILD_READ);
$SIG{CHLD} = \&reap_child;
while(!$DONE){
if($IN->can_read){ # get a message from one of the children
my $message;
next unless sysread(CHILD_READ,$message,4096);
my @messages = split /\n/,$message;
foreach (@messages){
next unless my ($pid,$status) = /^(\d+) (.+)$/;
if($status ne 'done'){
$STATUS{$pid} = $status;
}else{
delete $STATUS{$pid};
}
}
}
print join(" ",map {"$_ => $STATUS{$_}"} keys %STATUS )."\n";
#----- do nothing,if no child alive, -----
unless(%CHILDREN){
$normal = 1;
last;
}
}
##-------假如非正常结束,必须删除所有子程序
if($normal ==0){ kill_children(); exit 1;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询