SAPI single process life cycle

PHP CLI / CGI mode SAPI belong to single-process mode. Such a request processing request after closing.
That is only through the following links: Start - start request - Request closed - end SAPI interface to complete its life cycle. Shown in Figure 2.1:
1.php_module_startup () // start
2.php_request_startup () // activate
3.php_execute_script () // run
4.php_request_shutdown () // inactivated
5.php_module_shutdown () // End
1. Start
1.zend_startup // initialization Zend engine and core components
2.zend_startup_modules // PHP_MINIT_FUNCTION

2. activate
1.zend_activate // activate Zend engine
2.sapi_activate // activate the SAPI
3.zend_activate_modules // initialization module requests, PHP_RINIT_FUNCTION

3. run
1. // prepend_file_p zend_execute_scripts, primary_file, append_file_p
2.zend_compile_file // lexical analysis, parsing and code generation operation
3.zend_execute // if valid parsed file has generated intermediate code, call zend_execute (execute function) intermediate code execution

4. inactivation
1.zend_deactivate_modules // request PHP_RSHUTDOWN_FUNCTION, execution modules closing operation
2.zend_deactivate // zend_deactivate function by closing lexical analyzer, parser and the intermediate code execution unit.
3.zend_post_deactivate_modules // invoke the extension of post-RSHUTDOWN each function. Each extended post_deactivate_func just basic function pointer is NULL
4.sapi_deactivate // Close SAPI, destruction by sapi_deactivate SG (sapi_headers), SG (request_info ) content and the like.

5. End
1.sapi_flush // sapi_flush refresh the contents of the final out. Which call is sapi_module.flush, the CLI is equivalent to the function fflush
2.zend_shutdown // Close Zend engine
 

 

FIG as above is very simple, it is also well understood. But also between the various stages of PHP a lot of work to do. Here to do some added:

 

1. Start (php_module_startup)
before calling each module initialization module, there will be a process of initializing, it includes:

  

Initialization for global variables
here initialized global variables in most cases it is set to NULL, except for some,
such as setting zuf (zend_utility_functions), an example to zuf.printf_function = php_printf,
php_printf here will be assigned the function zend_startup zend_printf used as a global function pointer, the function will typically use a conventional zend_printf output as a string,
such as the display program call stack debug_print_backtrace it is to use it to print information.

 


Zend engine and a core component initialization

 


Parsing the php.ini
1. determine whether php_ini_path_override, this path can be specified by the CLI -c parameters (parameters in the command php -c A lookup ini files in the specified path).
2. If there is no php_ini_path_override, determines whether php_ini_ignore nonempty (ignoring php.ini configuration, it is useful where the CLI mode, -n parameter).
3. If you do not ignore the ini configuration, then start processing php_ini_search_path (Find ini file path), which path includes CWD (current path, but this does not apply CLI mode), execute scripts directory, and PATH environment variables and configuration files PHPRC the value of PHP_CONFIG_FILE_PATH.
4. After preparing search path, PHP will judge the current ini path (php_ini_file_name) whether the file and whether to open.
If there is a path to ini file and can be opened, will use this file, which is a priority CLI mode specified by the -c parameter ini file is the highest,
followed by PHPRC specified file, and the third is in the search path Find php-% sapi-module-name% .ini files (such as CLI mode should find php-cli.ini file),
and finally the search path to find the php.ini file.

 

 

 

 

 


The global initialization operation function
php_startup_auto_globals initialization function in the user space of the high frequency of the use of global variables,
such as: $ _ GET, $ _ POST , $ _ FILES like. Here are just initialization,
zend_register_auto_global the called function is only to add these variable names to CG (auto_globals) this variable table.

php_startup_sapi_content_types SAPI handler function is used to initialize the contents of different types,
where the default handler include POST data handler, the default data processing functions and the like.

 

 


