nginx源码学习(四)worker进程的启动

1、以下是我阅读nginx(1.13.2版本)源码的一些心得,个人觉得学一个东西最好学它的思想,学会举一反三。
因为互联网的东西实在太多 了。本次主要大题看看nginx末尾的ngx_master_process_cycle函数实现大体过程。
因为是从整体上去了解原理,细节的东西省去,
比如ssl初始化等操作。(nginx.c)。

1.首先屏蔽一些处理信号,因为woker进程没有创建,这些信息暂不处理
  
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);


2、设置进程名字,
   
     /*调用ngx_setproctilte设置进程标题,title = "master process" + ngx_argv[0] + ... + ngx_argv[ngx_argc-1];
    */
    size = sizeof(master_process);

    for (i = 0; i < ngx_argc; i++) {
        size += ngx_strlen(ngx_argv[i]) + 1;
    }

    title = ngx_pnalloc(cycle->pool, size);

    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);
 
3、启动worker进程和一个缓存进程

ngx_start_worker_processes(cycle, ccf->worker_processes, NGX_PROCESS_RESPAWN); //启动worker进程,ccf->worker_processes数量从配置文件中读取
ngx_start_cache_manager_processes(cycle, 0); //启动缓存进程


4、master进入循环,接收处理信号,保持子进程数量,重启等工作



ngx_new_binary = 0;
    delay = 0;
    sigio = 0;
    live = 1;

 for ( ;; ) {
//delay用来设置等待worker进程退出的时间,master接受退出信号后,
//首先发送退出信号给worker,而worker退出需要一些时间
        if (delay) {
            if (ngx_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);   //在这里挂起主进程,等待信号的到来
        ngx_time_update();
        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "wake up, sigio %i", sigio);

     //收到了子进程发出的异常信号,子进程退出了
     //SIGCHLD,在一个进程终止或者停止时,将SIGCHLD信号发送给其父进程
     if (ngx_reap) {
            ngx_reap = 0;
            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
          //负责重启子进程,如果是用了killall那种命令一次性把所有子进程关了,那么master就不会再重启子进程了
            live = ngx_reap_children(cycle);
        }

     //如果子进程都挂了,并且当前master也接收到了退出信号,回收资源,不再监听端口,退出
      if (!live && (ngx_terminate || ngx_quit)) {
            ngx_master_process_exit(cycle);
        }

     //下面是对一些信号的处理,不同的信号处理方式不同,退出也不同
//SIGINT
//SIGTERM
     if (ngx_terminate) {
            if (delay == 0) { //初始给子进程50的时间退出
                delay = 50;
            }
            if (sigio) {
                sigio--;
                continue;
            }
            sigio = ccf->worker_processes + 2 /* cache processes */;
            if (delay > 1000) { //延时超过1000了仍然没有退出,就给子进程发kill信号
                ngx_signal_worker_processes(cycle, SIGKILL);
            } else { //否则通知他们处理完请求后退出
                ngx_signal_worker_processes(cycle,  ngx_signal_value(NGX_TERMINATE_SIGNAL));
            }

            continue;
        }

     //SIGQUIT
      if (ngx_quit) {
            ngx_signal_worker_processes(cycle, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
            ls = cycle->listening.elts;
            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;
            if (ngx_new_binary) {
                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);
            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;
            ngx_signal_worker_processes(cycle,
                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
        }


        if (ngx_restart) { //重启,这个好像不是信号,在ngx_reap_children函数中设置的值,比如热更新后这个方法执行
            ngx_restart = 0;
            ngx_start_worker_processes(cycle, ccf->worker_processes,NGX_PROCESS_RESPAWN);
            ngx_start_cache_manager_processes(cycle, 0);
            live = 1;
        }

       //收到了 SIGUSR1信号,重新打开所有的文件句柄
        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));
        }
          //收到了 SIGUSR2信号,热代码替换,平滑升级新的程序,比如从nginx0.8不停服务的情况下升级到1.1
        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);
        }
          //收到了 SIGWINCH,不再接收请求,向所有worker进程发送信号,停止所有的worker进程,主进程继续循环挂起
        if (ngx_noaccept) {
            ngx_noaccept = 0;
            ngx_noaccepting = 1;
            ngx_signal_worker_processes(cycle, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
        }
    }


}




综合上面的,一些nginx操作的命令就可以的得出来了:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值