I have a php script that displays a web form to the user. When the user submits the form, i save the data in the database and then i fork a process, the son, to send some SMS to the database users afected with the new changes.
When i fork, i check for the son, and he sends the SMS correctly, and in the end exits. But by some reason, the father waits for the son to do his tasks. I dunno why this is happening..
Here is a sample of my code:
// before this point, i've inserted some data in the database, and now i commit the transaction
$conn->commit();
$pid = pcntl_fork();
if ($pid == 0) { // its the son, so he will send the messages
$conn = new PDO('oci:dbname='.SERVER.';charset='.CHARSET, DATABASE, PASSWORD);
$suppliersPhoneNumber = getSuppliersPhoneNumber($conn, ...);
$conn = null;
$sms = new MessageSender($suppliersPhoneNumber, $_POST['subCategory']);
$sms->handleMessages(); // send the sms
//sleep(60);
exit(0); // the son won't execute more code
}/
The line with the code "sleep(60)" is how i know that the father is waiting for the child. But how is this possible if the son exits?? I know the father waits for the son, cause in fact my script freezes for 1 minute, the waiting time.
My idea is to have a father inserting the required data in the database, in the end he spawns a new child to send the messages, but doesn't waits for him, so we can send a response page to the user saying everything went fine, while the messages are effectively being sent.
What is going wrong here?
Thks in advance
EDIT The problem was not solve, instead i followed the solution of Paulo H. registed below. Indeed it was a better way.