Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write daemon process by PHP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces PHP how to write daemon process, the article is very detailed, has a certain reference value, interested friends must read it!

I saw a question on segmentfault.com with the title "how does PHP service", asking if php can only be called as web. In fact, many people have a misunderstanding about the usage scenario of PHP, thinking that php can only be used to write web scripts. In fact, since PHP4, the usage scenario of php has not been limited to processing web requests. From the perspective of php architecture, php is divided into three levels: sapi, php core and zend engine. Php core itself does not have any coupling with web, and php communicates with other applications through sapi. For example, mod_php is a sapi implementation written for apache. Similarly, fpm is an sapi implementation based on the fastcgi protocol, and these sapi are used in conjunction with web server to process web requests. But there are many sapi that are independent of web, such as cli sapi, which enables direct execution of php,embed sapi in a command-line environment to embed php in other languages (such as Lua). I'm not going to discuss the architecture of php and the topic of sapi in detail here, just that from an architectural point of view, the current php has long been designed to support a variety of environments, not unique to web. In addition to the support of the architecture, php's rich expansion modules also provide backing for php to play a role in different environments. For example, the cooperation of pcntl module and posix module mentioned in this paper can achieve basic process management, signal processing and other operating system-level functions, while sockets module can enable php to have the ability of socket communication. So php can be used to write instrumental scripts similar to those commonly done by shell or perl, or even daemon process with the nature of server. To show how php writes daemon server, I wrote a simple http server in php that runs as daemon process. Of course, to focus on how to write daemon using php, I don't implement specific business logic for this http server, but it can listen on the specified port, accept http requests and return a fixed text to the client, the whole process is implemented through socket, all written by php.

Code example

Here is the complete code for this program:

Here I assume that you all know a lot about programming in the Unix environment, so I won't explain it in too much detail, just comb through it. To put it simply, this program is mainly composed of two parts. The handle_http_request function is responsible for handling http requests. It is written in a similar way to tcp server written in C: creating socket, binding, listening, and then processing the client from each connect through a loop, once accept is connected to a connection.

The above is all the content of the article "how PHP writes daemon process". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report