当前位置: 首页 > news >正文

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数

nei声明src/core/ngx_cycle.h

ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle);

实现src/core/ngx_cycle.c

ngx_cycle_t *
ngx_init_cycle(ngx_cycle_t *old_cycle)
{void                *rv;char               **senv;ngx_uint_t           i, n;ngx_log_t           *log;ngx_time_t          *tp;ngx_conf_t           conf;ngx_pool_t          *pool;ngx_cycle_t         *cycle, **old;ngx_shm_zone_t      *shm_zone, *oshm_zone;ngx_list_part_t     *part, *opart;ngx_open_file_t     *file;ngx_listening_t     *ls, *nls;ngx_core_conf_t     *ccf, *old_ccf;ngx_core_module_t   *module;char                 hostname[NGX_MAXHOSTNAMELEN];ngx_timezone_update();/* force localtime update with a new timezone */tp = ngx_timeofday();tp->sec = 0;ngx_time_update();log = old_cycle->log;pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);if (pool == NULL) {return NULL;}pool->log = log;cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));if (cycle == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->pool = pool;cycle->log = log;cycle->old_cycle = old_cycle;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->error_log.len = old_cycle->error_log.len;cycle->error_log.data = ngx_pnalloc(pool, old_cycle->error_log.len + 1);if (cycle->error_log.data == NULL) {ngx_destroy_pool(pool);return NULL;}ngx_cpystrn(cycle->error_log.data, old_cycle->error_log.data,old_cycle->error_log.len + 1);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;}n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;if (ngx_array_init(&cycle->paths, pool, n, sizeof(ngx_path_t *))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *));if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,ngx_str_rbtree_insert_value);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;}if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}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;}n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t));ngx_queue_init(&cycle->reusable_connections_queue);cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));if (cycle->conf_ctx == NULL) {ngx_destroy_pool(pool);return NULL;}if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");ngx_destroy_pool(pool);return NULL;}/* on Linux gethostname() silently truncates name that does not fit */hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';cycle->hostname.len = ngx_strlen(hostname);cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);if (cycle->hostname.data == NULL) {ngx_destroy_pool(pool);return NULL;}ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);if (ngx_cycle_modules(cycle) != NGX_OK) {ngx_destroy_pool(pool);return NULL;}for (i = 0; cycle->modules[i]; i++) {if (cycle->modules[i]->type != NGX_CORE_MODULE) {continue;}module = cycle->modules[i]->ctx;if (module->create_conf) {rv = module->create_conf(cycle);if (rv == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->conf_ctx[cycle->modules[i]->index] = rv;}}senv = environ;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 0log->log_level = NGX_LOG_DEBUG_ALL;
#endifif (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;}if (ngx_test_config && !ngx_quiet_mode) {ngx_log_stderr(0, "the configuration file %s syntax is ok",cycle->conf_file.data);}for (i = 0; cycle->modules[i]; i++) {if (cycle->modules[i]->type != NGX_CORE_MODULE) {continue;}module = cycle->modules[i]->ctx;if (module->init_conf) {if (module->init_conf(cycle,cycle->conf_ctx[cycle->modules[i]->index])== NGX_CONF_ERROR){environ = senv;ngx_destroy_cycle_pools(&conf);return NULL;}}}if (ngx_process == NGX_PROCESS_SIGNALLER) {return cycle;}ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);if (ngx_test_config) {if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {goto failed;}} else if (!ngx_is_init_cycle(old_cycle)) {/** we do not create the pid file in the first ngx_init_cycle() call* because we need to write the demonized process pid*/old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,ngx_core_module);if (ccf->pid.len != old_ccf->pid.len|| ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0){/* new pid file name */if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {goto failed;}ngx_delete_pidfile(old_cycle);}}if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {goto failed;}if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {goto failed;}if (ngx_log_open_default(cycle) != NGX_OK) {goto failed;}/* open the new files */part = &cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].name.len == 0) {continue;}file[i].fd = ngx_open_file(file[i].name.data,NGX_FILE_APPEND,NGX_FILE_CREATE_OR_OPEN,NGX_FILE_DEFAULT_ACCESS);ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,"log: %p %d \"%s\"",&file[i], file[i].fd, file[i].name.data);if (file[i].fd == NGX_INVALID_FILE) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_open_file_n " \"%s\" failed",file[i].name.data);goto failed;}#if !(NGX_WIN32)if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,"fcntl(FD_CLOEXEC) \"%s\" failed",file[i].name.data);goto failed;}
#endif}cycle->log = &cycle->new_log;pool->log = &cycle->new_log;/* create shared memory */part = &cycle->shared_memory.part;shm_zone = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;shm_zone = part->elts;i = 0;}if (shm_zone[i].shm.size == 0) {ngx_log_error(NGX_LOG_EMERG, log, 0,"zero size shared memory zone \"%V\"",&shm_zone[i].shm.name);goto failed;}shm_zone[i].shm.log = cycle->log;opart = &old_cycle->shared_memory.part;oshm_zone = opart->elts;for (n = 0; /* void */ ; n++) {if (n >= opart->nelts) {if (opart->next == NULL) {break;}opart = opart->next;oshm_zone = opart->elts;n = 0;}if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {continue;}if (ngx_strncmp(shm_zone[i].shm.name.data,oshm_zone[n].shm.name.data,shm_zone[i].shm.name.len)!= 0){continue;}if (shm_zone[i].tag == oshm_zone[n].tag&& shm_zone[i].shm.size == oshm_zone[n].shm.size&& !shm_zone[i].noreuse){shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
#if (NGX_WIN32)shm_zone[i].shm.handle = oshm_zone[n].shm.handle;
#endifif (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)!= NGX_OK){goto failed;}goto shm_zone_found;}break;}if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {goto failed;}if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {goto failed;}if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {goto failed;}shm_zone_found:continue;}/* handle the listening sockets */if (old_cycle->listening.nelts) {ls = old_cycle->listening.elts;for (i = 0; i < old_cycle->listening.nelts; i++) {ls[i].remain = 0;}nls = cycle->listening.elts;for (n = 0; n < cycle->listening.nelts; n++) {for (i = 0; i < old_cycle->listening.nelts; i++) {if (ls[i].ignore) {continue;}if (ls[i].remain) {continue;}if (ls[i].type != nls[n].type) {continue;}if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,ls[i].sockaddr, ls[i].socklen, 1)== NGX_OK){nls[n].fd = ls[i].fd;nls[n].inherited = ls[i].inherited;nls[n].previous = &ls[i];ls[i].remain = 1;if (ls[i].backlog != nls[n].backlog) {nls[n].listen = 1;}#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)/** FreeBSD, except the most recent versions,* could not remove accept filter*/nls[n].deferred_accept = ls[i].deferred_accept;if (ls[i].accept_filter && nls[n].accept_filter) {if (ngx_strcmp(ls[i].accept_filter,nls[n].accept_filter)!= 0){nls[n].delete_deferred = 1;nls[n].add_deferred = 1;}} else if (ls[i].accept_filter) {nls[n].delete_deferred = 1;} else if (nls[n].accept_filter) {nls[n].add_deferred = 1;}
#endif#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (ls[i].deferred_accept && !nls[n].deferred_accept) {nls[n].delete_deferred = 1;} else if (ls[i].deferred_accept != nls[n].deferred_accept){nls[n].add_deferred = 1;}
#endif#if (NGX_HAVE_REUSEPORT)if (nls[n].reuseport && !ls[i].reuseport) {nls[n].add_reuseport = 1;}
#endifbreak;}}if (nls[n].fd == (ngx_socket_t) -1) {nls[n].open = 1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)if (nls[n].accept_filter) {nls[n].add_deferred = 1;}
#endif
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (nls[n].deferred_accept) {nls[n].add_deferred = 1;}
#endif}}} else {ls = cycle->listening.elts;for (i = 0; i < cycle->listening.nelts; i++) {ls[i].open = 1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)if (ls[i].accept_filter) {ls[i].add_deferred = 1;}
#endif
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (ls[i].deferred_accept) {ls[i].add_deferred = 1;}
#endif}}if (ngx_open_listening_sockets(cycle) != NGX_OK) {goto failed;}if (!ngx_test_config) {ngx_configure_listening_sockets(cycle);}/* commit the new cycle configuration */if (!ngx_use_stderr) {(void) ngx_log_redirect_stderr(cycle);}pool->log = cycle->log;if (ngx_init_modules(cycle) != NGX_OK) {/* fatal */exit(1);}/* close and delete stuff that lefts from an old cycle *//* free the unnecessary shared memory */opart = &old_cycle->shared_memory.part;oshm_zone = opart->elts;for (i = 0; /* void */ ; i++) {if (i >= opart->nelts) {if (opart->next == NULL) {goto old_shm_zone_done;}opart = opart->next;oshm_zone = opart->elts;i = 0;}part = &cycle->shared_memory.part;shm_zone = part->elts;for (n = 0; /* void */ ; n++) {if (n >= part->nelts) {if (part->next == NULL) {break;}part = part->next;shm_zone = part->elts;n = 0;}if (oshm_zone[i].shm.name.len != shm_zone[n].shm.name.len) {continue;}if (ngx_strncmp(oshm_zone[i].shm.name.data,shm_zone[n].shm.name.data,oshm_zone[i].shm.name.len)!= 0){continue;}if (oshm_zone[i].tag == shm_zone[n].tag&& oshm_zone[i].shm.size == shm_zone[n].shm.size&& !oshm_zone[i].noreuse){goto live_shm_zone;}break;}ngx_shm_free(&oshm_zone[i].shm);live_shm_zone:continue;}old_shm_zone_done:/* close the unnecessary listening sockets */ls = old_cycle->listening.elts;for (i = 0; i < old_cycle->listening.nelts; i++) {if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {continue;}if (ngx_close_socket(ls[i].fd) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,ngx_close_socket_n " listening socket on %V failed",&ls[i].addr_text);}#if (NGX_HAVE_UNIX_DOMAIN)if (ls[i].sockaddr->sa_family == AF_UNIX) {u_char  *name;name = ls[i].addr_text.data + sizeof("unix:") - 1;ngx_log_error(NGX_LOG_WARN, cycle->log, 0,"deleting socket %s", name);if (ngx_delete_file(name) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,ngx_delete_file_n " %s failed", name);}}#endif}/* close the unnecessary open files */part = &old_cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {continue;}if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_close_file_n " \"%s\" failed",file[i].name.data);}}ngx_destroy_pool(conf.temp_pool);if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {ngx_destroy_pool(old_cycle->pool);cycle->old_cycle = NULL;return cycle;}if (ngx_temp_pool == NULL) {ngx_temp_pool = ngx_create_pool(128, cycle->log);if (ngx_temp_pool == NULL) {ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,"could not create ngx_temp_pool");exit(1);}n = 10;if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n,sizeof(ngx_cycle_t *))!= NGX_OK){exit(1);}ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *));ngx_cleaner_event.handler = ngx_clean_old_cycles;ngx_cleaner_event.log = cycle->log;ngx_cleaner_event.data = &dumb;dumb.fd = (ngx_socket_t) -1;}ngx_temp_pool->log = cycle->log;old = ngx_array_push(&ngx_old_cycles);if (old == NULL) {exit(1);}*old = old_cycle;if (!ngx_cleaner_event.timer_set) {ngx_add_timer(&ngx_cleaner_event, 30000);ngx_cleaner_event.timer_set = 1;}return cycle;failed:if (!ngx_is_init_cycle(old_cycle)) {old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,ngx_core_module);if (old_ccf->environment) {environ = old_ccf->environment;}}/* rollback the new cycle configuration */part = &cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {continue;}if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_close_file_n " \"%s\" failed",file[i].name.data);}}/* free the newly created shared memory */part = &cycle->shared_memory.part;shm_zone = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;shm_zone = part->elts;i = 0;}if (shm_zone[i].shm.addr == NULL) {continue;}opart = &old_cycle->shared_memory.part;oshm_zone = opart->elts;for (n = 0; /* void */ ; n++) {if (n >= opart->nelts) {if (opart->next == NULL) {break;}opart = opart->next;oshm_zone = opart->elts;n = 0;}if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {continue;}if (ngx_strncmp(shm_zone[i].shm.name.data,oshm_zone[n].shm.name.data,shm_zone[i].shm.name.len)!= 0){continue;}if (shm_zone[i].tag == oshm_zone[n].tag&& shm_zone[i].shm.size == oshm_zone[n].shm.size&& !shm_zone[i].noreuse){goto old_shm_zone_found;}break;}ngx_shm_free(&shm_zone[i].shm);old_shm_zone_found:continue;}if (ngx_test_config) {ngx_destroy_cycle_pools(&conf);return NULL;}ls = cycle->listening.elts;for (i = 0; i < cycle->listening.nelts; i++) {if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {continue;}if (ngx_close_socket(ls[i].fd) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,ngx_close_socket_n " %V failed",&ls[i].addr_text);}}ngx_destroy_cycle_pools(&conf);return NULL;
}

ngx_init_cycle 是 Nginx 核心模块中的一个关键函数,
负责初始化 Nginx 的运行环境。
它基于传入的旧周期(old_cycle)创建一个新的周期(cycle),
并完成一系列复杂的初始化工作,
包括配置文件解析、共享内存分配、监听套接字设置等。


函数签名

ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)

作用

  • 接收一个指向旧周期(old_cycle)的指针。

  • 返回一个新创建的周期(cycle)指针,表示初始化后的运行时环境。

  • 如果初始化失败,返回 NULL

  • 新周期会继承旧周期的部分信息(如路径、配置文件路径等),同时根据新的配置进行更新。


   ngx_timezone_update();/* force localtime update with a new timezone */tp = ngx_timeofday();tp->sec = 0;ngx_time_update();

ngx_timezone_update()更新进程的时区缓存
ngx_timeofday() 获取当前时间缓存对象 tp。
tp->sec = 0 强制标记时间缓存为“已失效”。
ngx_time_update() 重新计算当前时间并更新缓存。

tp->sec = 0; 的作用是强制标记当前时间缓存为无效,从而确保在调用 ngx_time_update() 时会重新调用系统函数来获取最新的时间戳。
如果没有强制刷新时间缓存(即 tp->sec != 0),ngx_time_update() 可能不会真正调用系统函数,而是直接使用缓存值


   log = old_cycle->log;

 从旧的 Nginx 运行周期(old_cycle)中继承日志对象(log),并将其赋值给当前的局部变量 log

