nginx fork子进程代码和epoll_create, epoll_ctrl函数的实现关系

  • Post author:
  • Post category:其他


main(int argc, char *const *argv)

->

ngx_master_process_cycle(cycle);

->

ngx_start_worker_processes(cycle, ccf->worker_processes,

NGX_PROCESS_RESPAWN);

->

ngx_spawn_process(cycle, ngx_worker_process_cycle,

(void *) (intptr_t) i, “worker process”, type);

->

pid = fork();

main(int argc, char *const *argv)

->

ngx_master_process_cycle(cycle);

->

ngx_start_cache_manager_processes(cycle, 0);

->

ngx_spawn_process(cycle, ngx_cache_manager_process_cycle,

&ngx_cache_manager_ctx, “cache manager process”,

respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN);

->

pid = fork();

static ngx_str_t epoll_name = ngx_string(“epoll”);

static ngx_command_t ngx_epoll_commands[]

ngx_epoll_module

->

ngx_epoll_module_ctx

->

ngx_epoll_init

->

ep = epoll_create(cycle->connection_n / 2);

static ngx_str_t event_core_name = ngx_string(“event_core”);

static ngx_command_t ngx_event_core_commands[]

ngx_event_core_module

->

ngx_event_core_module_ctx

->

ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)

->

fd = epoll_create(100);

在ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)函数中

会判断是否支持EPOLL,然后创建epoll树节点:

#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)

fd = epoll_create(100);

if (fd != -1) {
    (void) close(fd);
    module = &ngx_epoll_module;

} else if (ngx_errno != NGX_ENOSYS) {
    module = &ngx_epoll_module;
}

#endif

ngx_event_module_t ngx_epoll_module_ctx

->

ngx_epoll_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)

->

epoll_ctl(ep, op, c->fd, &ee)

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 */

#if (NGX_HAVE_EVENTFD)

ngx_epoll_notify, /* trigger a notify

/

#else

NULL, /

trigger a notify

/

#endif

ngx_epoll_process_events, /

process the events

/

ngx_epoll_init, /

init the events

/

ngx_epoll_done, /

done the events */

}

};

ngx_event_actions = ngx_epoll_module_ctx.actions;

在Ngx_event.c中,处理事件event的函数是从event_module中继承过来的:

#define ngx_process_events ngx_event_actions.process_events

#define ngx_done_events ngx_event_actions.done

#define ngx_add_event ngx_event_actions.add

#define ngx_del_event ngx_event_actions.del

#define ngx_add_conn ngx_event_actions.add_conn

#define ngx_del_conn ngx_event_actions.del_conn

#define ngx_notify ngx_event_actions.notify



版权声明:本文为katerdaisy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。