Reputation: 13
I saw at, http://php.net/manual/en/function.posix-setsid.php pcntl_fork() example at bottom. That code is working fine. can i combine it with php thread? http://www.php.net/manual/en/class.thread.php
<?php
$pid = pcntl_fork(); // fork
if ($pid < 0)
exit;
else if ($pid) // parent
exit;
else { // child
$sid = posix_setsid();
if ($sid < 0)
exit;
for($i = 0; $i <= 60; $i++) { // do something for 5 minutes
sleep(5);
}
}
?>
Upvotes: 0
Views: 1502
Reputation: 1275
I'm not sure what you want to achieve, but if you want to mix threading and forks randomly, you better read this before doing it: Threads and fork(): think twice before mixing them If you just want to detach from the controlling terminal and then start threads, (but not processes) that's most probably going to be fine. (Just replace the for
part with the threading code.)
At any rate, it should be noted that writing PHP daemons that don't have memory leaks is extremely hard, but not impossible. Expect to spend a LOT of time on hunting down memory issues, especially if you are using complex class structures.
Also, some of PHP's extensions are not thread safe, especially if they execute some external binary. You may really want to think twice before doing this. I would also consider picking a different language.
Upvotes: 1