Home  >  Article  >  Backend Development  >  How does phpfpm work?

How does phpfpm work?

青灯夜游
青灯夜游Original
2019-10-16 13:54:143567browse

php-fpm is a master (main)/worker (child) multi-process architecture, which is somewhat similar to the nginx design style. The master process is mainly responsible for CGI and PHP environment initialization, event monitoring, sub-process status, etc., and the worker process is responsible for processing PHP requests.

How does phpfpm work?

Operation principle

php-fpm adopts master/worker architecture design, the front is simple Describe the functions of the master and worker process modules. The operating principles of these two modules will be explained in detail below.

master process

The master process workflow is divided into 4 stages, as shown below:

How does phpfpm work?

1. cgi initialization phase: call the fcgi_init() and sapi_startup() functions respectively, register process signals and initialize the sapi_globals global variable.

2. PHP environment initialization phase: triggered by cgi_sapi_module.startup. The php_cgi_startup function is actually called, and php_cgi_startup internally calls php_module_startup for execution.

Main functions of php_module_startup:

a), load and parse php configuration;

b), load php module and record it in the function symbol table (function_table);

c), load zend extension;

d), set disabled functions and class library configuration;

e), register memory recovery method;

3, php -fpm initialization phase: execute the fpm_init() function. Responsible for parsing the php-fpm.conf file configuration, obtaining process-related parameters (the maximum number of files allowed to be opened by the process, etc.), initializing the process pool and event model and other operations.

4. php-fpm running phase: execute the fpm_run() function, and the main process will be blocked after running. This phase is divided into two parts: fork child process and loop event.

The fork child process part is handled by the fpm_children_create_initial function (Note: ondemand mode is created in the fpm_pctl_on_socket_accept function).

The loop event part is processed through the fpm_event_loop function, which is an infinite loop inside and is responsible for the collection of events.

Worker process

The worker process is divided into three stages: receiving client requests, processing requests, and completing requests.

How does phpfpm work?

#1. Receive client request: Execute the fcgi_accept_request function, which internally obtains the client request by calling the accept function.

//请求锁
FCGI_LOCK(req->listen_socket);
req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
//释放锁
FCGI_UNLOCK(req->listen_socket);

From the above code, you can notice that there is a request lock operation before accept. This design is to avoid the "shock" phenomenon in the request. Of course, this is an optional option and the feature can be disabled.

2. Request processing stage: First, call fpm_request_info and php_request_startup respectively to obtain the request content and register global variables ($_GET, $_POST, $_SERVER, $_ENV, $_FILES); then call php_fopen_primary_script to access according to the request information Script file; finally handed over to php_execute_script for execution. php_execute_script internally calls the zend_execute_scripts method to hand the script over to the zend engine for processing.

3. Request end stage: execute the php_request_shutdown function. At this time, call back the registered function and __destruct() method of register_shutdown_function, send response content, release memory and other operations.

Summary

php-fpm adopts the master/worker architecture design. The master process is responsible for the initialization and event monitoring operations of CGI and PHP public environments. The worker process is responsible for request processing functions. When the worker process processes the request, there is no need to initialize the PHP running environment again, which is one of the reasons for the excellent performance of php-fpm.

For more PHP related knowledge, please visit php中文网!

The above is the detailed content of How does phpfpm work?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn