函数作用
ngx_master_process_cycle()函数,这个函数会启动工作进程干活,并且会处理信号量,处理的过程中会杀死或者创建新的进程。
具体流程
- a) 阻塞所有nginx关心的信号;
- b) 设置进程的title(如果你用ps –aux来查看就可以分清master与worker进程,这就是title的作用。);
- c) 按照ngx_core_conf_t中worker_processes数,启动若干个work进程;
- d) 启动一个缓冲管理进程;
- e) 初始化几个标志:ngx_new_binary = 0; delay = 0; live = 1; 后面一个循环对不同的状态进行不同处理,而那些状态多数是进程收到的不同信号。
后面一个循环对不同的状态进行不同处理,而那些状态多数是进程收到的不同信号:- f) delay不为0,如果收到SIGALRM信号ngx_sigalrm设为1,将delay时间乘以2;最后设置一个实时类型的计时器;
- h) 挂起当前进程,等到有信号,就会从挂起状态退出,继续执行;
- i) 退出挂起状态后,根据操作系统时间重新更新当前时间;
- j) ngx_reap为1(收到SIGCHLD信号,有worker退出(ngx_reap==1)),调用ngx_reap_children()回收子进程;
- k) 如果子进程都退出了(!live)且当前进程收到ngx_signal_value(NGX_SHUTDOWN_SIGNAL)或ngx_signal_value(NGX_TERMINATE_SIGNAL)信号,本进程进行退出处理(ngx_master_process_exit());退出处理先删除pid文件,然后将调用所有模块的进程退出钩子,销毁内存池对象;
- l) 如果ngx_terminate为1,delay为0,就设成50;如果delay>1000,向work进程发送SIGKILL信号,否则向work进程发送ngx_signal_value(NGX_TERMINATE_SIGNAL)信号;
- m) 如果ngx_quit为1,向work进程发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号,然后将所有全局listening中的socket全关闭;continue;
- n) 如果ngx_reconfigure为1(ngx_signal_value(NGX_RECONFIGURE_SIGNAL)信号对应),就重新读取config文件;重新创建并初始化ngx_cycle对象,启动work进程,启动缓冲管理进程,将live设为1,调用ngx_signal_worker_processes(),发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号;
- o) ngx_new_binary为1(表示是新启动的一个进程),启动work进程,启动缓冲管理进程,然后将ngx_noaccepting设0;continue;
- p) 如果ngx_restart为1(当ngx_noaccepting=1的时候会把ngx_restart设为1,重启worker),启动work进程,启动缓冲管理进程,live设为1;
- q) 如果ngx_reopen为1(ngx_signal_value(NGX_REOPEN_SIGNAL)信号对应),则重新找开log文件,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_REOPEN_SIGNAL)信号;
- r) 如果ngx_change_binary为1(ngx_signal_value(NGX_CHANGEBIN_SIGNAL)信号对应),调用ngx_exec_new_binary()执行新进程;
- s) 如果ngx_noaccept为1(ngx_signal_value(NGX_NOACCEPT_SIGNAL)对应),设ngx_noaccepting为1,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号。
源码
void
ngx_master_process_cycle(ngx_cycle_t *cycle)
{
char *title;
u_char *p;
size_t size;
ngx_int_t i;
ngx_uint_t n, sigio;
sigset_t set;
struct itimerval itv;
ngx_uint_t live;
ngx_msec_t delay;
ngx_listening_t *ls;
ngx_core_conf_t *ccf;
/*master 进程设置的要处理的信号*/
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigaddset(&set, SIGALRM);
sigaddset(&set, SIGIO);
sigaddset(&set, SIGINT);
sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL));
sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"sigprocmask() failed");
}
sigemptyset(&set);
/*设置title*/
size = sizeof(master_process);
for (i = 0; i < ngx_argc; i++) {
size += ngx_strlen(ngx_argv[i]) + 1;
}
title = ngx_pnalloc(cycle->pool, size);
if (title == NULL) {
/* fatal */
exit(2);
}
p = ngx_cpymem(title, master_process, sizeof(master_process) - 1);
for (i = 0; i < ngx_argc; i++) {
*p++ = ' ';
p = ngx_cpystrn(p, (u_char *) ngx_argv[i], size);
}
ngx_setproctitle(title);
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
/*启动worker进程*/
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
ngx_start_cache_manager_processes(cycle, 0);//启动cache进程
ngx_new_binary = 0;
delay = 0;
sigio = 0;
live = 1;
for ( ;; ) {
if (delay) {
if (ngx_sigalrm) {//sigalrm为时钟定时信号
sigio = 0;
delay *= 2;
ngx_sigalrm = 0;
}
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"termination cycle: %M", delay);
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = delay / 1000;
itv.it_value.tv_usec = (delay % 1000 ) * 1000;
//设置定时器,以系统真实时间来计算,送出SIGALRM信号
if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setitimer() failed");
}
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "sigsuspend");
sigsuspend(&set);//每次处理完一个信号,master进程会被挂起,直到有新的信号到来
ngx_time_update();
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"wake up, sigio %i", sigio);
if (ngx_reap) {
ngx_reap = 0;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
live = ngx_reap_children(cycle);
}
if (!live && (ngx_terminate || ngx_quit)) {
ngx_master_process_exit(cycle);
}
//TERM or INT信号,强制关闭进程
//设置退出延迟时间
if (ngx_terminate) {
if (delay == 0) {
delay = 50;
}
if (sigio) {
sigio--;
continue;
}
sigio = ccf->worker_processes + 2 /* cache processes */;
if (delay > 1000) {
ngx_signal_worker_processes(cycle, SIGKILL);
} else {
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_TERMINATE_SIGNAL));
}
continue;
}
if (ngx_quit) {
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
ls = cycle->listening.elts;
//关闭所有socket套接字
for (n = 0; n < cycle->listening.nelts; n++) {
if (ngx_close_socket(ls[n].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
ngx_close_socket_n " %V failed",
&ls[n].addr_text);
}
}
cycle->listening.nelts = 0;
continue;
}
//收到SIGHUP信号,
if (ngx_reconfigure) {
ngx_reconfigure = 0;
//如果是平滑升级程序,则重启worker进程,不需要重新初始化配置
if (ngx_new_binary) {
//启动ccf->worker_processes个worker子进程,并设置好每个子进程与
//master父进程之间使用socketpair系统调用建立起来的socket句柄通信机制
//启动方式为NGX_PROCESS_RESPAWN,该值影响ngx_process_t结构体的respawn
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
//执行缓存管理工作的循环方法
ngx_start_cache_manager_processes(cycle, 0);
ngx_noaccepting = 0;
continue;
}
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
//不是平滑升级,重新读取配置
cycle = ngx_init_cycle(cycle);
if (cycle == NULL) {
cycle = (ngx_cycle_t *) ngx_cycle;
continue;
}
ngx_cycle = cycle;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_core_module);
//重启worker进程
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_JUST_RESPAWN);
ngx_start_cache_manager_processes(cycle, 1);
/* allow new processes to start */
ngx_msleep(100);
live = 1;
//关闭所有old worker
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
}
if (ngx_restart) {
ngx_restart = 0;
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
ngx_start_cache_manager_processes(cycle, 0);
live = 1;
}
if (ngx_reopen) {
ngx_reopen = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
ngx_reopen_files(cycle, ccf->user);
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_REOPEN_SIGNAL));
}
if (ngx_change_binary) {
ngx_change_binary = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");
ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);
}
if (ngx_noaccept) {
ngx_noaccept = 0;
ngx_noaccepting = 1;
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
}
}
}
补充
在ngx_master_process_cycle函数中,用到了信号机制。用到了几个linux系统调用:
- int sigprocmask(int how,const sigset_t *set,sigset_t * oldset);
sigprocmask()可以用来改变目前的信号遮罩(掩码),其操作依参数how来决定:
- SIG_BLOCK 新的信号遮罩(掩码)由目前的信号遮罩(掩码)和参数set 指定的信号遮罩(掩码)作联集,也就是将set中的信号掩码添加到现有的信号掩码集合中
- SIG_UNBLOCK 将目前的信号遮罩(掩码)删除掉参数set指定的信号遮罩(掩码),也就是在现有的信号掩码集合中删去set制定的信号掩码
- SIG_SETMASK 将目前的信号遮罩(掩码)设成参数set指定的信号遮罩(掩码)。
(SIG_BLOCK是添加,SIG_UNBLOCK是删除,SIG_SETMASK是直接赋值)- 如果参数oldset不是NULL指针,那么目前的信号遮罩会由此指针返回。
- int sigsuspend(const sigset_t*sigmask);
- (1) 设置新的mask阻塞当前进程;
- (2) 收到信号(sigmask指向的信号中没有的信号),调用该进程设置的信号处理函数;
- (3) 待信号处理函数返回后,恢复原先mask;
- (4) sigsuspend返回
- int sigaddset(sigset_t *set,int signum);
sigaddset()用来将参数signum 代表的信号加入至参数set 信号集里。