In the previous analysis, we analyzed the shared memory and sockets of the Nginx interprocess communication mechanism. This time we analyze the remaining one interprocess communication mechanism---signal.
First, the signal and semaphore are differentiated: the signal is the mechanism for interprocess communication, and the semaphore is a mechanism for ensuring that shared resources are not concurrently accessed, such as the synchronization of shared resources under multiple processes using semaphores as mutexes.
where the signal is used in the 1.nginx.
For example, we enter the command:./nginx-s reload or./nginx-s Stop,nginx Reload the configuration file or terminate it. All these places use the signaling mechanism. Another example: The worker process hangs, how the master process is known. The answer is a signal, and when the worker process hangs off, Nginx can detect the SIGCHLD signal for subsequent processing.
2.nginx Signal how to achieve it.
Nginx the signal information in the structure of the ngx_signal_t, as follows:
.../os/unix/ngx_process.c:
/* Nginx signal
/typedef struct {
int signo; /* Signal Number
* /char *signame; /* Signal name
* /char *name; /* signal corresponding to the Nginx
command /Void (*handler) (int signo); /* Processing Signal callback method *
/} ngx_signal_t;
Each signal corresponds to a struct, and all the signals are placed in the array signals of the signal, as follows:
.../os/unix/ngx_process.c:
/* The process will define all signals/ngx_signal_t signals[] = {{Ngx_signal_value (ngx_reconfigure_signal), "SIG" Ngx_value (ngx_re configure_signal), "Reload", Ngx_signal_handler}, {Ngx_signal_value (ngx_reopen_signal), "SIG" Ngx
_value (ngx_reopen_signal), "REOPEN", Ngx_signal_handler}, {Ngx_signal_value (ngx_noaccept_signal), "SIG" Ngx_value (ngx_noaccept_signal), "", Ngx_signal_handler}, {Ngx_signal_value (ngx_terminate_signal) , "SIG" Ngx_value (ngx_terminate_signal), "Stop", Ngx_signal_handler}, {Ngx_signal_value (NGX_SHUTD own_signal), "SIG" Ngx_value (ngx_shutdown_signal), "Quit", Ngx_signal_handler}, {Ngx_signal_value (ngx_changebin_signal), "SIG" Ngx_value (ngx_changebin_signal), "", Ngx_signal_handler}, {SIGALRM, "Sigalrm", "" ", Ngx_signal_handler}, {SIGINT," SIGINT "," ", Ngx_signal_handler}, {Sigio," Sigio "," ", Ngx_si Gnal_handler}, {SIGCHLD, "sigchld", "" ", Ngx_signal_handler}, {Sigsys," Sigsys, Sig_ign "," ", sig_ign}, {sigpipe," sigpipe, SI
G_ign "," ", sig_ign}, {0, NULL," ", NULL}};In the array above we see a lot of familiar signals, such as RELOAD,STOP,SIGCHLD and so on. If we want to define our own signal in NGIXN, we can add the signal to the array.
The signal name in ngx_signal_t is as follows: Core/ngx_config.h:
/* TODO: #ifndef * *
#define ngx_shutdown_signal QUIT
#define ngx_terminate_signal TERM
#define Ngx_noaccept_signal Winch
#define Ngx_reconfigure_signal HUP
#if (ngx_linuxthreads)
#define Ngx_reopen_signal INFO
#define ngx_changebin_signal xcpu
#else
#define Ngx_reopen_ SIGNAL USR1
#define ngx_changebin_signal USR2
#endif
3. Registration signal and its callback method
The registration signal is completed in the function ngx_init_signals, the callback function of the signal is unified as ngx_signal_handler, and the function is then judged by what signal is callback the method, thus executing the corresponding bit. And then continue the subsequent processing.
Source:
ngx_int_t ngx_init_signals (ngx_log_t *log) {ngx_signal_t *sig;
struct Sigaction sa; /* Add signal in signals array/for (sig = signals; Sig->signo!= 0; sig++) {Ngx_memzero (&sa, sizeof struct
ction)); Sa.sa_handler = sig->handler;
/* Signal callback method */Sigemptyset (&sa.sa_mask); if (Sigaction (Sig->signo, &sa, NULL) = = 1) {/* Registration signal */#if (Ngx_valgrind) ngx_log_error (ngx_log_al
ERT, log, Ngx_errno, "Sigaction (%s) failed, ignored", sig->signame); #else ngx_log_error (Ngx_log_emerg, log, Ngx_errno, "Sigaction (%s) failed", Sig->s
Igname);
return ngx_error;
#endif} return NGX_OK;
} void Ngx_signal_handler (int signo) {char *action;
ngx_int_t ignore;
ngx_err_t err;
ngx_signal_t *sig;
Ignore = 0;
err = Ngx_errno; for (sig = signals; Sig->signo!= 0; sig++) {if (Sig->signo = = Signo) {/* cyclic traversal signal/break;
} ngx_time_sigsafe_update ();
action = ""; Switch (ngx_process) {case Ngx_process_master:case Ngx_process_single:switch (signo) {case n
Gx_signal_value (ngx_shutdown_signal): Ngx_quit = 1;
Action = ", shutting down";
Break
Case Ngx_signal_value (ngx_terminate_signal): Case sigint:ngx_terminate = 1;
Action = ", exiting";
Break
Case Ngx_signal_value (ngx_noaccept_signal): if (ngx_daemonized) {ngx_noaccept = 1;
Action = ", stop accepting connections";
} break;
Case Ngx_signal_value (ngx_reconfigure_signal): ngx_reconfigure = 1;
Action = ", reconfiguring";
Break
Case Ngx_signal_value (ngx_reopen_signal): Ngx_reopen = 1; Action = ", reopening logs";
Break
Case Ngx_signal_value (ngx_changebin_signal): if (Getppid () > 1 | | ngx_new_binary > 0) { * * Ignore the signal in the new binary if it parent is * not the init process, i.e. The old binary ' s process * is still running.
Or ignore the signal in the old binary ' s * process if the new binary ' s process is already running.
* action = ", ignoring";
Ignore = 1;
Break
} ngx_change_binary = 1;
Action = ", changing binary";
Break
Case SIGALRM:NGX_SIGALRM = 1;
Break
Case Sigio:ngx_sigio = 1;
Break
Case sigchld:ngx_reap = 1;
Break
} break;
Case Ngx_process_worker:case Ngx_process_helper: Switch (signo) {case Ngx_signal_value (ngx_noaccept_signal): if (!ngx_daemonized) {
Break
} ngx_debug_quit = 1;
Case Ngx_signal_value (ngx_shutdown_signal): Ngx_quit = 1;
Action = ", shutting down";
Break
Case Ngx_signal_value (ngx_terminate_signal): Case sigint:ngx_terminate = 1;
Action = ", exiting";
Break
Case Ngx_signal_value (ngx_reopen_signal): Ngx_reopen = 1;
Action = ", reopening logs";
Break
Case Ngx_signal_value (ngx_reconfigure_signal): Case Ngx_signal_value (ngx_changebin_signal): Case Sigio:
Action = ", ignoring";
Break
} break; } ngx_log_error (Ngx_log_notice, Ngx_cycle->log, 0, "signal%d (%s) received%s", Signo, Sig->s
Igname, action); if (ignore) {ngx_lOg_error (Ngx_log_crit, Ngx_cycle->log, 0, "The changing binary signal is ignored:"
"You should shutdown or terminate" "before either old or new binary ' s process");
} if (Signo = = sigchld) {ngx_process_get_status ();
} ngx_set_errno (ERR);
}