新周期初始化时,尚未解析新的配置文件,无法确定新的日志路径或级别。
若直接使用新配置的日志可能失败(如路径无效),导致错误信息无法记录。
复用旧日志配置,新周期完全初始化前,确保初始化阶段的日志记录可靠。


   pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);if (pool == NULL) {return NULL;}pool->log = log;

创建一个内存池(memory pool),用于管理 Nginx 运行周期(cycle)中的内存分配

NGX_CYCLE_POOL_SIZE
这是一个宏定义,表示内存池的初始大小。

log 是一个指向日志对象的指针,用于记录内存池操作中的错误或调试信息。


   cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));if (cycle == NULL) {ngx_destroy_pool(pool);return NULL;}

从内存池分配一个新的 ngx_cycle_t 结构(核心运行时上下文)


   cycle->pool = pool;cycle->log = log;cycle->old_cycle = old_cycle;

 cycle->pool = pool;
关联新内存池到新周期。
所有后续内存分配均通过此池进行,确保统一管理。

 cycle->log = log;
设置新周期的日志对象。
确保新周期的所有操作使用继承的日志配置,直到新配置生效。

 cycle->old_cycle = old_cycle;
保存旧周期指针到新周期。
在平滑重启或重新配置时,新周期需要访问旧周期的资源(如监听套接字、共享内存)。
资源复用:通过 old_cycle 复用旧资源(如 SO_REUSEPORT 套接字),实现零停机更新。
渐进式释放:旧周期资源在新周期稳定后逐步清理,避免服务中断。


   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;}

