了解core模块之前还应改学习ngx_start_worker_processes函数,今天我就来详细学一下这个方法,主要内容来自于http://blog.sina.com.cn/s/blog_677be95b0100iivk.html。
nginx的进程启动过程是在ngx_master_process_cycle(src/os/unix/ngx_process_cycle.c)中完成的(单进程是通过ngx_single_process_cycle完成,这里只分析多进程的情况),在ngx_master_process_cycle中,会根据配置文件的worker_processes值创建多个子进程,即一个master进程和多个worker进程。进程之间、进程与外部之间保持通信,进程之间是通过socketpair进行通信的,socketpair是一对全双工的无名socket,可以当作管道使用,和管道不同的是,每条socket既可以读也可以写,而管道只能用于写或者用于读;进程与外部之间是通过信号通信的。
master进程主要进行一些全局性的初始化工作和管理worker的工作;事件处理是在worker中进行的。
进程启动的过程中,有一些重要的全局数据会被设置,最重要的是进程表ngx_processes,master每创建一个worker都会把一个设置好的ngx_process_t结构变量放入ngx_processes中,进程表长度为1024,刚创建的进程存放在ngx_process_slot位置,ngx_last_process是进程表中最后一个存量进程的下一个位置,ngx_process_t是进程在nginx中的抽象:
- src/os/unix/ngx_process.h
- typedef void (*ngx_spawn_proc_pt) (ngx_cycle_t *cycle, void *data);
-
- typedef struct {
- ngx_pid_t pid;
- int status;
- ngx_socket_t channel[2];
-
- ngx_spawn_proc_pt proc;
- void *data;
- char *name;
-
- unsigned respawn:1;
- unsigned just_spawn:1;
- unsigned detached:1;
- unsigned exiting:1;
- unsigned exited:1;
- } ngx_process_t;
进程间通信是利用socketpair创建的一对socket进行的,通信中传输的是ngx_channel_t结构变量:
- typedef struct {
- ngx_uint_t command;
- ngx_pid_t pid;
- ngx_int_t slot;
- ngx_fd_t fd;
- } ngx_channel_t;(src/os/unix/ngx_channel.h)
-
-
-
-
-
-
-
-
-
进程的启动过程是比较重要的一个环节,为了把这个过程分析透彻,下面会多采用首先分析ngx_master_process_cycle函数,可以分解为以下各步骤:
1、master设置一些需要处理的信号,这些信号包括
SIGCHLD,SIGALRM,SIGIO,SIGINT,NGX_RECONFIGURE_SIGNAL(SIGHUP),NGX_REOPEN_SIGNAL(SIGUSR1),
NGX_NOACCEPT_SIGNAL(SIGWINCH),NGX_TERMINATE_SIGNAL(SIGTERM),NGX_SHUTDOWN_SIGNAL(SIGQUIT),
NGX_CHANGEBIN_SIGNAL(SIGUSR2);
- 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));
2、调用ngx_setproctilte设置进程标题,title = "master process" + ngx_argv[0] + ... + ngx_argv[ngx_argc-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、调用ngx_start_worker_processes(cycle, ccf->worker_processes, NGX_PROCESS_RESPAWN)启动worker进程;
- 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);
4、调用ngx_start_cache_manager_processes(cycle, 0)启动文件cache管理进程,有些模块需要文件cache,比如fastcgi模块,这些模块会把文件cache路径添加到cycle->paths中,文件cache管理进程会定期调用这些模块的文件cache处理钩子处理一下文件cache;
- ngx_start_cache_manager_processes(cycle, 0);
注释代码的方式分析。
5、master循环处理信号量。
- src/os/unix/ngx_process_cycle.c
- 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");
-
- sigsuspend(&set);
-
- 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);
- }
-
-
- if (ngx_terminate) {
- if (delay == 0) {
-
- delay = 50;
- }
-
- if (sigio) {
- sigio--;
- continue;
- }
-
- sigio = ccf->worker_processes + 2 ;
-
- 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;
- 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;
- }
-
- 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);
- live = 1;
- 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));
- }
- }
下面总结下,也希望各位同学,再看总结之前,已经阅读了(起码是看了一遍)上面的代码注释!!!
该总结来自于http://blog.csdn.net/lu_ming/article/details/5144427 非常感激作者无私奉献。
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) 后面一个循环对不同的状态进行不同处理,而那些状态多数是进程收到的不同信号。下面是各个处理的详解:
a) delay不为0,如果收到SIGALRM信号ngx_sigalrm设为1,将delay时间乘以2;最后设置一个实时类型的计时器;
b) 挂起当前进程,等到有信号,就会从挂起状态退出,继续执行;
c) 退出挂起状态后,根据操作系统时间重新更新当前时间;
d) ngx_reap为1(收到SIGCHLD信号,有worker退出(ngx_reap==1)),调用ngx_reap_children()回收子进程;
e) 如果子进程都退出了(!live)且当前进程收到ngx_signal_value(NGX_SHUTDOWN_SIGNAL)或ngx_signal_value(NGX_TERMINATE_SIGNAL)信号,本进程进行退出处理(ngx_master_process_exit());退出处理先删除pid文件,然后将调用所有模块的进程退出钩子,销毁内存池对象;
f) 如果ngx_terminate为1,delay为0,就设成50;如果delay>1000,向work进程发送SIGKILL信号,否则向work进程发送ngx_signal_value(NGX_TERMINATE_SIGNAL)信号;
g) 如果ngx_quit为1,向work进程发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号,然后将所有全局listening中的socket全关闭;continue;
h) 如果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)信号;
i) ngx_new_binary为1(表示是新启动的一个进程),启动work进程,启动缓冲管理进程,然后将ngx_noaccepting设为0;continue;
j) 如果ngx_restart为1(当ngx_noaccepting=1的时候会把ngx_restart设为1,重启worker),启动work进程,启动缓冲管理进程,live设为1;
k) 如果ngx_reopen为1(ngx_signal_value(NGX_REOPEN_SIGNAL)信号对应),则重新找开log文件,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_REOPEN_SIGNAL)信号;
l) 如果ngx_change_binary为1(ngx_signal_value(NGX_CHANGEBIN_SIGNAL)信号对应),调用ngx_exec_new_binary()执行新进程;
m) 如果ngx_noaccept为1(ngx_signal_value(NGX_NOACCEPT_SIGNAL)对应),设ngx_noaccepting为1,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号。