【发布时间】:2011-04-28 03:35:20
【问题描述】:
我做什么:
- 对 cgi 脚本进行 ajax 调用。
- Cgi 脚本分叉,但父级立即返回响应消息。
- 孩子进行了系统调用,但需要退出代码和任何错误消息。
伪代码:
$SIG{CHLD} = ‘IGNORE’; # or waitpid($pid,0) in the parent process
$pid = fork();
if($pid == 0)
{
close STDOUT; # So that the parent sends the response to the client right away.
@errorMsgs = qx(tar up big directories over 50G…); # This can go on for a few minutes.
if($? ==0) { Send a ‘success’ email } # Is always false ($? == -1)
else { Send a ‘failure’ email }
}
elsif($pid){ sendResponse; waitpid($pid,0) if $SIG{CHLD} != 'IGNORE'; exit;}
我的问题:
由于 ($SIG{CHLD} = 'IGNORE') 将 qx() 设置为 -1,因此无法从 qx() 获取正确的返回码 ($?) 和任何错误消息。如果我删除 $SIG{CHLD} 语句,客户端网页不会收到来自父级的响应消息,直到子级被收割之后。
【问题讨论】:
-
可能父输出被缓冲了。如果您忘记了 SIGCHLD 处理程序并确保设置了
$| = 1会发生什么? -
@mob:它在 Linux 上。我可以通过设置 $| 来发送响应但我读到不等孩子是一种不好的做法,它可能会导致僵尸。