复制配置文件前缀(conf_prefix

从旧周期复制 conf_prefix,用于定位配置文件
平滑重启:保持配置路径一致,避免重新解析路径导致的延迟。

ngx_pstrdup


    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;}

从旧周期复制 prefix(如 /usr/local/nginx/),用于解析相对路径


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

继承错误日志路径:复制旧周期的错误日志文件路径
日志连续性:初始化阶段使用旧日志配置,避免日志记录中断
字符串安全性:通过 ngx_cpystrn 确保字符串以 \0 结尾


   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;}

继承命令行配置参数:复制通过 -g 参数传递的配置


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

确定 paths 数组的初始容量 n

若旧周期(old_cycle)存在路径配置,则继承其大小;否则预分配 10 个元素。

paths 用于存储 Nginx 运行时路径(如临时文件目录)。旧周期可能已包含路径信息(如 client_body_temp_path),新周期需复用或初始化。

资源复用:继承旧周期的容量,避免重复计算路径数量。
预分配优化:默认值 10 是经验值,平衡内存占用与扩容开销。


    ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *));

将 paths 数组的前 n 个元素清零(初始化为 NULL

ngx_array_init 分配的内存未初始化,可能包含脏数据。路径指针需显式置空,避免后续误判

确保数组初始状态明确(所有元素为 NULL


    if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}

初始化数组 config_dump,用于存储配置转储条目

使用内存池 pool 分配内存,初始容量为 1,每个元素大小为 ngx_conf_dump_t

若初始化失败(返回 NGX_ERROR),销毁内存池并终止初始化。

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_conf_dump_t-CSDN博客


    ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,ngx_str_rbtree_insert_value);

初始化红黑树 config_dump_rbtree,用于快速查找和去重

根节点为 config_dump_rbtree,哨兵节点为 config_dump_sentinel
使用 ngx_str_rbtree_insert_value 作为插入回调,按字符串键排序。
 


Ubuntu 下 nginx-1.24.0 源码分析 - ngx_rbtree_init-CSDN博客

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_str_rbtree_insert_value-CSDN博客

ngx_array_t config_dump

存储配置条目

动态数组,存储 ngx_conf_dump_t 结构体

ngx_rbtree_t config_dump_rbtree

索引配置条目,加速查找与去重

红黑树键值为配置名称(ngx_str_t),节点数据指向 config_dump 数组素


                
        

ngx_conf_dump_t.name(配置块名称)作为红黑树的键,通过 ngx_str_rbtree_insert_value 回调按字符串排序。


