What phpfpm is the principle?

php-fpm is a master (primary) / worker (child) multi-process architecture, design style is somewhat similar with nginx. master process is responsible for CGI and PHP environment initialization, event listeners, the child process status, etc., worker process is responsible for handling the request php. 

  

Operating principle

php-fpm using master / worker architecture, functionality described briefly in front of the master and worker process modules. The following will explain in detail the principles of operation of these two modules.

master process

master process workflow is divided into four stages, as follows: 

 

1, cgi initialization phase: each call fcgi_init () and sapi_startup () function, the registration process and the initialization signal sapi_globals global variables.

2, php environment initialization phase: triggered by cgi_sapi_module.startup. The actual call php_cgi_startup function, while internal php_cgi_startup turn calls php_module_startup execution.

php_module_startup main functions:

a), loading and parsing php configuration;

b), and recorded in the module loading php function symbol table (function_table);

c), loading zend extension;

d), arranged to disable the function library and configuration;

e), register memory recovery method;

3, php-fpm initialization phase: performing fpm_init () function. Responsible for parsing the php-fpm.conf configuration file to obtain the relevant process parameters (process allowed maximum number of open files, etc.), the initialization process pool and event model and so on.

4, php-fpm operational phase: the implementation of fpm_run () function, running processes occur emperor blocked. The stage is divided into two parts: fork child process and recurring events.

fork child process section referred fpm_children_create_initial function processing (Note: ondemand model created in fpm_pctl_on_socket_accept function).

Cycle event processing section by fpm_event_loop function, its interior is a cycle of death, responsible for the collection of events.

worker process

worker process is divided into receiving client requests, handling requests, request the end of three stages.

  

1, the receiving client requests: performing fcgi_accept_request function, its internal acquisition accept client requests by invoking the function. 

 

From the above code, you can request a lock before the operation noticed accept, such a request is designed to avoid the phenomenon of a "thundering herd" of. Of course, this is an alternative option to cancel the function.

2, the request processing stages: first, each call fpm_request_info, php_request_startup content acquisition request and registration of global variables (the GET, _GET,
G the ET, _POST, SERVER, _SERVER, S erver, _ENV, the FILES $ _); upon request and call information php_fopen_primary_script access script file; and finally to php_execute_script execution. Internal php_execute_script call zend_execute_scripts method script to the zend engine.




3, the end of the request phase: the implementation of php_request_shutdown function. At this time, and the callback function registered register_shutdown_function __destruct () method, the content transmitted in response, release the memory and other operations.

to sum up

php-fpm using master / worker architecture, master process is responsible for CGI, PHP public environment initialization and event monitor operations. worker process is responsible for processing the request. One of the reasons when the worker process to handle requests, without having to re-initialize the PHP runtime environment, which is an excellent performance php-fpm

 

Guess you like

Origin www.cnblogs.com/IT-SUJIU/p/11688012.html