使用pcntl_fork子进程无法访问从父
我试图使用代码点火器库写一个守护进程,问题是,当我用叉子叉守护进程就无法再访问该CI实例和所有的图书馆创建的CodeIgniter实例这是在__construct初始化,也无法访问加,如果我创建子进程中的一个新的CI实例,我仍然无法访问任何库,我得到以下错误:使用pcntl_fork子进程无法访问从父
错误 - 2012-02-27 00:13: 07 - >严重性:警告 - >包括(应用/错误/ error_general.php):未能打开流:没有这样的文件或目录/srv/http/FreeFileConvert/system/core/Exceptions.php 146 错误 - 2012 -02-27 00:13:07 - >严重性:警告 - > include():打开'application/errors/error_general失败。 PHP的 '列入(include_path中=':在/ usr /共享/梨')/srv/http/FreeFileConvert/system/core/Exceptions.php 146
这里是我的代码:
class Conversion_workers
{
function __construct() {
$this->ci =& get_instance();
$this->ci->load->library('Gearman');
}
private function is_locked($lock_file) {
if(file_exists($lock_file)) {
$lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
if (! flock($lock, LOCK_EX | LOCK_NB)) {
flock($lock, LOCK_UN);
return TRUE;
}
}
return FALSE;
}
private function lock_file($lock_file, &$lock) {
$lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
if (! flock($lock, LOCK_EX | LOCK_NB)) {
//error_log('Unable to lock file.');
return FALSE;
}
fseek($lock, 0);
ftruncate($lock, 0);
fwrite($lock, posix_getpid());
fflush($lock);
return TRUE;
}
private function daemonize($lock_file, &$lock, &$parent)
{
// TODO: In the install check if pcntl_fork supported
if($this->is_locked($lock_file)) {
$parent = TRUE;
return FALSE;
}
$pid = pcntl_fork();
if($pid < 0) {
log_message('error', 'Unable to fork process');
return FALSE;
}
// If we got a good PID, then we can exit the parent process.
if($pid > 0) {
log_message('info', 'Exiting parent as process forked successfully');
$parent = TRUE;
return FALSE;
}
ob_start();
// Change the file mode mask
umask(0);
// Create a new SID for the child process
if (posix_setsid() < 0) {
//error_log('Unable to create a new SID for child process');
return FALSE;
}
// Change the current working directory
if(chdir("/tmp") < 0) {
//error_log('Unable to change directory of the daemonize process');
return FALSE;
}
// Lock in child process to get correct pid in lock file
if(!$this->lock_file($lock_file, $lock)) {
exit;
}
fclose(STDIN); // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.
register_shutdown_function(create_function('$pars',
'ob_end_clean();posix_kill(posix_getpid(), SIGKILL);'), array());
// Might be good idea to have register_shutdown_function() here if we want to
// check status when the daemon terminates.
return TRUE;
}
private function start_workers() {
$lock_file = $this->ci->config->item('lock_file');
$parent = FALSE;
if(! $this->daemonize($lock_file, $lock, $parent)) {
if($parent)
return;
} else {
// Start a worker
$this->ci->gearman->gearman_worker();
$this->ci->gearman->add_worker_function('some_function', 'some_function_fn');
error_log('Starting worker ['.posix_getpid().']');
while($this->ci->gearman->work());
}
error_log('Exiting worker');
exit;
}
function add_to_queue($function_name, $params) {
$this->start_workers();
$this->ci->gearman->gearman_client();
return $this->ci->gearman->do_job_background($function_name, serialize($params));
}
} // END class Controller
功能“add_to_queue()”的作品不错,但问题是“start_worker”这分叉后刚刚停止工作,我甚至无法访问日志的辅助功能,这就是为什么我在我的代码使用error_log中()。
我将不胜感激,如果有人可以帮助请。
也许不会是守护进程方法更改工作目录/tmp
?在这种情况下,系统正在寻找.
(/ tmp)和/usr/share/pear
中的包含,这两者都不能保留它们?
也许http://www.php.net/manual/en/ini.core.php#ini.include-path会有所帮助。