功能分离:
数组存储数据,红黑树管理索引,职责清晰。


   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;}

open_files 存储 nginx 运行时需持久打开的文件(如日志文件、共享内存文件)。

在平滑重启或重新配置时,新周期需继承这些文件以避免频繁打开/关闭。

判断旧cycle(old_cycleopen_files链表的第一个节点(part)是否有元素

open_filesngx_list_t类型,内部由多个ngx_list_part_t节点组成,每个节点包含多个元素。

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_list_t-CSDN博客

如果旧cycleopen_files存在元素,进入计算总元素数的逻辑

初始化容量 n 为旧周期文件数量

获取旧周期 open_files 列表第一个分片(part)的元素数量。

ngx_list_t 是分片链表结构,每个分片(part)包含 nelts 个元素。
此处初始化 n 为第一个分片的元素数。

遍历旧周期 open_files 的所有分片,累加总元素数到 n

ngx_list_t 可能包含多个分片(如元素数量超过单个分片容量),需遍历所有分片统计总数。

若旧周期无文件,设置初始容量 n = 20


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

初始化新周期的 open_files 列表。

pool:内存池,用于管理列表内存。

n:初始容量(继承旧文件数或默认 20)。

sizeof(ngx_open_file_t):每个元素的大小(文件描述符结构)。

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_list_init-CSDN博客


    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;}

判断旧周期(old_cycle)的共享内存列表是否非空。
shared_memory 存储 nginx 的共享内存区域
在平滑重启或重新配置时,新周期需继承这些区域以避免重复创建。

获取旧周期共享内存列表第一个分片(part)的元素数量

ngx_list_t 是分片链表结构,每个分片(part)包含 nelts 个元素。此处初始化 n 为第一个分片的元素数。

遍历旧周期 shared_memory 的所有分片,累加总元素数到 n

ngx_list_t 可能包含多个分片(如元素数量超过单个分片容量),需遍历所有分片统计总数。

若旧周期无共享内存,设置初始容量 n = 1


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

初始化新周期的 shared_memory 列表。


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

根据旧周期(old_cycle)的监听套接字数量设置新周期的初始容量 n
若旧周期无监听套接字,则默认预分配 10 个元素。

old_cycle->listening.nelts 是旧周期监听数组的元素数量。
若存在旧监听套接字,继承其数量;否则使用默认值 10
监听数组(listening)存储 Nginx 监听的端口和套接字信息(如 listen 80;)。

在 Nginx 启动时,所有监听套接字会被初始化并集中存储到 cycle->listening 数组中,方便后续统一操作(如绑定、监听、关闭)。

初始化阶段:在 ngx_init_cycle() 函数中,Nginx 会遍历 cycle->listening 数组,为每个监听创建套接字并设置为监听状态。

平滑重启:当配置文件更改时,Nginx 可以平滑地切换到新配置,而不中断当前连接。

这需要能够比较新旧配置中的监听端口,cycle->listening 提供了这种能力


  if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}

初始化 cycle->listening 数组


    ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t));

将监听数组的前 n 个元素清零。

ngx_memzero 是 memset 的封装,确保内存初始化为 0

动态数组分配的内存可能包含脏数据,直接使用可能导致未定义行为(如误判套接字状态)


   ngx_queue_init(&cycle->reusable_connections_queue);

初始化可重用连接队列:
cycle->reusable_connections_queue 初始化为一个空的双向链表(队列),用于管理可重用的空闲连接(ngx_connection_t)。

ngx_queue_t 是 Nginx 的双向链表节点结构

ngx_queue_init(q) 宏会将队列的 prev next 指针均指向自身,表示队列为空。

队列用途:
reusable_connections_queue 存储当前未被使用的连接对象(如已关闭的 TCP 连接),这些连接可被重新分配以避免频繁创建/销毁的开销。

频繁调用 accept() 创建新连接会导致性能下降,而重用空闲连接可显著降低延迟。

资源复用:
当连接关闭时,Nginx 不会立即释放其资源(如套接字、内存),而是将其放入 reusable_connections_queue,等待后续请求复用。

在内存紧张时,可通过调整队列大小(worker_connections)动态平衡资源。

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_queue_init-CSDN博客


    cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));if (cycle->conf_ctx == NULL) {ngx_destroy_pool(pool);return NULL;}

从内存池 pool 中分配一个指针数组 conf_ctx,每个元素对应一个模块的配置结构指针。

ngx_max_module:编译时确定的模块总数

sizeof(void *):每个指针的大小

数组长度为 ngx_max_module,索引为模块的唯一标识符(module->index)。

若内存分配失败,销毁内存池并终止初始化。

Ubuntu 下 nginx-1.24.0 源码分析 - conf_ctx-CSDN博客


    if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");ngx_destroy_pool(pool);return NULL;}

调用 gethostname() 系统调用获取本地主机名,存储到 hostname 缓冲区。

缓冲区大小为 NGX_MAXHOSTNAMELEN

若调用失败(返回 -1),记录致命错误(NGX_LOG_EMERG),销毁内存池并终止初始化。

gethostname-CSDN博客


    hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';cycle->hostname.len = ngx_strlen(hostname);

确保 hostname 以 \0 结尾,避免未终止字符串导致的安全风险。

Linux 的 gethostname() 在缓冲区不足时静默截断,但不会添加 \0
手动设置最后一个字节为 \0,确保字符串合法性。

计算主机名的实际长度(不含终止符),存储到 cycle->hostname.len


    cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);if (cycle->hostname.data == NULL) {ngx_destroy_pool(pool);return NULL;}

从内存池 pool 分配内存,存储主机名的副本

ngx_pnalloc 分配指定长度的内存
若分配失败,销毁内存池并终止初始化。


    ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);

将主机名转换为全小写,存储到 cycle->hostname.data

ngx_strlow 是 Nginx 的封装函数,逐字符转换为小写。

主机名在 DNS 和 HTTP 协议中通常不区分大小写,统一格式避免配置或路由问题。

统一小写格式,简化后续比较和匹配逻辑(如虚拟主机配置)。


    if (ngx_cycle_modules(cycle) != NGX_OK) {ngx_destroy_pool(pool);return NULL;}

调用 ngx_cycle_modules 初始化 cycle->modules 数组,该数组包含所有核心模块的指针。

Nginx 模块分为核心模块(NGX_CORE_MODULE)、事件模块、HTTP 模块等。

ngx_cycle_modules 会遍历全局模块列表(ngx_modules),筛选出核心模块并按优先级排序。
若模块初始化失败(如内存不足),立即回滚资源。

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_cycle_modules-CSDN博客


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

遍历所有核心模块

Ubuntu 下 nginx-1.24.0 源码分析 - cycle->modules[i]->type-CSDN博客


module = cycle->modules[i]->ctx;

获取核心模块的配置

module 的类型是:

ngx_core_module_t   *module;

Ubuntu 下 nginx-1.24.0 源码分析 - cycle->modules[i]->ctx-CSDN博客

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_core_module_t-CSDN博客


 

相关文章:

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数

nei声明在 src/core/ngx_cycle.h ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle);实现在 src/core/ngx_cycle.c ngx_cycle_t * ngx_init_cycle(ngx_cycle_t *old_cycle) {void *rv;char **senv;ngx_uint_t i, n;ngx_log_t …...

【redis】数据类型之geo

Redis的GEO数据类型用于存储地理位置信息&#xff08;如经纬度&#xff09;&#xff0c;并提供高效的地理位置查询功能&#xff08;如计算两地距离、搜索附近地点等&#xff09;。其底层基于Sorted Set&#xff08;有序集合&#xff09;实现&#xff0c;通过Geohash编码将经纬度…...

vue3 vite或者vue2 百度地图(卫星图)离线使用详细讲解

1、在Windows上下载瓦片&#xff0c;使用的工具为: 全能电子地图下载器3.0最新版&#xff08;推荐&#xff09; 下载后解压&#xff0c;然后进入目录"全能电子地图下载器3.0最新版&#xff08;推荐&#xff09;\全能电子地图下载器3.0\MapTileDownloader" 在这个目录…...

《Python实战进阶》No17: 数据库连接与 ORM(SQLAlchemy 实战)

No17: 数据库连接与 ORM&#xff08;SQLAlchemy 实战&#xff09; 摘要 本文深入探讨SQLAlchemy在复杂场景下的高级应用&#xff0c;涵盖四大核心主题&#xff1a; 会话生命周期管理&#xff1a;通过事件钩子实现事务监控与审计追踪混合继承映射&#xff1a;结合单表/连接表继…...

工程化与框架系列(27)--前端音视频处理

前端音视频处理 &#x1f3a5; 引言 前端音视频处理是现代Web应用中的重要组成部分&#xff0c;涉及音频播放、视频处理、流媒体传输等多个方面。本文将深入探讨前端音视频处理的关键技术和最佳实践&#xff0c;帮助开发者构建高质量的多媒体应用。 音视频技术概述 前端音视…...

芋道打包时报错:缺失@unocss插件

在遇到打包时&#xff0c;报这个错误&#xff0c;提示构建失败是因为 ESLint 在加载 unocss 插件时&#xff0c;找不到 unocss/eslint-plugin 模块 解决办法&#xff1a;安装缺失的依赖&#xff1a;保证unocss/eslint-plugin已经被正确安装&#xff0c; 使用以下命令安装&…...

PY32MD320单片机 QFN32封装,内置多功能三相 NN 型预驱。

PY32MD320单片机是普冉半导体的一款电机专用MCU&#xff0c;芯片采用了高性能的 32 位 ARM Cortex-M0 内核&#xff0c;主要用于电机控制。PY32MD320嵌入高达 64 KB Flash 和 8 KB SRAM 存储器&#xff0c;最高工作频率 48 MHz。PY32MD320单片机的工作温度范围为 -40 ~ 105 ℃&…...

深入解析 configService.addListener 使用中的注意事项

在使用 Nacos 的 configService.addListener 方法进行配置监听时&#xff0c;为了确保程序的稳定性、可靠性以及高效性&#xff0c;有诸多注意事项需要我们关注。下面将对这些关键要点进行详细阐述。 一、连接稳定性 1.1 网络连接问题 Nacos 客户端与服务端通过网络进行通信&…...

Windows控制台函数:控制台读取输入函数ReadConsoleA()

目录 什么是 ReadConsoleA&#xff1f; 它长什么样&#xff1f; 怎么用它&#xff1f; 它跟 std::cin 有什么不一样&#xff1f; 注意事项 什么是 ReadConsoleA&#xff1f; ReadConsoleA 是一个 Windows API 函数&#xff0c;用来从控制台读取用户输入。想象一下&#…...

奇安信 2025 年护网蓝队初选笔试题(附答案解析)

&#x1f525; 爆款 CSDN 题库 | 超全护网蓝队笔试真题 | 含详细答案解析 &#x1f525; 熬夜为大家整理了 奇安信 2025 年护网蓝队初选笔试题&#xff0c;&#xff08;关注我我会持续更新&#xff09;涵盖 SQL 注入、Web 安全、渗透测试、二进制安全 等核心知识点&#xff0c;…...

国产编辑器EverEdit - Web预览设置

1 设置-高级-Web预览 1.1 设置说明 选择主菜单工具 -> 设置 -> 常规&#xff0c;在弹出的选项窗口中选择Web预览分类&#xff0c;如下图所示&#xff1a; 1.1.1 本地浏览HTML文件 如果用户只是在本地浏览HTML文件&#xff0c;即直接用浏览器打开HTML文件&#xff0c;确…...

P8686 [蓝桥杯 2019 省 A] 修改数组--并查集 or Set--lower_bound()的解法!!!

P8686 [蓝桥杯 2019 省 A] 修改数组--并查集 题目 并查集解析代码【并查集解】 Set 解法解析lower_bound代码 题目 并查集解析 首先先让所有的f&#xff08;i&#xff09;i&#xff0c;即每个人最开始的祖先都是自己&#xff0c;然后就每一次都让轮到那个数的父亲1&#xff08…...

HTML 编辑器推荐与 VS Code 使用教程

在进行 HTML 编程时&#xff0c;选择一款合适的 HTML 编辑器能极大地提高开发效率。以下为大家推荐几款常用且功能强大的 HTML 编辑器&#xff0c;同时详细介绍如何使用 VS Code 创建和预览 HTML 文件。 一、HTML 编辑器推荐 VS Code&#xff1a;由微软开发&#xff0c;是一款…...

MyBatis增删改查:静态与动态SQL语句拼接及SQL注入问题解析

MyBatis 是一个优秀的持久层框架&#xff0c;它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。本文将深入探讨 MyBatis 中的增删改查操作&#xff0c;重点讲解静态与动态 SQL 语句的拼接&#xff0c;并分析 S…...

在运维工作中,Lvs、nginx、haproxy工作原理分别是什么?

在运维工作中&#xff0c;LVS、NGINX和HAProxy都是常用的负载均衡和反向代理工具&#xff0c;它们在高可用性和负载均衡场景中发挥重要作用。以下是其原理和应用场景详解&#xff1a; LVS&#xff08;Linux Virtual Server&#xff09; 工作原理 LVS是基于Linux内核的负载均…...

linux学习(五)(服务器审查,正常运行时间负载,身份验证日志,正在运行的服务,评估可用内存)

服务器审查 在 Linux 中审查服务器的过程包括评估服务器的性能、安全性和配置&#xff0c;以确定需要改进的领域或任何潜在问题。审查的范围可以包括检查安全增强功能、检查日志文件、审查用户帐户、分析服务器的网络配置以及检查其软件版本。 Linux 以其稳定性和安全性而闻名…...

Java在小米SU7 Ultra汽车中的技术赋能

目录 一、智能驾驶“大脑”与实时数据 场景一&#xff1a;海量数据的分布式计算 场景二&#xff1a;实时决策的毫秒级响应 场景三&#xff1a;弹性扩展与容错机制 技术隐喻&#xff1a; 二、车载信息系统&#xff08;IVI&#xff09;的交互 场景一&#xff1a;Android Automo…...

开发环境搭建-02.后端环境搭建-熟悉项目结构

一.后端环境搭建...

js实现pdf文件路径预览和下载

预览 直接浏览器窗口打开默认就是预览 window.open(文件路径)下载 function downloadPDF(url, filename) {fetch(url).then(response > response.blob()).then(blob > {const link document.createElement(a);link.href URL.createObjectURL(blob);link.download fi…...

【RAG】基于向量检索的 RAG (BGE示例)

RAG机器人 结构体 文本向量化: 使用 BGE 模型将文档和查询编码为向量。 &#xff08;BGE 是专为检索任务优化的开源 Embedding 模型&#xff0c;除了本文API调用&#xff0c;也可以通过Hugging Face 本地部署BGE 开源模型&#xff09; 向量检索: 从数据库中找到与查询相关的文…...

Vue源码解析之mustache模板引擎

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…...

python: DDD using postgeSQL and SQL Server

postgreSQL 注意&#xff1a; # psycopg 2 驱动的连接字符串 #engine create_engine(postgresql://post:geovindulocalhost:5433/TechnologyGame) #Session sessionmaker(bindengine)# 使用 psycopg3 驱动的连接字符串 #engine create_engine(postgresqlpsycopg://user:g…...

Python实例:PyMuPDF实现PDF翻译,英文翻译为中文,并按段落创建中文PDF

基于PyMuPDF与百度翻译的PDF翻译处理系统开发:中文乱码解决方案与自动化排版实践 一 、功能预览:将英文翻译为中文后创建的PDF 二、完整代码 from reportlab.lib.pagesizes import letter from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle...

IntelliJ IDEA 2021版创建springboot项目的五种方式

第一种方式&#xff0c;通过https://start.spring.io作为spring Initializr的url来创建项目。 第二种方式&#xff0c;通过https://start.spring.io官网来直接创建springboot项目压缩包&#xff0c;然后导入至我们的idea中。 点击generate后&#xff0c;即可生成压缩包&#xf…...

c#面试题整理6

1.String类能否被继承&#xff0c;为什么 可以看到String类的修饰符是sealed&#xff0c;即是密封类&#xff0c;故不可被继承 2.一个对象的方法是否只能由一个线程访问 不是&#xff0c;但是可通过同步机制&#xff0c;确保同一个时间只有一个线程访问 3.计算2*8&#xff…...

跟着 Lua 5.1 官方参考文档学习 Lua (12)

文章目录 5.7 – Input and Output Facilities补充内容io.input ([file])io.read ()io.write ()io.output ([file])io.lines ([filename])io.flush ()io.close ([file])io.open (filename [, mode])io.popen (prog [, mode])io.tmpfile ()io.type (ob)file:read ()file:lines (…...

大语言模型中的归一化技术:LayerNorm与RMSNorm的深入研究

在LLama等大规模Transformer架构的语言模型中&#xff0c;归一化模块是构建网络稳定性的关键组件。本文将系统分析归一化技术的必要性&#xff0c;并详细阐述为何原始Transformer架构中的LayerNorm在LLama模型中被RMSNorm所替代的技术原理。 归一化技术的基础原理 归一化的核…...

nodejs使用WebSocket实现聊天效果

在nodejs中使用WebSocket实现聊天效果&#xff08;简易实现&#xff09; 安装 npm i ws 实现 创建 server.js /*** 创建一个 WebSocket 服务器&#xff0c;监听指定端口&#xff0c;并处理客户端连接和消息。** param {Object} WebSocket - 引入的 WebSocket 模块&#xff0c…...

【仿muduo库one thread one loop式并发服务器实现】

文章目录 一、项目介绍1-1、项目总体简介1-2、项目开发环境1-3、项目核心技术1-4、项目开发流程1-5、项目如何使用 二、框架设计2-1、功能模块划分2-1-1、SERVER模块2-1-2、协议模块 2-2、项目蓝图2-2-1、整体图2-2-2、模块关系图2-2-2-1、Connection 模块关系图2-2-2-2、Accep…...

10.2 继承与多态

文章目录 继承多态 继承 继承的作用是代码复用。派生类自动获得基类的除私有成员外的一切。基类描述一般特性&#xff0c;派生类提供更丰富的属性和行为。在构造派生类时&#xff0c;其基类构造函数先被调用&#xff0c;然后是派生类构造函数。在析构时顺序刚好相反。 // 基类…...