This article mainly introduces the implementation of the PHP daemon process and principles of the process, as well as in the C Environment and PHP implementation of the code, like a friend collection.
What is a daemon process?
A daemon usually complements a background task that does not control the terminal. It has three distinct features: running in the background, disconnected from the startup process, and without the need to control the terminal. The common implementation is fork (), Setsid (), fork ()
There is a function daemon in glibc. Call this function, you can make the current process out of the terminal into a daemon, the concrete content see man daemon. PHP does not have this function temporarily, PHP program implementation Daemon 2 ways:
1. Using System Commands Nohup
Nohup php myprog.php > Log.txt The execution of the program is also run in the background, but it is actually dependent on the terminal, and the process is killed when the user exits the terminal. Need to use Nohup to implement
2. Using the Supervisor tool (recommended for this scenario)
Detailed Supervisor Use tutorial
3. Of course, you can also use the program implementation (not recommended for production environment) C Program implementation:
#include #include#include#include#include#include//implementation daemon step void Crete_daemon (void) {pid_t pid = 0;pid = Fork (); if (PID <0) {perror ("fork"); exit (-1);} if (PID > 0) {//1. Parent process exits exit (0) directly;} 2.//execution to this is the child process//setsid the current process is set to a new sessions session, the purpose is to//let the current process out of the console, become the daemon process. PID = Setsid (), if (PID < 0) {perror ("Setsid"); exit (-1);} 3. Set the working directory of the current process as the root directory, independent of other chdir ("/"),//4.umask set to 0 to ensure that future processes have the maximum file operation permissions umask (0);//5. Close file Descriptor// To get the maximum number of file descriptors allowed to open in the current system, int i = 0;int cnt = sysconf (_sc_open_max); for (i=0;i
Test results:
Daemon process:
The key two PHP functions here are pcntl_fork () and Posix_setsid ()
Fork () is a process that creates a copy of the running process, which is considered a child process, and the original process is considered a parent process. When fork () runs, it can be detached from initiating his process and terminal control, and also means that the parent process is free to exit. Setsid (), which first makes the new process the "leader" of a session, and finally makes the process no longer control the terminal, which is the most critical step in becoming a daemon, which means that the process will not be forced to quit as the terminal shuts down. This is a critical step for a permanent process that will not be interrupted. The last fork (), this step is not necessary, but usually do so, its greatest significance is to prevent access to the control terminal. (The control terminal is obtained when a terminal device is opened directly without the use of the O_NOCTTY flag)
Other Notes:
The ChDir () daemon inherits the current working directory of the parent process by default, which will cause a lot of trouble when the system disk Umount, usually "/" as the current working directory of the daemon, can avoid the above problems Umask () daemon inherits the file permission mask of the parent process by default. This creates a lot of trouble for the child process to use the file. Therefore, setting the file permission mask to 0 can greatly enhance the daemon's flexibility fclose (STDIN), fclose (STDOUT), fclose (STDERR) Turn off standard I/O streams. A new child process with the fork function inherits some files that have already been opened from the parent process. These open files may never be read and written by the daemon, but they consume system resources as well, and may cause the file system in which they reside to fail to be unloaded.
Related recommendations:
Two ways to Daemon in PHP
PHP Multi-Process Implementation programming example
PHP for multi-process and multi-threading