PHP daemon process instance details, daemonprocess
Write daemon process in PHP
This afternoon, segmentfault.com asked a question titled "How To Make PHP service", asking if php can only be called on the web. In fact, many people have misunderstandings about the use of PHP, and think that php can only be used to write web scripts. In fact, from PHP4, the use of php is no longer limited to processing web requests. From the perspective of php architecture, php is divided into three layers: sapi, php core, and zend engine. Php core is not coupled with the web. php communicates with other applications through sapi. For example, mod_php is the sapi for apache. Similarly, fpm is an sapi implementation based on fastcgi protocol, these sapis work with web servers to process web requests. However, many sapis 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 am not going to discuss in detail the php architecture and sapi topics here. It just shows that from the perspective of the architecture system, php has already been designed to support various environments, rather than being unique to the web. In addition to the support of the architecture system, the rich extension modules of php also provide the backend for php to play a role in different environments, for example, the pcntl module mentioned in this Article can work with the posix module to implement basic process management, signal processing, and other operating system-level functions. The sockets module can make php capable of socket communication. Therefore, php can be used to write tool scripts similar to shell or perl, or even daemon process of server nature. To demonstrate how to compile the daemon server in php, I wrote a simple http server in php, which runs in the form of daemon process. Of course, in order to focus on how to use php to write daemon, I did not implement specific business logic for this http server, but it can listen to the specified port, accept the http request and return a fixed text to the client. The entire process is implemented through socket, all written in php.
Code instance
The complete code of this program is as follows:
<?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 OKContent-Length: 15Content-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 suppose you know more about programming in the Unix environment, so I will not explain it too much, just sort it out. To put it simply, this program is mainly composed of two parts. The handle_http_request function is responsible for processing http requests. The compiling method is similar to that of the tcp server written in C: Creating socket, binding, and listening, then, each connect client is processed in a loop. Once the accept is connected to a connection...
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!