我该如何向一个分叉的孩子发出终止Perl信号的信号? [英] How can I signal a forked child to terminate in Perl?
本文介绍了我该如何向一个分叉的孩子发出终止Perl信号的信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在分叉的进程之间共享相同的变量?还是我需要在父文件中写入文件,然后在文件存在后读取在子文件中保存的值? $ something似乎从未设置过,因此它只是在睡眠中循环
How can I make the same variable shared between the forked process? Or do I need to write to a file in the parent then read the value saved to the file in the child once the file exists? $something never appears to get set in this so it just loops in the sleep
my $something = -1;
&doit();
sub doit
{
my $pid = fork();
if ($pid == 0)
{
while ($something == -1)
{
print "sleep 1\n";
sleep 1;
}
&function2();
}
else
{
print "parent start\n";
sleep 2;
$something = 1;
print "parent end: $something\n";
}
}
sub function2 {
print "END\n";
}
推荐答案
文件描述符(有时锁定这些描述符)是共享的,而其他所有内容都将被复制.
File descriptors (and sometimes locks on those descriptors) are shared, while everything else is copied.
另请参见perldoc perlipc
中的与您自己进行双向通信.
更新:再三考虑,您想要这样的东西吗?
Update: On second thought, do you want something like this?
#!/usr/bin/perl
use strict;
use warnings;
my $pid = fork;
die "Cannot fork: $!" unless defined $pid;
if ($pid == 0) {
print "Child start\n";
my $end;
local $SIG{HUP} = sub { $end = 1 };
until ($end) {
print "Sleep 1\n";
sleep 1;
}
function2();
}
else {
print "Parent start\n";
sleep 5;
kill HUP => $pid;
waitpid($pid, 0);
}
sub function2 {
print "END\n";
}
输出:
C:\Temp> w
Parent start
Child start
Sleep 1
Sleep 1
Sleep 1
Sleep 1
Sleep 1
END
这篇关于我该如何向一个分叉的孩子发出终止Perl信号的信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文