ngx_cycle变量的初始化

本文详细介绍了nginx中的核心变量ngx_cycle,包括其结构体定义、各字段含义及ngx_init_cycle函数的实现过程。从内存池创建到配置文件解析,再到监听套接字与共享内存的初始化。
应该可以说ngx_cycle变量应该是nginx最重要的变量之一的,在源码的很多地方都会用到这个变量,首先看该变量结构体的定义:(src/core/Ngx_cycle.h)
struct ngx_cycle_s {
    void                  ****conf_ctx;   //一些配置上下文
    ngx_pool_t               *pool;   //内存池

    ngx_log_t                *log;    //日志
    ngx_log_t                 new_log;

    ngx_connection_t        **files;   //所有在线的connection
    ngx_connection_t         *free_connections;   //当前空闲可用的connection
    ngx_uint_t                free_connection_n;   //空闲connection的数量

    ngx_queue_t               reusable_connections_queue;

    ngx_array_t               listening;   //保存所有的监听套接字
    ngx_array_t               pathes;
    ngx_list_t                open_files;   //保存所有打开的文件
    ngx_list_t                shared_memory;

    ngx_uint_t                connection_n;   //连接的个数
    ngx_uint_t                files_n;   //打开文件的个数

    ngx_connection_t         *connections;
    ngx_event_t              *read_events;    //读事件
    ngx_event_t              *write_events;   //写事件

    ngx_cycle_t              *old_cycle;    

    ngx_str_t                 conf_file;   //配置文件
    ngx_str_t                 conf_param;   //配置参数
    ngx_str_t                 conf_prefix;   //配置前缀
    ngx_str_t                 prefix;   //前缀
    ngx_str_t                 lock_file;  //锁文件
    ngx_str_t                 hostname;   //主机名字
};

至于每一个域的作用在上面已经说清楚了,然后接下来可以看初始这个全局变量的函数了ngx_init_cycle(),该函数的定义在Ngx_cycle.c当中。

这里我们分段来解析这个函数:

(1)调用ngx_timezone_update以及ngx_time_update函数更新时间相关的一些东西。

    ngx_timezone_update();
    tp = ngx_timeofday();
    tp->sec = 0;
    ngx_time_update();

(2)创建内存池,并为新的cycle变量分配内存,以及初始化

//创建内存池
    pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
    if (pool == NULL) {
        return NULL;
    }
    pool->log = log;
//在内存池中分配cycle的内存空间
    cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
    if (cycle == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }
//初始化cycle的一些数据
    cycle->pool = pool;
    cycle->log = log;
    cycle->new_log.log_level = NGX_LOG_ERR;
    cycle->old_cycle = old_cycle;

(3)初始化配置前缀,前缀,配置文件,配置参数等字符串

 cycle->conf_prefix.len = old_cycle->conf_prefix.len;
    cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
    if (cycle->conf_prefix.data == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

    cycle->prefix.len = old_cycle->prefix.len;
    cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
    if (cycle->prefix.data == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

    cycle->conf_file.len = old_cycle->conf_file.len;
    cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
    if (cycle->conf_file.data == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }
    ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
                old_cycle->conf_file.len + 1);

    cycle->conf_param.len = old_cycle->conf_param.len;
    cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
    if (cycle->conf_param.data == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

(4)初始化pathes数组的信息:

    n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
    cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
    if (cycle->pathes.elts == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }
    cycle->pathes.nelts = 0;
    cycle->pathes.size = sizeof(ngx_path_t *);
    cycle->pathes.nalloc = n;
    cycle->pathes.pool = pool;
    if (old_cycle->open_files.part.nelts) {
        n = old_cycle->open_files.part.nelts;
        for (part = old_cycle->open_files.part.next; part; part = part->next) {
            n += part->nelts;
        }
    } else {
        n = 20;
    }

(5)初始化open_files链表:

if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
        != NGX_OK)
    {
        ngx_destroy_pool(pool);
        return NULL;
    }

(6)初始化共享内存:

 if (old_cycle->shared_memory.part.nelts) {
        n = old_cycle->shared_memory.part.nelts;
        for (part = old_cycle->shared_memory.part.next; part; part = part->next)
        {
            n += part->nelts;
        }

    } else {
        n = 1;
    }
    if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
        != NGX_OK)
    {
        ngx_destroy_pool(pool);
        return NULL;
    }

(7)初始化监听数组相关的信息:

    n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;

//分配存储监听数组的内存空间
    cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
    if (cycle->listening.elts == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }
//设置监听数组的一些基本信息
    cycle->listening.nelts = 0;
    cycle->listening.size = sizeof(ngx_listening_t);
    cycle->listening.nalloc = n;
    cycle->listening.pool = pool;

(8)初始化reusable_connections_queue

    ngx_queue_init(&cycle->reusable_connections_queue);

(9)为配置上下文分指针配内存

