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