2012-08-31 34 views
2

这是我的代码,里面index.php(只是一个例子):如何与Apache使用pcntl_fork()?

$pid = pcntl_fork(); 
if ($pid == -1) { 
    die("failed to fork"); 
} else if ($pid) { 
    // nothing to do 
} else { 
    putDataIntoWebService(); 
    exit(); 
} 
echo "normal page content"; 

这段代码工作在命令行罚款。在Apache中exit()杀他们两个,家长和孩子的过程。什么是解决方法?

+0

请参阅http://stackoverflow.com/a/13105346/632951 – Pacerier

回答

1

这是解决方案:

posix_kill(getmypid(), SIGKILL); 

,而不是exit()

+2

正确的解决办法是使用'pcntl_wait'父和令状为孩子rpocesses到结束,以避免僵尸。建议的解决方案可能会也可能不工作,这取决于孩子们的信号处理程序,对SIGKILL生产代码中的进程SIGKILL并不真正“健康”,因为您无法在SIGKILL上发出信号,因此您可能会遗留资源在空中或使程序处于不一致的状态。 – gphilip

4

你不能用PHP的Apache模块版本使用pcntl_*功能。从pcntl_fork documentation评论引述:

It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.

Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'

+0

您指的是在php.net上的评论,而不是官方文档的一部分。 'pcntl_fork()'不会工作在Apache中,但不创建一个正常的独立进程,我猜。 – yegor256

+1

我以前尝试过。我收到了评论描述的错误。 – Hubro

+0

你应该安装'pcntl'扩展,然后 – yegor256