Initialize static build modules and sharing module (MINIT)
php_register_internal_extensions_func function module is used to register a static building, which is the default module loaded,
we think it can be built-in module. Built-in PHP5.3.0 version of the standard module includes PHP extension module (/ ext / standard / directory, here is our most frequently used function, such as string functions, mathematical functions, array manipulation functions, etc.), the calendar extension , FTP expansion module,
the session expansion module. These built-in module is not static, in different templates in PHP,
as demand at different times or other factors cause these changes will be loaded by default modules,
such as we can see mysql, xml and other expansion modules from the code once or as a built-in module will appear in the future.

Initialization module performs two operations: 1. These modules are registered to the list of registered modules (module_registry),
if the registration module has already been registered, PHP will report Module XXX already loaded error.
1. The register function to each module included in the function table (CG (function_table)), if the function can not be added, will be reported Unable to register functions, unable to load .

After registering module static build, PHP will register additional modules, different modes can load different set of modules, such as in CLI mode is none of these additional modules.

After the built-in module and additional modules, followed by a registered shared objects (such as DLL) file extensions and php.ini flexible configuration.

After all the modules are registered, PHP will execute module initialization operation (zend_startup_modules) immediately.
The whole process is its turn through each module, each module calls the initialization function of the module,
which is mentioned earlier in this section with a macro PHP_MINIT_FUNCTION contain.

 

 

 

 


Disable functions and classes
php_disable_functions function is used to disable some functions of PHP. These functions are disabled disable_functions variables from PHP configuration file.
The procedure is to call it off zend_disable_function function specified function name removed from the CG (function_table) function table.

php_disable_classes function is used to disable some kind of PHP. These variables are disabled disable_classes class configuration files from PHP.
The procedure is to call it off zend_disable_class function specified class name deleted from the CG (class_table) class table.


 
2.ACTIVATION (php_request_startup)
in the processing of the files related to the content, PHP will call php_request_startup do request initialization.
Initialization request, in addition to the function call initialization request for each module shown in the drawing, also other work done more, the following key elements
activated Zend engine
gc_reset function to reset the garbage collection, of course, this is PHP5 .3 only after.

init_compiler function is used to initialize the compiler, such as the compilation process in the array on the opcode empty, ready to compile the data structure when needed like.

init_executor function to initialize the intermediate code execution. During compilation, the list of functions, and so on stored in the class list of global variables in compile time,
in preparation for execution, these lists will be executed to assign the global variable, as: EG (function_table) = CG ( function_table) ;
intermediate code execution is performed in a virtual stack of PHP, initialization of these stacks together like are initialized.
In addition to the stack, as well as the symbol table storing variables (EG (symbol_table)) is initialized Hashtable elements 50, object storage EG (objects_store) 1024 is initialized elements. PHP execution environment in addition to the above variables, as well as error handling, exception handling, etc., which are here to be initialized.
Configuration by php.ini zend_extensions also here to be traversed call activate the function.
 

Activating the SAPI
sapi_activate function is used to initialize the SG (sapi_headers) and SG (request_info), and some of the content is provided for the method of the HTTP request,
such as when the request method HEAD, provided SG (request_info) .headers_only = 1; the most important function a data processing request operation,
which will eventually call sapi_module.default_post_reader.
In the foregoing and sapi_module.default_post_reader module initialization default handler is registered for the main / php_content_types.c file php_default_post_reader php_startup_sapi_content_types function by function. This function will POST the original data written to the $ HTTP_RAW_POST_DATA.

After the post processed data, PHP is read by sapi_module.read_cookies value of the cookie,
in CLI mode, this function is achieved sapi_cli_read_cookies, while the body of the function is only a return NULL;

