This article mainly introduces the method of PHP using processes as daemons. It analyzes the implementation techniques of daemons in PHP with examples. Be sure to refer to the reference value. Friends who need it can refer to it
The examples in this article describe how PHP uses processes as daemons. Share it with everyone for your reference. The specific analysis is as follows:
Usage of posix_setsid() in php
The documentation explanation is “Make the current process a session leader”
Reference documentation: http://linux.die.net/man/2/setsid
It means that the process that calls this function between a process group (parent process and child process) will be elected as the leader of the process group
So the way to make a process a daemon is:
1 fork out a child process
2 In the child process posix_setsid()
3 Exit the parent process
There is an example in the documentation:
?
3 4 513 14
15
|
<🎜>$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);<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>?> |