Nginx學習——進程模型(master 進程)


進程模型

        Nginx分為Single和Master兩種進程模型。Single模型即為單進程方式工作,具有較差的容錯能力,不適合生產之用。Master模型即為一個master進程+N個worker進程的工作方式。

生產環境都是用master-worker模型來工作。


master進程

        我們知道在main函數中完畢了Nginx啟動初始化過程,啟動初始化過程中的一個重要環節就是解析配置文件。回調各個配置指令的回調函數。因此完畢了各個模塊的配置及相互關聯。在全部的這些重要及不重要的初始化完畢后。main函數就開始為我們打開進程的“大門”——調用ngx_master_process_cycle(cycle); 在剖析ngx_master_process_cycle(cycle)之前,我們先來看看該函數中用到的7個標志位,如以下的表所看到的:


進程中接收到的信號對Nginx框架的意義:

信號

相應進程中的全局標志位變量

意義

QUIT

ngx_quit

優雅地關閉整個服務

TERM或INT

ngx_terminate

強制關閉整個服務

USR1

ngx_reopen

又一次打開服務中的全部文件

WINCH

ngx_noaccept

全部子進程不再接受處理新的連接,實際相當於對全部子進程發送QUIT信號

USR2

ngx_change_binary

平滑升級到新版本號的Nginx程序

HUP

ng_reconfigure

重讀配置文件並使服務對新配置項生效

CHLD

ngx_reap

有子進程意外結束。這時須要監控全部子進程,也就是ngx_reap_children方法所做的工作

 

        另一個標志位會用到:ngx_restart。它不過在master工作流程中作為標志位使用。與信號無關。master進程就是依據這8個標志來決定工作流程的,流程圖例如以下:

 

        下面是對ngx_master_process_cycle()函數源代碼的剖析,可參照流程圖理解:

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;
	/*屏蔽一些列信號*/
    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);

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

	
    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
	/*創建子進程,接受請求,完畢響應*/
    ngx_start_worker_processes(cycle, ccf->worker_processes,
                               NGX_PROCESS_RESPAWN);
	/*創建有關cache的子進程*/
    ngx_start_cache_manager_processes(cycle, 0);

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

    for ( ;; ) {
        if (delay) {
            if (ngx_sigalrm) {
                sigio = 0;
                delay *= 2;
                ngx_sigalrm = 0;
            }

            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                           "termination cycle: %d", 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;
			//設置定時器
            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");
		
		/*master進程掛起,等待信號的產生*/
        sigsuspend(&set);

        ngx_time_update();

        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                       "wake up, sigio %i", sigio);
		/*ngx_reap若為1。則表示要監控全部子進程*/
        if (ngx_reap) {
            ngx_reap = 0;
            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
			/*ngx_reap_children(會遍歷ngx_process數組。檢查每一個子進程的狀態,對於非正常退出的子進程會又一次拉起,
			最后。返回一個live標志位,假設全部的子進程都已經正常退出,則live為0。初次之外live為1。*/
            live = ngx_reap_children(cycle);
        }
		/*當live標志為0,而且ngx_terminate或ngx_quit中的一個標志位1時,都將調用ngx_master_process_exit方法退出master進程*/
        if (!live && (ngx_terminate || ngx_quit)) {
            ngx_master_process_exit(cycle);
        }
		/*ngx_terminate標志為1,則向全部子進程發送信號TERM。通知子進程強制退出進程*/
        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;
        }
		//優雅地退出服務。向全部子進程發送QUIT信號,通知它們退出進程
        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;
        }
		/*
		又一次讀取配置文件。Nginx不會讓原先的worker等子進程在從新讀取配置文件。
		它的策略是又一次初始化ngx_cycle_t結構體,用它來讀取新的配置文件。再拉起
		新的worker進程,銷毀就的worker進程。

*/ 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"); /*調用ngx_init_cycle方法又一次初始化ngx_cycle_t結構體*/ 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); //啟動cache_manage子進程 ngx_start_cache_manager_processes(cycle, 1); /* allow new processes to start */ ngx_msleep(100); live = 1; //向原先的全部子進程發送QUIT信號 ngx_signal_worker_processes(cycle, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); } /* 調用ngx_start_worker_processes拉起worker進程。調用ngx_start_cache_manager_processes 依據緩存模塊的請款選擇是否啟動cache manage進程或者cache loader進程, 同一時候將live置為1。ngx_restart置為0 */ 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_reopen = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs"); /*調用ngx_reopen_files方法又一次打開全部文件*/ ngx_reopen_files(cycle, ccf->user); /*向全部子進程發送USR1信號,要求子進程都又一次打開全部文件*/ ngx_signal_worker_processes(cycle, ngx_signal_value(NGX_REOPEN_SIGNAL)); } /*表示須要平滑升級Nginx*/ if (ngx_change_binary) { /*將ngx_change_binary標志置為0*/ ngx_change_binary = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary"); /*調用ngx_exec_new_binary方法用新的子進程啟動新版本號的Nginx程序*/ ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv); } /*向全部子進程發送QUIT信號,要求他們優雅關閉服務*/ if (ngx_noaccept) { /*將ngx_noaccept置為0*/ ngx_noaccept = 0; /*將ngx_noaccepting置為1,表示正在停止接受新的連接*/ ngx_noaccepting = 1; /*向全部子進程發送QUIT信號*/ ngx_signal_worker_processes(cycle, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); } } }



 

 轉載注明出處,謝謝~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM