PHP Start-up and termination

Source: Internet
Author: User

1.2 PHP Start-up and termination

The start of a PHP program can be seen as two conceptual start-up and termination with two conceptual terminations. One is PHP as Apache (take it for example, brick do not throw) the start and termination of a module, this time the launch of PHP will initialize some necessary data, such as the host Apache related, and this data is resident memory! Terminated with the opposite. Another concept is that when Apache assigns a page request, PHP has a start-and-stop, which is one of the most frequently discussed.

Now let's take a look at how a php extension's life journey is going through these four processes.

At the initial initialization time, when PHP was born in memory as Apache started, it would put all of its loaded minit methods (full-name module initialization, which are the functions defined by each module itself. ) are executed once. At this time, the extension can define some of its own constants, classes, resources, and so on all the user-side PHP script to use. But you have to remember, the things defined here will be with Apache resident memory, can be used by all requests, until Apache unloaded PHP module!

The Php_minit_function macro function is preset in the kernel to help us implement this function:

?
12345678 //abandon the author that example, the book only look at the two pages of such an example is too complicated! //walu is the name of my extension int time_of_minit; //is initialized in Minit () and output in each page request to see if it changes php_minit_function (walu) {      time _of_minit= time (NULL); //we initialize him in Minit boot      return success; //return success is normal, return faliure will not load this extension. }

When a page request arrives, PHP will quickly open up a new environment and rescan its own extensions, traversing through their respective rinit methods (called request initialization), when an extension may initialize the variables used in this request, etc. , it initializes the variables in the later client (i.e. PHP script), and the kernel presets the php_rinit_function () macro function to help us achieve this:

?
123456 inttime_of_rinit;//在RINIT里初始化,看看每次页面请求的时候变不。PHP_RINIT_FUNCTION(walu){    time_of_rinit=time(NULL);    returnSUCCESS;}

OK, now this page request execution almost, may be smooth to the end of their own files, it may be apprenticeship, half-way by the user to die or exit, this time PHP will start the recycling process, clean up the mess left by the request. This time it will execute all the Rshutdown (commonly called request Shutdown) methods of the loaded extension, when the extension can take advantage of the variable tables in the kernel to do something, because once PHP has done all the extended Rshutdown methods, Will release everything that this request used, including all variables in the variable table, all the memory requested in this request, and so on.

The kernel presets the Php_rshutdown_function macro function to help us implement this function

?
1234567 PHP_RSHUTDOWN_FUNCTION(walu){    FILE *fp=fopen("time_rshutdown.txt","a+");    fprintf(fp,"%ld\n",time(NULL));//让我们看看是不是每次请求结束都会在这个文件里追加数据    fclose(fp);    return SUCCESS;}

The beginning of the start also started, the end of the end, and now the Apache old man rest, when the Apache notice PHP itself to stop, PHP will enter the Mshutdown (commonly known as module Shutdown) stage. At this time PHP will give all the extension under the ultimatum, if which extension has unfinished wish, put in their own Mshutdown method, this is the last chance, once PHP has extended mshutdown execution, it will enter the self-destruct program, Here must be the unauthorized application of the memory to be released, or the cup has.

The Php_mshutdown_function macro function is preset in the kernel to help us implement this function:

?
123456 php_mshutdown_function (walu) {      file *fp= fopen ( "Time_mshutdown.txt" "A +"      fprintf (FP, time      return success; }

These four macros are all done in walu.c, and they are defined in the/main/php.h (and actually the other macros that are called, and at the end of this section I have expanded these macros to be viewed by those in need).

Well, now that we're done with this section, let's put all the code together and predict what should happen:

?
1234567891011121314151617181920212223242526272829303132333435363738 //这些代码都在walu.c里面,不再.h里int time_of_minit;//在MINIT中初始化,在每次页面请求中输出,看看是否变化PHP_MINIT_FUNCTION(walu){    time_of_minit=time(NULL);//我们在MINIT启动中对他初始化    return SUCCESS;}int time_of_rinit;//在RINIT里初始化,看看每次页面请求的时候变不。PHP_RINIT_FUNCTION(walu){    time_of_rinit=time(NULL);    return SUCCESS;} PHP_RSHUTDOWN_FUNCTION(walu){    FILE *fp=fopen("/cnan/www/erzha/time_rshutdown.txt","a+");//请确保文件可写,否则apache会莫名崩溃    fprintf(fp,"%d\n",time(NULL));//让我们看看是不是每次请求结束都会在这个文件里追加数据    fclose(fp);    return SUCCESS;}PHP_MSHUTDOWN_FUNCTION(walu){    FILE *fp=fopen("/cnan/www/erzha/time_mshutdown.txt","a+");//请确保文件可写,否则apache会莫名崩溃    fprintf(fp,"%d\n",time(NULL));    return SUCCESS;}//我们在页面里输出time_of_minit和time_of_rinit的值PHP_FUNCTION(walu_test){    php_printf("%d<br />",time_of_minit);    php_printf("%d<br />",time_of_rinit);    return;}
    • The value of Time_of_minit is not changed for each request.
    • The value of time_of_rinit changes every time the request is changed.
    • Each page request writes data to the Time_rshutdown.txt.
    • Data is only written to Time_mshutdown.txt after Apache has finished.

Thanks to Zhabei Lu Xiaohong for Time_of_rinit's clerical errors.

The above is the typical start-stop model in PHP, the actual situation may vary depending on the pattern, in the end, the start-end of PHP how many different ways to change, see the next section.

PHP Start-up and termination

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.