php introduce life cycle

PHP modes of operation:

PHP WEB mode are two modes of operation, CLI mode. In either mode, PHP works are the same, run as a SAPI.

1, when we knock php command in a terminal, it uses the CLI.

It's like as a web server to support php complete the request, then the request is complete re-transfers control to the terminal.

2, when not using the Apache web server or as a host, when a request comes in, PHP support will complete the request. General are:

  • Multi-process (usually compiled as apache module to handle PHP requests)
  • Multi-threaded mode

Summary of the implementation process:

1, PHP with Apache is to start and run;

2, PHP, and is connected through the Apache module mod_php5.so (specifically a SAPI, i.e., the server application programming interface);

3, PHP total of three modules: the kernel, the Zend engine, and a spreading layer;

4, PHP kernel used to process the request, the stream file, error processing and other related operations;

5, Zend Engine (ZE) to convert the source file into a machine language, and then run on a virtual machine;

6, the enhancement layer is a set of functions, and libraries flow, PHP use them to perform certain operations. For example, we need to connect to the MySQL database MySQL extension;

7, when ZE execution of the program may need to connect several extensions, ZE then transfers control to expand, such as processed after the return of a specific task;

8, finally, ZE returns the result to the program running PHP core, which then transmits the result to the SAPI layer, the final output to the browser.

PHP is run in several stages:

SAPI are running PHP through the following stages:

1, the initialization phase module (Module init):

I.e. each extension calls in the source code of the Method PHP_MINIT_FUNCTION initialization module, some variables required application modules, memory allocation.

2, the initialization phase requests (Request init):

Each method call that is received after the extension of the client's request PHP_RINIT_FUNCTION in the execution environment initialization PHP script.

3, PHP script execution

4, the end of the request (Request Shutdown):

This time calling each extension request PHP_RSHUTDOWN_FUNCTION way to clean the site and begin recycling ZE variables and memory.

5, close the module (Module shutdown):

Exit Web server or command-line script completes exit calls PHP_MSHUTDOWN_FUNCTION way to extend the source code

SAPI life cycle in each case

1. Single SAPI process life cycle. (CGI)

PHP CLI / CGI mode SAPI belong to single-process mode. Such a request processing request after closing. That request will be repeated each time these links: Start - start request - Request closed - end. We can see an extended initialization takes a lot of resources.

2. SAPI multi-process life cycle. (MPM's perfork way)

PHP is typically compiled to a module to handle the apache request PHP (mod_php). Apache will generally multi-process mode, the start will fork out more Apache child processes (apache where you can specify the number of start fork out process). Memory space per process independence, each child will just fock out after the start and end links.

3. Multi-threaded SAPI life cycle. (FastCGI mode or single process work of MPM's)

Multi-threaded and multi-process model in a process similar to that in this mode, only one server process running, but it will run many threads simultaneously, thus reducing the cost of some resources, and to Module init Module shutdown will only need run again on the line, some global variables initialized only once, because the thread unique characteristics that facilitate the sharing of data among a number of individual requests possible.

Guess you like

Origin www.cnblogs.com/mengxiangdetiankonghenda/p/12591774.html