Nginx Learning 14 ngx_master_process_cycle (Master process)

Source: Internet
Author: User
Tags sigint signal

The Ngx_master_process_cycle () function, which starts the work process and processes the semaphore, kills or creates a new process as follows:

http://blog.csdn.net/xiaoliangsky/article/details/40866855
A) block all nginx-concerned signals;

b) Set the title of the process (if you use Ps–aux to see the master and worker processes, this is the role of title.) );

c) Initiate a number of work processes in accordance with the number of worker_processes in ngx_core_conf_t;

d) Initiate a buffer management process;

e) Initialization of several flags: ngx_new_binary = 0; Delay = 0; Live = 1;

The latter loop handles different states differently, and those states are mostly different signals that the process receives. Here is an explanation of each process:

f) Delay is not 0, if the received SIGALRM signal NGX_SIGALRM is set to 1, the delay time is multiplied by 2, and finally a real-time type timer is set;

h) suspends the current process, waits for a signal, exits from the suspended state, and resumes execution;

i) The current time is re-updated according to the operating system time after exiting the suspended state;

j) Ngx_reap is 1 (Received SIGCHLD signal, there is a worker exit (ngx_reap==1)), call Ngx_reap_children () to reclaim the child process;

K) If the child process exits (!live) and the current process receives a ngx_signal_value (ngx_shutdown_signal) or ngx_signal_value (ngx_terminate_signal) signal, This process exits the processing (Ngx_master_process_exit ()), the exit processing first deletes the PID file, and then the process that calls all modules exits the hook, destroying the memory pool object;

L) If Ngx_terminate is 1,delay 0, set to 50; if delay>1000, send Sigkill signal to the work process, or send ngx_signal_value to the work process (ngx_terminate _signal) signals;

m) if the ngx_quit is 1, send the Ngx_signal_value (ngx_shutdown_signal) signal to the work process and then close all the sockets in the global listening; continue;

N) If the ngx_reconfigure is 1 (ngx_signal_value (ngx_reconfigure_signal) signal corresponding), re-read the config file, re-create and initialize the Ngx_cycle object, start the work process , start the buffer management process, set live to 1, call Ngx_signal_worker_processes () to send the Ngx_signal_value (ngx_shutdown_signal) signal;

O) Ngx_new_binary is 1 (indicates a newly started process), starts the work process, starts the buffer management process, and then sets the ngx_noaccepting 0;continue;

P) If Ngx_restart is 1 (when ngx_noaccepting=1 will set the Ngx_restart to 1, restart the worker), start the work process, start the buffer management process, live set to 1;

Q) If the Ngx_reopen is 1 (ngx_signal_value (ngx_reopen_signal) signal corresponding), then the log file is reopened, call Ngx_signal_worker_processes () Send Ngx_ Signal_value (ngx_reopen_signal) signal;

R) If Ngx_change_binary is 1 (ngx_signal_value (ngx_changebin_signal) signal corresponding), call Ngx_exec_new_binary () to execute the new process;

s) if Ngx_noaccept is 1 (ngx_signal_value (ngx_noaccept_signal) corresponds), set ngx_noaccepting to 1, call Ngx_signal_worker_processes () Sends a ngx_signal_value (ngx_shutdown_signal) signal.

Here's a look at the code:

1. First block some processing signal, because the woker process is not created, this information is temporarily not processed

The/*master process sets the signal to be processed */Sigemptyset (&set);//To initialize the signal set to null sigaddset (&set, SIGCHLD);//The sub-process exits the sent signal Sigaddset (&amp ; set, SIGALRM);//Sigaddset (&set, SIGIO);//Asynchronous IO Sigaddset (&set, SIGINT);//Terminal signal Sigaddset (&set, Ngx_si Gnal_value (ngx_reconfigure_signal));//sighup, re-read configuration Sigaddset (&set, Ngx_signal_value (ngx_reopen_signal));// SIGUSR1, re-open all open files Sigaddset (&set, Ngx_signal_value (ngx_noaccept_signal));//sigwinch,debug Sigaddset (& Set, Ngx_signal_value (ngx_terminate_signal)),//sigterm, program termination signal sigaddset (&set, Ngx_signal_value (NGX_SHUTDOWN_ SIGNAL));//sigquit, graceful exit signal sigaddset (&set, Ngx_signal_value (ngx_changebin_signal));//SIGUSR2, open new binary Execute file signal if ( Sigprocmask (Sig_block, &set, NULL) = =-1) {//Mask signal ngx_log_error in Set signal set (Ngx_log_alert, Cycle->log, Ngx_errno    , "Sigprocmask () failed"); }
2 Then set the process title:

/* Call Ngx_setproctilte to set the process title, 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 Start the Woker process and astart the buffering management process:

/* Start worker process *    /ngx_start_worker_processes (cycle, ccf->worker_processes,                               ngx_process_respawn); /* Call ngx_start_cache_manager_processes (cycle, 0) to start the file cache management process, some modules need the file cache, such as the fastcgi module, these modules will add the file cache path to the cycle- >paths, the file cache management process periodically calls the files of these modules cache processing hooks to process files cache;*/    ngx_start_cache_manager_processes (cycle, 0);
4master Loop processing signal:

    ngx_new_binary = 0;    Delay = 0;    Sigio = 0;    Live = 1; for (;;) {//delay is used to set the time to wait for the worker process to exit, Master accepts the exit signal,//first sends an exit signal to the worker, and the worker exits takes some time 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", Dela            y);            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;//set the timer to calculate the system real time, send out the SIGALRM signal if (Setitimer (Itimer_real, &it V, NULL) = =-1) {Ngx_log_error (Ngx_log_alert, Cycle->log, Ngx_errno, "Seti            Timer () failed ");        }} ngx_log_debug0 (Ngx_log_debug_event, Cycle->log, 0, "sigsuspend");//each time a signal is processed, the master process will be suspended until a new signal arrives    Sigsuspend (&set);    Ngx_time_update (); NGX_LOG_DEBUG1 (ngx_log_debug_event, Cycle->log, 0, "Wake Up, Sigio%i", Sigio);//Received SIGCHLD signal, have wo            Rker Abnormal exit (ngx_reap = = 1) if (ngx_reap) {ngx_reap = 0; Ngx_log_debug0 (ngx_log_debug_event, Cycle->log, 0, "reap children");//Handle all workers, restart the worker process if a worker exits abnormally,//        Returns 0 live = Ngx_reap_children (cycle) If all worker processes have exited; }//If all worker processes have exited and received a sigterm signal or SIGINT signal or sigquit signal, etc., the//master process begins processing the exit if (!live && (ngx_terminate | | ngx_q        Uit)) {ngx_master_process_exit (cycle);             }//and receive sigterm signal or SIGINT signal exit signal, set the release delay time if (ngx_terminate) {if (delay = = 0) {delay = 50;                } if (Sigio) {sigio--;            Continue            }//total number of child processes Sigio = ccf->worker_processes + 2 */cache processes */; if (Delay > 1000) {//delay greater than 1000, send Sigkill to all worker processes, force kill all worker Processes NGx_signal_worker_processes (cycle, SIGKILL);                                       } else {//sends Sigterm signals to all worker processes, notifies the worker to exit ngx_signal_worker_processes (cycle,            Ngx_signal_value (ngx_terminate_signal));        } continue;                                        }//received Sigquit signal if (ngx_quit) {//Send Sigquit signal ngx_signal_worker_processes (cycle, for all worker processes) Ngx_signal_value (ngx_shutdown_signal));//Turn off all listener sockets socket 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 }//received Sighup signal, if (ngx_reconfigure) {Ngx_recOnfigure = 0;//If it is a smooth upgrade program, restart the worker process and do not want to reinitialize the configuration 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");//Not smooth upgrade, re-read config 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_modu                                       ///Restart worker process ngx_start_worker_processes (cycle, ccf->worker_processes,            Ngx_process_just_respawn);            Ngx_start_cache_manager_processes (cycle, 1); Live = 1;Sends a sigquit signal to all old worker processes ngx_signal_worker_processes (cycle, Ngx_sig        Nal_value (ngx_shutdown_signal));            } if (Ngx_restart) {//restart Ngx_restart = 0;            Ngx_start_worker_processes (cycle, ccf->worker_processes, ngx_process_respawn);            Ngx_start_cache_manager_processes (cycle, 0);        Live = 1;            }//Receive SIGUSR1 signal if (ngx_reopen) {//re-open all files 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));            }//received SIGUSR2 signal, tropical code replacement if (ngx_change_binary) {//execute new program 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);//Smooth Upgrade flag}//receive Sigwinch signal no longer accepts request, send sigquit if (ngx_noaccept) {ngx_noaccept to all worker processes            = 0; ngx_noaccepting = 1;//worker process gracefully exits ngx_signal_worker_processes (cycle, Ng        X_signal_value (ngx_shutdown_signal)); }    }
The main task of the master process is to start the woker process and manage the worker process based on the signal, while event handling is given to the woker process.

Reference:

http://blog.csdn.net/lu_ming/article/details/5144427


Nginx Learning 14 ngx_master_process_cycle (Master process)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.