From:http://www.csdn.net/article/2014-09-26/2821885-exploring-of-the-php-2
Summary: PHP, as a simple and powerful language, can provide many web-applicable language features. Starting from the practice, after the weak type variable principle inquiry, Shuai will continue to take you to understand some common parts of the PHP kernel, this period is sapi deep understanding.
SAPI is the abbreviation for the server Application programming Interface (the application programming interface for servers). PHP provides a set of interfaces through SAPI for data interaction between the application and the PHP kernel.
In simple terms, just like the input and output of a function, we execute a section of PHP code through the Linux command line, which is essentially a Linux shell that passes through PHP's SAPI to a set of parameters that the Zend engine executes and returns to the shell, which is displayed by the shell. Similarly, through Apache invoke PHP, through the Web server to SAPI incoming data, Zend engine execution, returned to Apache, by the Apache display on the page.
Figure 1. PHP Architecture Diagram
PHP offers many forms of interface, including Apache, Apache2filter, Apache2handler, Caudium, CGI, cgi-fcgi, CLI, cli-server, continuity, embed, ISAPI , Litespeed, Milter, Nsapi, phttpd pi3web, Roxen, thttpd, Tux and Webjames. But there are only 5 forms commonly used, cli/cgi (command line), multiprocess (multiple processes), multithreaded (multithreading), fastcgi, and embedded (inline).
PHP provides a function to view the current SAPI interface type:
[PHP] view Plain copy string php_sapi_name (void) PHP run and load
Regardless of which SAPI is used, before and after PHP executes a script, it contains a series of events: Init (MINT) and shutdown (mshutdown) of module, Init (RINT) of the Request, and shutdown (Rshutdown). The first phase is the PHP module initialization phase (MINT), which allows you to initialize extended internal variables, allocate resources, and register resource processors, which are executed only once throughout the life cycle of the PHP instance.
What is a PHP module. With the above PHP frame composition, you can use the Get_loaded_extensions function in PHP to view all the compiled and loaded modules/extensions, equivalent to the php-m in CLI mode.
Take the PHP memcached extended source code as an example:
[PHP] view plain copy php_minit_function (memcached) {zend_class_entry CE; memcpy (&memcached_object_handlers,zend_get_std_object_handlers (), sizeof (zend_object_handlers)); Memcached_object_handlers.clone_obj = NULL; /* Performed some similar initialization operations/return SUCCESS; The second stage is the request initialization phase (RINT), which creates a PHP runtime environment when the module is initialized and activated, invokes all module-registered RINT functions, invokes each extended request initialization function, sets specific environment variables, allocates resources, or performs other tasks, such as auditing.
[PHP] view plain copy php_rinit_function (memcached) {/* Performs some initialization of the request/return SUCCESS; }
In the third phase, when the request processing is complete, the php_rshutdown_function is invoked for recycling, which is the request shutdown function for each extension to perform the final cleanup. The Zend engine performs the cleanup process, garbage collection, and executes unset for each variable used during the previous request. The completion of the request may be done to the script, or the call to die () or the exit () function completes
Phase IV, when the PHP lifecycle ends, php_mshutdown_function the module for recycling, which is an extended module shutdown function that shuts down its own kernel subsystem.
[PHP] view plain copy php_mshutdown_function (memcached) {/* To perform destruction work on modules/unregister_ini_entries (); return SUC CESS; }
Common mode of Operation
There are five common SAPI modes:
CLI and CGI mode (single process mode) multi-process mode multithreaded mode fastcgi mode embedded
1. cli/cgi mode
Both CLI and CGI are part of a single process mode, and the life cycle of PHP is completed in one request. That is, every time you execute a PHP script, you execute the four int and shutdown events in the second section.
Figure 2. CGI/CLI Life cycle
2. Multi-process mode (multiprocess)
Multi-process mode can be built into the Web server PHP, PHP can be compiled into Apache under the Prefork mpm mode and APXS module, when the Apache started, will fork many child processes, each child process has its own separate process address space.
Figure 3. Multi-process Mode lifecycle
In a subprocess, the life cycle of PHP is to invoke the Mint startup, execute multiple requests (Rint/rshutdown), and then invoke the Mshutdown for the recycle phase after the Apache shutdown or process ends.
Figure 4. Multi-Process Lifecycle
In a multi-process model, each subprocess is run independently, without code and data sharing, so a child process terminates the exit and regeneration without affecting the stability of other child processes.
3. Multithreading mode (multithreaded)
The Apache2 worker MPM employs a multithreaded model that creates multiple threads under one process and executes in the same process address space.
Figure 5. Multithreading Lifecycle
4. fastcgi mode
In our use of the NGINX+PHP-FPM is the fastcgi mode, fastcgi is a special CGI mode, is a resident process type of CGI, after running can fork multiple processes, do not spend time dynamic fork child processes, Mint/mshutdown is also not required to be invoked on each request. PHP manages and dispatches the fastcgi process pool through PHP-FPM. Nginx and PHP-FPM communicate through a local TCP socket and a UNIX socket.
Figure 6. fastcgi Mode life cycle
PHP-FPM the process manager itself, initiating multiple CGI interpreter processes to wait for requests from Nginx. When the client request reaches PHP-FPM, the manager selects a CGI process for processing, Nginx sends the CGI environment variable and standard input to a php-cig subprocess. When the php-cgi process completes, the standard output and error messages are returned to Nginx, and the request processing completes when the php-cgi child process closes the connection. The php-cgi child process waits for the next connection.
You can imagine how much the CGI system overhead is. Each Web request PHP must reparse php.ini, load all extensions, and start all data structures. With fastcgi, all of these occur only once when the process is started. In addition, continuous connections to the database and memcache can work.
5. Inline mode (Embedded)
Embed SAPI is a special SAPI that allows you to invoke PHP-supplied functions in the C + + language. This sapi, like the CLI mode, runs in the mode of module init => request Init => request => request Shutdown => Module Shutdown.
Embed SAPI can invoke PHP's rich library of classes, as well as advanced gameplay, such as viewing php's opcode (php-Executed middle code, Zend-engine instructions, generated by PHP code).
Detailed See: http://www.laruence.com/2008/09/23/539.html SAPI operation mechanism
Let's take a CGI example and look at the operating mechanism of SAPI.
[PHP] view plain copy