if the current mode is provided activate the function then run this function, the SAPI activation, in the CLI function pointer is set to NULL.
Environment initialization
environment initialization here refers to some environmental variables need to use in user space initialization, where the environment includes a server environment, the request data environment. The actual variable we used to, that is, $ _POST, $ _ GET, $ _ COOKIE, $ _ SERVER, $ _ ENV, $ _ FILES.
And sapi_module.default_post_reader, as also when the value sapi_module.treat_data module is initialized
by php_startup_sapi_content_types function registered default data processing function main / php_variables.c file php_default_treat_data function.

To $ _COOKIE for example, php_default_treat_data function would be based on a delimiter, and splits all the cookie assigned to the corresponding variable.
Module initialization request
PHP module initialization request is achieved by zend_activate_modules function,
that is, we see Call each extension's RINIT in the figure. This function is registered by traversing all modules in module_registry variable,
call its RINIT implemented method of requesting initialization module.
 

3. Run (php_execute_script)
php_execute_script function includes all processes run PHP scripts.

When a file PHP requires parsing performed, it may need to perform three documents, including a pre-execution file,
the current master file and the need to perform a post-execution file. Noncurrent two files may be provided in php.ini by auto_prepend_file auto_append_file parameters and parameter.
If these two parameters are set to null, the corresponding executable file is disabled.

For documents analyzed and executed, lexical analysis done by zend_compile_file (compile_file function), the intermediate code generation and parsing operation,
to return all the file of the intermediate code. If the resolution of the intermediate file has generated a valid code is called zend_execute (execute function) intermediate code execution. If an exception occurs during execution and processing of these user defined exception, the exception handler calls these.
In all operations are dealt, PHP returns results EG (return_value_ptr_ptr).

 

4.DEACTIVATION (php_request_shutdown)
the PHP off request process is a set of a plurality of closing operation, is present in this collection php_request_shutdown function.
This set includes the following:

1. Call all functions by register_shutdown_function () registered. These functions are called when closed is in user space to add come.
As a simple example, we can call upon a unified script error function, a user-friendly to a number of pages, this is somewhat similar to the page of 404 pages.

2. Perform all available __destruct function. Herein includes a destructor object pool (EG (objects_store)
destructor method for each element in the destructor all the objects and EG (symbol_table) in.

3. All outputs brush out.

4. transmits an HTTP response header it is also a process output string, the string may only comply with certain specifications.

5. the method of traversing each module off request, a request to perform the closing operation of the module,
which is what we see in FIG Call each extension's RSHUTDOWN.

6. the destruction of variables that are global table (PG (http_globals)) is.

7. by zend_deactivate function, close lexical analyzer, parser and the intermediate code execution unit.

8. the extended post-RSHUTDOWN call each function. just each post_deactivate_func substantially extended function pointer is NULL.

9. the closed SAPI, destruction by sapi_deactivate SG (sapi_headers), SG (request_info ) content and the like.

10. A closed wrapper stream flows off the filter.

11. Close memory management.

12. re-set the maximum execution time
 

5. End (php_module_shutdown)
flush
sapi_flush refresh the contents of the final out. Which call is sapi_module.flush, in CLI mode is equivalent to fflush function.

Close Zend engine
zend_shutdown Zend engine will shut down.

At this time, the flow map, we should be performed for each module close the module operation.
Here is only a zend_hash_graceful_reverse_destroy function module_registry destroyed.
Of course, it will eventually also call a method to close the module, which is rooted in the initialization module_registry
is set up to call the destructor ZEND_MODULE_DTOR hash table macro. And ZEND_MODULE_DTOR corresponds module_destructor macro function.
In this function calls the module module_shutdown_func method, namely the macro function PHP_RSHUTDOWN_FUNCTION generated.

After closing all the modules, PHP continues to destroy global function table, the destruction of the global class table, table sales and other global variables.
By traversing zend_extensions zend_shutdown_extensions all the elements, the extended shutdown function for each call.

 
---------------------
Author: enlyhua
Source: CSDN
Original: https: //blog.csdn.net/enlyhua/article/details/81905154
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/shixiuxian/p/11230955.html