原文:
18.5. How do you fork on System V (HP)?
Kraegeloh Martin <mkr@dm-server.cv.com> originally asked: ! Subj: signal handling difference on HP vs. SUN!! the following code will fork an xterm with vi in it, and it! will refuse to do so while the first xterm is still running.! works fine on my sun.! On HP however, the second time an xterm is started, NO handler! is called when the child dies.!! the code:! ===================== 8< ===============================! $SIG{CHLD}=\&w;!! sub w{! $pid=wait;! print STDERR "died: $pid\n";! if ( $have == $pid ) { $have = 0; }! }
To which a part of Nick Ing-Simmons' response was:
I suspect HPUX is SysV-ish not BSD or POSIX. So every time a signal fires, it removes the handler - you need to reset it in the handler:
sub w{ $SIG{CHLD}=\&w; $pid=wait; print STDERR "died: $pid\n"; if ( $have == $pid ) { $have = 0; } }
Whether you reset it before/after the wait may be very important ...
Then Bjarne Steinsbo <bjarne@hsr.no> followed up with:
That's not the whole story... Another problem is that SIGCLD interrupts the read system call on SysV-ish (I like that word! :-) systems. This means that you have to test why "" fails, and act accodingly. A program that works on both Sun and HP is:
$SIG{CHLD}=\&w; while(1){ $_ = ; $! = 0, next if $! =~ /Interrupted/; last if $! or !defined $_; if($have){ print STDERR "child still alive\n"; } else{ if(($pid=fork()) != 0){ $have=$pid; print STDERR "forked $pid\n"; } else { exec("xterm -e vi") } } } sub w{ $pid=wait; print STDERR "died: $pid\n"; if ( $have == $pid ) { $have = 0; } $SIG{CHLD}=\&w; }
译文:
18.5. 在HPUX系统上如何使用fork函数?
Kraegeloh Martin最初问到:
!主题:在HP和SUN系统上关于信号控制的不同
!
!下面的代码将产生一个进行vi操作的xterm子进程,但是当第一个xterm仍然在运行时,
!这个程序将不会正确执行。
!在我的Sun系统上它运行良好,但是在HP系统上,当第二个xterm开始后,子进程死亡时
!没有句柄被调用。(?译者注:老实说我没有完全理解他的意思……)
!
!程序代码:
!=================================================================
!$SIG{CHLD}=\&w;
!
!sub w{
! $pid=wait;
! print STDERR “died: $pid\n”;
! if($have == $pid){ $have = 0;}
!}
下面是Nick Ing-Simmons对于这个问题的部分的回应:
我怀疑HPUX的系统是类似Sys-V的,而不像BSD或者POSIX。因此每次一个信号发出以后,句柄就自动被删掉了——你需要重置句柄:
sub w{
$SIG{CHLD}=\&w;
$pid=wait;
print STDERR "died: $pid\n";
if ( $have == $pid ) { $have = 0; }
}
这里,在wait之前还是之后进行重置很重要……
然后,Bjarne Steinsbo跟着回答到:
这还不完全……另一个问题是,SIGCLD在类Sys-V的系统(我喜欢这个词!:-))上会打断系统的读入。这意味着你必须测试为什么””失败了,然后如何进行(译者注:实在不懂……:()。下面的程序可以在Sun和HP系统上都正常运行:
$SIG{CHLD}=\&w;
while(1){
$_ = ;
$! = 0, next if $! =~ /Interrupted/;
last if $! or !defined $_;
if($have){
print STDERR "child still alive\n";
}
else{
if(($pid=fork()) != 0){
$have=$pid;
print STDERR "forked $pid\n";
}
else {
exec("xterm -e vi")
}
}
}
sub w{
$pid=wait;
print STDERR "died: $pid\n";
if ( $have == $pid ) { $have = 0; }
$SIG{CHLD}=\&w;
}
(译者注:因为本人没有用过HPUX系统,所以没办法试验了……)