//为配置上下文分配内存
    cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));  //说白了就是一个指针数组
    if (cycle->conf_ctx == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

(10)初始化hostname

(11)循环遍历全局变量ngx_modules,找到所有的core module,并调用它们的create_conf函数为它们创建配置,并将最终配置的信息保存到cycle的conf_ctx相应的位置当中去。其实这里比较有意思的地方时,所有core类型的模块有ngx_core_module,ngx_errlog_module,ngx_events_module,ngx_http_module,但是只有ngx_core_module模块注册了相应的create_conf函数,其余的都为NULL,因此这里其实也就只调用了ngx_core_module的create_conf函数,为该模块创建了配置,其余的core类型的模块并没有创建

   for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->type != NGX_CORE_MODULE) {
            continue;
        }

        module = ngx_modules[i]->ctx;

        if (module->create_conf) {   //创建模块的配置
            rv = module->create_conf(cycle);
            if (rv == NULL) {
                ngx_destroy_pool(pool);
                return NULL;
            }
            cycle->conf_ctx[ngx_modules[i]->index] = rv;   //在cycle的conf_ctx的相应位置保存创建的配置
        }
    }

(12)配置文件的解析,这里通过解析配置文件会调用相应命令的set回调函数,也是很重要的一个过程:

ngx_memzero(&conf, sizeof(ngx_conf_t));
    /* STUB: init array ? */
    conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));
    if (conf.args == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

    conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
    if (conf.temp_pool == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }


    conf.ctx = cycle->conf_ctx;
    conf.cycle = cycle;
    conf.pool = pool;
    conf.log = log;
    conf.module_type = NGX_CORE_MODULE;
    conf.cmd_type = NGX_MAIN_CONF;

#if 0
    log->log_level = NGX_LOG_DEBUG_ALL;
#endif

//一些配置文件的解析
    if (ngx_conf_param(&conf) != NGX_CONF_OK) {
        environ = senv;
        ngx_destroy_cycle_pools(&conf);
        return NULL;
    }

    if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
        environ = senv;
        ngx_destroy_cycle_pools(&conf);
        return NULL;
    }

(13)调用所有core模块的init_conf()函数

    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->type != NGX_CORE_MODULE) {
            continue;
        }

        module = ngx_modules[i]->ctx;

        if (module->init_conf) {
            if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
                == NGX_CONF_ERROR)
            {
                environ = senv;
                ngx_destroy_cycle_pools(&conf);
                return NULL;
            }
        }
    }

(14)遍历open_files链表,并将它们打开

(15)创建共享内存,并初始化它们

(16)遍历listen数组,打开所有的socket

(17)调用所有模块的init_module,其实只有ngx_event_core_module一个模块定义了这个回调函数

    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->init_module) {
            if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
                /* fatal */
                exit(1);
            }
        }
    }

(18)关闭一些残留的资源,并返回cycle

内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
在 Nginx 源码中,`ngx_array_init` 是一个用于初始化 `ngx_array_t` 结构的静态内联函数。该函数的主要作用是设置数组的基本属性、分配内存以存储数组元素,并返回初始化是否成功的状态[^1]。 `ngx_array_t` 是 Nginx 中用于实现动态数组的核心结构体,其定义如下: ```c struct ngx_array_s { void *elts; // 数组数据区起始位置 ngx_uint_t nelts; // 实际存放的元素个数 size_t size; // 每个元素大小 ngx_uint_t nalloc; // 数组所含空间个数 ngx_pool_t *pool; // 该数组在此内存池中分配 }; typedef struct ngx_array_s ngx_array_t; ``` 使用 `ngx_array_init` 函数前,需要先声明一个 `ngx_array_t` 类型的变量,并确保内存池(`ngx_pool_t`)已经正确初始化。然后调用 `ngx_array_init` 函数,传入数组指针、内存池指针、预分配的元素个数和每个元素的大小作为参数。 以下是一个典型的使用 `ngx_array_init` 初始化数组的代码示例: ```c ngx_pool_t *pool; ngx_array_t my_array; int init_elements = 4; size_t element_size = sizeof(int); // 假设 pool 已经通过 ngx_create_pool 初始化 if (ngx_array_init(&my_array, pool, init_elements, element_size) != NGX_OK) { // 处理初始化失败的情况 } // 此时 my_array 已初始化完成,可以开始使用 ``` 在调用 `ngx_array_init` 时,该函数会根据传入的参数设置 `elts` 指针的初始值,并将 `nelts` 设置为 0,`size` 设置为传入的元素大小,`nalloc` 设置为预分配的元素个数,`pool` 设置为传入的内存池指针[^2]。如果初始化成功,函数返回 `NGX_OK`,否则返回 `NGX_ERROR`。 除了直接调用 `ngx_array_init`,也可以使用 `ngx_array_create` 函数一次性分配 `ngx_array_t` 结构体和初始化数组内存空间。`ngx_array_create` 的实现如下: ```c ngx_array_t * ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size) { ngx_array_t *a; a = ngx_palloc(p, sizeof(ngx_array_t)); if (a == NULL) { return NULL; } if (ngx_array_init(a, p, n, size) != NGX_OK) { return NULL; } return a; } ``` 这种方式可以简化数组的创建流程,避免手动分配 `ngx_array_t` 结构体的步骤[^3]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值