Nginx Source (4) main process

Source: Internet
Author: User
Tags epoll
Connect to the above.

Nginx in a single process run up in the console, read core/nginx.c in the main method, before the initialization of the code, first not carefully read, easy to fall into the delicate ring. Directly find the definition of ngx_single_process_cycle, in os/unix/ngx_process_cycle.c.

Add the following code to the first for loop:

  for  (i =  0 ; ngx_modules[i]; i++) { char  * p = NULL;  if  (Ngx_modules[i]->commands! = NULL) {p = ngx_modules[i]->commands->name.data; }  if  (ngx_modules[i]->init_process) { if  (Ngx_modules[i]->init_process (c ycle) = = Ngx_error) {/* fatal */  exit  ( 2 );  printf  ( "[ngx_process_cycle] module ctx_index=%d index=%d name=%s init process\n" , n Gx_modules[i]->ctx_index, Ngx_modules[i]->index, p); }  else  { printf  ( "[ngx_process_cycle] module ctx_index=%d index=%d Name=%s\n ", Ngx_modules[i]->ctx_index, Ngx_modules[i]->index, p); }}  printf  ( "[ngx_process_cycle] for ngx_modules init done\n" ); 

The function is to print which modules and related indexes and names are in the current nginx.

In the second for loop, the main is the Ngx_process_events method, find this is a macro, and then this macro is a global variable, because we load the Epoll module, so in event/modules/ngx_epoll_module.c
Code found in:

ngx_event_actions = ngx_epoll_module_ctx.actions;

The discovery Ngx_process_events method is actually the Ngx_epoll_process_events method in the actions in the Ngx_epoll_module_ctx module. In this method, find the place where the Epoll_wait method is called, plus the following code:

printf("[ngx_epoll_module] epoll wait\n");    events = epoll_wait(ep, event_list, nevents, timer);    printf("[ngx_epoll_module] epoll wait ---\n");

Re-make run result:

[main] to start ngx_single_process_cycle
[Ngx_process_cycle] Module ctx_index=0 index=0 Name=daemon
[Ngx_process_cycle] Module ctx_index=0 index=1 name=error_log
[Ngx_process_cycle] Module ctx_index=0 index=2 name=include
[Ngx_process_cycle] Module ctx_index=0 index=3 name=events
[Ngx_process_cycle] Module ctx_index=0 index=4 name=connections init process
[Ngx_process_cycle] Module ctx_index=1 index=5 Name=rtsig_signo
[Ngx_process_cycle] Module ctx_index=2 index=6 name=epoll_events
[Ngx_process_cycle] Module ctx_index=0 index=7 name=http
[Ngx_process_cycle] Module ctx_index=0 index=8 name=server
[Ngx_process_cycle] Module ctx_index=1 index=9 name=log_format
[Ngx_process_cycle] Module ctx_index=2 index=10 name= (NULL)
[Ngx_process_cycle] Module ctx_index=3 index=11 name=index
[Ngx_process_cycle] Module ctx_index=4 index=12 name=allow
[Ngx_process_cycle] Module ctx_index=5 index=13 name=rewrite
[Ngx_process_cycle] Module ctx_index=6 index=14 name=proxy_pass
[Ngx_process_cycle] Module ctx_index=7 index=15 name= (NULL)
[Ngx_process_cycle] Module ctx_index=8 index=16 name= (NULL)
[Ngx_process_cycle] Module ctx_index=9 index=17 name= (NULL)
[Ngx_process_cycle] Module ctx_index=10 index=18 name= (NULL)
[Ngx_process_cycle] Module ctx_index=11 index=19 name=gzip
[Ngx_process_cycle] Module ctx_index=12 index=20 name=charset_map
[Ngx_process_cycle] Module ctx_index=13 index=21 Name=userid
[Ngx_process_cycle] Module ctx_index=14 index=22 name=expires
[Ngx_process_cycle] Module ctx_index=15 index=23 name=output_buffers
[Ngx_process_cycle] Module ctx_index=16 index=24 name= (NULL)
[Ngx_process_cycle] Module ctx_index=17 index=25 name= (NULL)
[Ngx_process_cycle] for ngx_modules init done
[Ngx_epoll_module] Epoll wait
^c[ngx_epoll_module] Epoll wait-

There are 25 modules found, only the Connections module has the Init_process method, and some modules name is empty. Finally, the program entered into the Epoll module, blocking at the epoll_wait call, CTRL + C launched after the execution of the following print statements.

Here can take a closer look at the Nginx definition of the module, see the code when it is easy to find the actual type of nginx in the _t end, and if the type is defined by the structure of the actual type is _s end, nginx_module_s is defined in the Core/ngx_ In the Conf_file.h file:

struct ngx_module_s {    ngx_uint_t       ctx_index;    ngx_uint_t       index;    void            *ctx;    ngx_command_t   *commands;    ngx_uint_t       type;    ngx_int_t      (*init_module)(ngx_cycle_t *cycle);    ngx_int_t      (*init_process)(ngx_cycle_t *cycle);#if 0    ngx_int_t      (*init_thread)(ngx_cycle_t *cycle);#endif};

There are two index numbers, as well as commands and 3 functions, and what they mean to use later.

There are two other structural bodies ngx_event_module_t and ngx_event_actions_t; The latter is a member of the former, the former is the Nginx event module, but the abstract, specific to a platform will have a different type, here is Epoll.
ngx_event_module_t as follows:

typedef struct {    ngx_str_t              *name;    void                 *(*create_conf*cycle);    char                 *(*init_conf*cycle*conf);    ngx_event_actions_t     actions;} ngx_event_module_t;

Epoll examples are as follows:

ngx_event_module_t Ngx_epoll_module_ctx = {&epoll_name, ngx_epoll_create_conf,/ * Create configuration * /Ngx_epoll_init_conf,/ * INIT configuration * /{ngx_epoll_add_event,/ * Add an event * /Ngx_epoll_del_event,/ * Delete an event * /Ngx_epoll_add_event,/ * Enable an event * /Ngx_epoll_del_event,/ * Disable an event * /Ngx_epoll_add_connection,/ * Add an connection * /Ngx_epoll_del_connection,/ * Delete an connection * /NULL,/ * Process the changes * /Ngx_epoll_process_events,/ * Process the events * /Ngx_epoll_init,/ * Init the events * *Ngx_epoll_done,/* Done the events * /}};

Just as the actions members in ngx_event_module_t are defined in NGX_EPOLL_MODULE_CTX as Epoll related functions, the preceding ngx_process_events is concatenated by the following two lines of code:
In Event/ngx_event.h

#define ngx_process_events   ngx_event_actions.process_events

In event/modules/ngx_epoll_module.c

   ngx_event_actions = ngx_epoll_module_ctx.actions;

Ngx_event_actions is defined as a global variable in event/ngx_event.c:

ngx_event_actions_t               ngx_event_actions;

Next look at the use of Epoll in Nginx.

To be Continued ...

The above describes the Nginx source (4) main process, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.