pcntl_fork()返回,致命错误:调用未定义的函数pcntl_fork()

Ben*_*rce 19 php fork command-line-interface pcntl undefined-function

我正在尝试使用pcntl_fork()分叉命令行运行XAMPP php进程.当我运行以下命令时:

$pid = pcntl_fork();
if($pid == -1){
    file_put_contents('testlog.log',"\r\nFork Test",FILE_APPEND);
    return 1; //error
}
else if($pid){
    return 0; //success
}
else{   
    file_put_contents($log, 'Running...', FILE_APPEND);
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

Fatal error: Call to undefined function pcntl_fork()
Run Code Online (Sandbox Code Playgroud)

谁能建议如何解决这个问题?

小智 29

当PHP用作Apache模块(例如XAMPP)时,不可能使用函数'pcntl_fork'.您只能在CGI模式或命令行中使用pcntl_fork.

使用此函数将导致:'致命错误:调用未定义的函数:pcntl_fork()'

资料来源:http://php.net/manual/en/function.pcntl-fork.php


Stu*_*nie 10

要查看它是否已安装,请运行:

php -i | grep pcntl

如果它存在并启用,那么pcntl函数可能被禁用,这似乎是较新的PHP 5.x安装中的默认值.要检查,请运行:

php -i | grep disable_functions

如果你看到一个pcntl_*函数列表,你需要编辑你的php.ini文件(在XAMPP内)并注释掉该行 disable_functions=

我建议你使用这个分布的OS X PHP,其中有最新版本,我可以证实确实有pcntl扩展.