October 26th Monday

  I got the logic of the function ngx_master_process_cycle().  Many a variables is checked in the function ngx_master_process_cycle() is according to the signals which is received.

 

  signal                                                                                                     status variable             action

  SIGALRM                                                                                              ngx_sigalrm                  delay, suspend and update time.

  SIGCHLD                                                                                               ngx_reap                       reap children processes

  ngx_signal_value(NGX_SHUTDOWN_SIGNAL)                           ngx_quit                        exit

  ngx_signal_value(NGX_TERMINATE_SIGNAL)                           ngx_terminate             exit

  ngx_signal_value(NGX_RECONFIGURE_SIGNAL)                      ngx_reconfigure           get the configure from the configure file again.

  ngx_signal_value(NGX_REOPEN_SIGNAL)                                  ngx_reopen                    open the log file again.

  ngx_signal_value(NGX_NOACCEPT_SIGNAL)                              ngx_noaccept               ngx_noaccepting = 1;

                                                                                                                                                           send ngx_signal_value(NGX_SHUTDOWN_SIGNAL);

  ngx_signal_value(NGX_CHANGEBIN_SIGNAL)                           ngx_change_binary     exec a new binary

 

 

 

 

1.  ngx_master_process_cycle() 函数,这个函数会启动工作进程干活,并且会处理信号量,处理的过程中会杀死或者创建新的进程。

a)       阻塞所有nginx关心的信号;

b)       设置进程的title(有点类似命令行形式的一个字符串,应该不是很重要。);

c)       启动一个work process进程;

d)       启动一个缓冲管理进程;

e)       初始化几个标志:ngx_new_binary = 0; delay = 0; live = 1;

f)        后面一个循环对不同的状态进行不同处理,而那些状态多数是进程收到的不同信号。下面是各个处理的详解:

a)         delay不为0,如果收到SIGALRM信号ngx_sigalrm设为1,将delay时间乘以2;最后设置一个实时类型的计时器;

b)         挂起当前进程,等到有信号,就会从挂起状态退出,继续执行;

c)         退出挂起状态后,根据操作系统时间重新更新当前时间;

d)         ngx_reap1(收到SIGCHLD信号),调用ngx_reap_children()回收子进程;

e)         如果子进程退出了(!live)且当前进程收到ngx_signal_value(NGX_SHUTDOWN_SIGNAL)ngx_signal_value(NGX_TERMINATE_SIGNAL)信号,本进程进行退出处理(ngx_master_process_exit());退出处理先删除pid文件,然后将调用所有模块的进程退出钩子,销毁内存池对象;continue

f)          如果ngx_terminate1delay0,就设成50;如果delay>1000,向work进程发送SIGKILL信号,否则向work进程发送ngx_signal_value(NGX_TERMINATE_SIGNAL)信号;

g)         如果ngx_quit1,向work进程发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号,然后将所有全局listening中的socket全关闭;continue

h)         如果ngx_reconfigure1(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_binary1(表示是新启动的一个进程),启动work进程,启动缓冲管理进程,然后将ngx_noaccepting设为0continue

j)           如果ngx_restart1,启动work进程,启动缓冲管理进程,live设为1

k)         如果ngx_reopen1(ngx_signal_value(NGX_REOPEN_SIGNAL)信号对应),则重新找开log文件,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_REOPEN_SIGNAL)信号;

l)           如果ngx_change_binary1(ngx_signal_value(NGX_CHANGEBIN_SIGNAL)信号对应),调用ngx_exec_new_binary()执行新进程;

m)  如果ngx_noaccept1(ngx_signal_value(NGX_NOACCEPT_SIGNAL)对应),设ngx_noaccepting1,调用ngx_signal_worker_processes()发送ngx_signal_value(NGX_SHUTDOWN_SIGNAL)信号。

你可能感兴趣的:(October 26th Monday)