I am working on a shell script in cakephp that processes a queue of items in my mysql database. To speed up the process I am using pcntlfork like so:
$pids = array();
$i = 0;
for($i = 0; $i < count($queue); $i++)
{
$pids[$i] = pcntl_fork();
if(!$pids[$i]) {
# do code
exit();
}
}
While this code is executing the shell script may run before the current script has time to delete the items from the queue. I was using mysql and locking the table like so:
$this->Queue->query("SELECT GET_LOCK('".$this->mysqlLock."', ".$this->mysqlLockTime.") AS 'GetLock'");
This implementation does not work giving me the error “General error: mysql server has gone away”. This is because the connection is lost in the children. It appears to be a flaw within fork itself in php.
My question is there a better solution to locking this processes until it finishes and then releasing it for the other shell scripts to execute?
I have found the best solution to assure that the program does not call and execute a instance of itself. I will be using semaphores in php to lock the program.
Here is a short tutorial on semaphores:
http://www.re-cycledair.com/php-dark-arts-semaphores