nginx源码分析--多路复用epoll的初始化流程

本文深入探讨了nginx中的事件处理机制,特别是ngx_event_core_module如何负责配置解析和选择事件处理方式,以及ngx_epoll_module如何实现epoll的具体创建和操作。核心在于ngx_event_process_init函数调用ngx_epoll_init,完成epoll多路复用器的初始化。
摘要由CSDN通过智能技术生成

NGX_EVENT_MODULE类型的module有两个:
(1)ngx_event_core_module
第一个事件类型模块,它的主要功能是负责解析事件类配置项,选择事件处理机制(select,poll,epoll还是kequeue机制),创建连接池,预分配读写事件池等。ngx_event_core_commands[] 中指定处理conf配置的关于sevents的配置项
(2)ngx_epoll_module
实现epoll的具体创建以及相关操作,在整个nginx中的reactor都是依赖于这个module。

/**
 * 工作进程初始化
 */
static void
ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
{
   
    /**
    *调用每个模块注册的init_process函数(这个操作是在每个worker进程中的,所以申请一些进程
    *自用的一些资源,如内存、变量等)
    */
    for (i = 0; cycle->modules[i]; i++) {
   
        if (cycle->modules[i]->init_process) {
   
            if (cycle->modules[i]->init_process(cycle) == NGX_ERROR) {
    //ngx_core_module->NULL
                                                                       //ngx_events_module->NULL
                                                                       //ngx_event_core_module->ngx_event_process_init()
                                                                       //ngx_epoll_module->NULL
                                                                       //ngx_http_module->NULL
                                                                       //ngx_http_core_module->NULL
                /* fatal */
                exit(2);
            }
        }
    }
}

在这个循环遍历中,只有ngx_event_core_module的init_process函数指针不为空,其对应的函数为:ngx_event_process_init

/* 该模块会决定使用哪种事件驱动机制,以及如何管理事件 */
ngx_module_t  ngx_event_core_module = {
   
    NGX_MODULE_V1,
    &ngx_event_core_module_ctx,            /* module context */
    ngx_event_core_commands,               /* module directives */
    NGX_EVENT_MODULE,                      /* module type */
    NULL,                                  /* init master */
    /* 没有 fork 出 worker 子进程时,会调用该函数 */
    ngx_event_module_init,                 /* init module */
    /* fork出子进程后,每一个worker进程会在用 ngx_event_core_module 模块的 ngx_event_process_init 方法后才会进入正式的工作循环 */
    ngx_event_process_init,                /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};
/**该函数ngx_worker_process_init中的init_process函数中被调用,因为执行阶段位于
 *worker进程初始化时,所以这里申请的资源都属于各个worker独有,不用担心因被继承而导致的资源浪费
 *在这个函数中完成了对从master进程继承的listen fd的初始化工作
 */
static ngx_int_t
ngx_event_process_init(ngx_cycle_t 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值