Home > Article > Backend Development > Detailed explanation of examples of writing daemon process in PHP
PHP writing daemon process
I saw a question on segmentfault.com this afternoon. The title of the question was "How to do PHP as a service", which asked whether PHP can only be used as a service. Called via web. In fact, many people have misunderstandings about the usage scenarios of PHP, thinking that PHP can only be used to write web scripts. In fact, starting from PHP4, the usage scenarios of PHP are no longer limited to processing web requests. From the perspective of PHP's architecture system, PHP is divided into three levels: sapi, php core and zend engine. PHP core itself has no coupling with the web. PHP communicates with other applications through sapi. For example, mod_php is a sapi implementation written for apache. Similarly, fpm is a sapi implementation based on the fastcgi protocol. These sapis are used in conjunction with the web server. Handles web requests. But there are also many sapis that have nothing to do with the web. For example, cli sapi can directly execute php in the command line environment, and embed sapi can embed php into other languages (such as Lua). I do not intend to discuss the topic of PHP's architecture system and SAPI in detail here. I just want to explain that from the perspective of architecture system, PHP has already been designed to support various environments and is not unique to the web. In addition to the support of the architecture system, PHP's rich extension modules also provide support for PHP to function in different environments. For example, the pcntl module and POSIX module mentioned in this article can realize basic process management, signal processing and other operating system-level functions. function, and the sockets module enables PHP to have socket communication capabilities. Therefore, PHP can be used to write tool scripts similar to those commonly used by shell or perl, or even a daemon process with a server nature. In order to show how to write a daemon server in PHP, I wrote a simple http server in PHP. This server runs as a daemon process. Of course, in order to focus on how to use PHP to write daemons, I did not implement specific business logic for this http server, but it can listen to the specified port, accept http requests and return a fixed text to the client. The whole process is implemented through sockets. All written in php.
Code Example
The following is the complete code of this program:
<?php //Accpet the http client request and generate response content. //As a demo, this function just send "PHP HTTP Server" to client. function handle_http_request($address, $port) { $max_backlog = 16; $res_content = "HTTP/1.1 200 OK Content-Length: 15 Content-Type: text/plain; charset=UTF-8 PHP HTTP Server"; $res_len = strlen($res_content); //Create, bind and listen to socket if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE) { echo "Create socket failed!\n"; exit; } if((socket_bind($socket, $address, $port)) === FALSE) { echo "Bind socket failed!\n"; exit; } if((socket_listen($socket, $max_backlog)) === FALSE) { echo "Listen to socket failed!\n"; exit; } //Loop while(TRUE) { if(($accept_socket = socket_accept($socket)) === FALSE) { continue; } else { socket_write($accept_socket, $res_content, $res_len); socket_close($accept_socket); } } } //Run as daemon process. function run() { if(($pid1 = pcntl_fork()) === 0) //First child process { posix_setsid(); //Set first child process as the session leader. if(($pid2 = pcntl_fork()) === 0) //Second child process, which run as daemon. { //Replaced with your own domain or address. handle_http_request('www.codinglabs.org', 9999); } else { //First child process exit; exit; } } else { //Wait for first child process exit; pcntl_wait($status); } } //Entry point. run(); ?>
Here I assume Everyone is familiar with Unix environment programming, so I won’t explain it in too many details, just sort it out. To put it simply, this program mainly consists of two parts. The handle_http_request function is responsible for processing http requests. Its writing method is similar to the tcp server written in C: create socket, bind, listen, and then process each connect through a loop. Client, once a connection is accepted...
The above is the detailed explanation of the example of writing daemon process in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !