I wrote daemon script in php, which works using hosted server. I haven't got access to shell, so I execute it using popen function and CLI
pclose(popen('/usr/bin/php full_path/index.php daemon
start|exit &', 'r'));
Everything works fine, except it ignores usleep(5000000)
while (!$this->_sigterm) {
pcntl_signal_dispatch();
// Save the total running children
$processes_running = count($this->_pids);
// See if we are within our defined child limits.
if($processes_running >= $this->_config['max']) {
// Now lets pause.
$this->iterate();
continue; // Restart.
}
// Lets get the next task
if(($task = Tasks::getNextTask()) !== false) {
// Fork process to execute task
$pid = pcntl_fork();
if ($pid == -1) {
exit(1);
} elseif ($pid) {
$this->_pids[$pid] = time();
} else // We are child so lets do it!
{
if (posix_setsid() == -1) {
exit(1);
}
// Child - Execute task
exit(0);
}
}
// Now lets pause.
$this->iterate();
}
Added links to full code preview
Full daemon code can be previewed here: http://pasted.co/ffa1d125
Code which manages (starts / stops) daemon: http://pasted.co/e3b575b0
Method iterate
consists of mainly usleep(5000000)
function. I ran daemon for 30 seconds, so I should have received max 6 iterations. Instead of that I received over 600.
Why it ignores the sleep fuction? Is it due to CLI execution? How to force daemon to use sleep to not kill my CPU?