PHP extensions and Embedding--php life cycle and variable details _php tutorial

Source: Internet
Author: User
Tags learn php zts
Start with the life cycle of PHP, understand how a PHP program starts running to the end of the process, it is important to learn PHP and PHP development in peacetime.

start and close phases:
  • For PHP, the start and close stages can be divided into two tiers,
    • The first layer is the initialization process for the structure and values of the PHP interpreter as a whole.
    • The second layer is in the request process for each page.
    • For each extension, there will be an initialization mint function, which declares variables, classes, registered resources, streams, and filter processors, which are present in all requests, so it can be called persistent. The following two steps are generally performed:
      • Register_ini_entries ()
      • Initialize the global variables of the module
      • After the page makes a request, PHP creates an operating environment that includes the symbol table and configuration values, and then this time the PHP interpreter loops through each extension, invoking each extended rinit initialization function. The general Rint function operates as follows:
        • Set Globalvalue to default values, which are often required for each request, but are independent of each request.
        • The variables that need to be used are placed in the symbol table for invocation.
        • In this function, you can also record information about the request.
        • Once the request has been processed, if it reaches the end of the script or is rolled out through die () exit, PHP starts cleaning by calling Rshutdown ()
          • The variables in each symbol table will be unset out.
          • And when all the requests are fulfilled, the Mshutdown process for the module begins.
            • Call Unregister_ini_entryes (), which corresponds to the initialization process of the Minit function.PHP Program execution life cycle:
              To understand the life cycle, it is necessary to have a different way of doing something. PHP can be implemented in several different ways, each with its own specific life cycle.
              • CLI: This is a PHP program executed from the command line, and its life cycle is the simplest. For example, when the test.php is executed, it goes through the following process.
              • Figure 1 CLI execution procedure for PHP
              • Note that Minit Rinit Rshutdown Mshutdown have been called only once, which is similar to the waterfall-like structure.
              • Multi-threaded Module mode: This is the most common way, PHP as a APXS module for Apache to cooperate. When Apache starts, it will fork a lot of sub-processes. For many different requests, the combination is a number of different initialization and completion processes. But for each thread, there is only one minit and Mshutdown call. Each request corresponds to its own separate rinit and Rshutdown.
              • Multi-threaded Module mode: Multi-threading method can avoid different thread repeated call Minit/mshutdown. It has the advantage that multiple requests can share information, but the isolation requirement between requests is high, otherwise the access error of the variable is easy to occur.

                Zend Thread Safety
                PHP has a special mechanism for threading security, called Thread safe Resource Management (TSRM). There are obvious differences when it comes to thread-safe and non-thread-safe declarations:
                • Thread-safe variable declarations:
                • typedef struct {
                  int sampleint;
                  Char *samplestring;
                  } php_sample_globals;
                  int sample_globals_id;
                  Php_minit_function (sample)
                  {
                  ts_allocate_id(&SAMPLE_GLOBALS_ID,
                  sizeof (Php_sample_globals),
                  (Ts_allocate_ctor) Php_sample_globals_ctor,
                  (ts_allocate_dtor) php_sample_globals_dtor);
                  return SUCCESS;
                  }
                  • As you can see from this code, in the Minit phase, the TS_ALLOCATE_ID function is required to inform TSRM how much space the program needs, TSRM increases the current space consumption, and returns an ID pointing to the appropriate portion of the thread data pool.
                  • When requesting access to the data, the pointer to the resource pool of the current thread is first found from the TSRM layer, followed by the resource ID returned by TS_ALLOCATE_ID () as offset.
                  • Non-thread-safe variable declaration:
                  • typedef struct {
                    int sampleint;
                    Char *samplestring;
                    } php_sample_globals;
                    Php_sample_globals sample_globals;
                    Php_minit_function (sample)
                    {
                    Php_sample_globals_ctor (&sample_globals tsrmls_cc);
                    return SUCCESS;
                    }
                    • In non-thread-safe situations, simply declaring variables is faster and more efficient, and the address of the data can be determined at compile time, rather than requiring runtime calculations, as is the case with thread security. It also has the advantage that a bug in a program doesn't make the whole webserver bad. So when you don't need to use thread safety, you should try to use a non-thread-safe declarative approach.
                    • In order to build a thread-safe php, the--ENABLE-MAINTAINER-ZTS option must be added at compile time. In the process of detection can be used #ifdef ZTS, through this detection can achieve the global variable declaration of different processing, as shown in the following example:
                    • #ifdef ZTS
                      #define HELLO_G (v) tsrmg (hello_globals_id, zend_hello_globals *, v)
                      #else
                      #define HELLO_G ( V) (HELLO_GLOBALS.V)
                      #endif

                      • Specifies that variables are declared and accessed differently by determining whether thread safety is initiated.
                      • after thread safety is enabled, PHP automatically enables a Tsrm_ls pointer in order to differentiate data between different threads. With the help of this pointer, the function of each thread can find the corresponding symbol table for the corresponding variable read operation. It is this mechanism that allows multiple threads to work together, and the data space does not mess up. The

                        facilitates subsequent research into PHP extension compilation by understanding the starting and ending stages of PHP, the lifecycle, and the mechanism of Zend thread safety.

                        http://www.bkjia.com/phpjc/621631.html www.bkjia.com true http://www.bkjia.com/phpjc/621631.html techarticle start with the PHP lifecycle and learn how a PHP program runs from the beginning to the end, It should be important to learn PHP and PHP development in peacetime. Start ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.