I am new on PHP forking and I just copy this code sample from PHP.net. Basically I have a tool that monitor a device and save the data into a database. This tool is running in background.
here is my code:
for(;;)
{
//build connection
$conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
//select db
mysql_select_db(DB_NAME);
$sql='SELECT * FROM tbl';
$query=mysql_query($sql,$conn);
$zpid=array();
while($res=mysql_fetch_assoc($query))
{
$pid = pcntl_fork();
$execute=0;
if ($pid == -1)
{
echo("could not fork");
}
elseif ($pid)
{
$execute++;
//pcntl_wait($status);
if ($execute>=5)
{
$child=pcntl_wait($status);
if($child)
{
$execute--;
echo 'exited child'.$child;
}
}
}
else
{
$obj=new Monitor($res['ip'],$res['community_string'],$res['id']);
$obj->execute();
//save
$obj->insert_data();
sleep(10);
exit();
}
}
mysql_close($conn);
sleep(60);
}
But when the tool run it will produce a zombie process. What will I do to prevent this zombie or defunct process on our server. Please help me. thanks in advance