Nginx源码分析-进程管理之worker进程

转自 http://blog.csdn.net/marcky/article/details/6013502

本文着手分析一下worker进程的情况。首先找到worker进程的入口地方——ngx_worker_process_cycle。这个函数不光是worker进程的入口函数,同时也是worker进程循环工作的主体函数,看函数名含有一个cycle嘛。进入这个cycle函数,第一件事就是调用ngx_worker_process_init(cycle, 1);对worker进程进行初始化操作。先看看这个worker进程的初始化过程。

 


 

 [cpp] view plaincopyprint?

  1. ngx_process = NGX_PROCESS_WORKER;  
  2. if (ngx_set_environment(cycle, NULL) == NULL) {  
  3.     /* fatal */  
  4.     exit(2);  
  5. }  

ngx_process = NGX_PROCESS_WORKER; if (ngx_set_environment(cycle, NULL) == NULL) { /* fatal */ exit(2); }

 进入初始化就将全局变量ngx_process设置为worker进程的标志,由于这个变量是从master进程复制过来的,所以没设置前就是master进程的标志。然后设置相应的环境变量。接下去就是设置了一些列的资源限制,id等玩意,这里就忽略代码了。

 


 

  1. for (i = 0; ngx_modules[i]; i++) {  
  2.     if (ngx_modules[i]->init_process) {  
  3.         if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {  
  4.             /* fatal */  
  5.             exit(2);  
  6.         }  
  7.     }  
  8. }  

for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->init_process) { if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) { /* fatal */ exit(2); } } }

 此处循环调用每个模块的init_process,完成每个模块自定义的进程初始化操作,一般在模块定义的时候设置这个回调指针的值,即注册一个函数给它。做模块开发的时候,貌似使用得挺少的,遇到的时候好好关注下。


  1.      
  2.    /* 
  3.    此处循环用于关闭其他worker进程的无用channel资源 
  4.    */  
  5.    for (n = 0; n < ngx_last_process; n++) {  
  6. /* 
  7. ngx_processes数组中n位置的进程不存在。 
  8. */    
  9.        if (ngx_processes[n].pid == -1) {  
  10.            continue;  
  11.        }  
  12. /* 
  13. 全局变量ngx_process_slot的值是创建worker进程的时候,从 
  14. master进程复制过来的,所以此处ngx_process_slot就指本worker 
  15. 进程在ngx_process_slot数组中的索引位置。此处不处理本worker 
  16. 进程,所以跳过。 
  17. */  
  18.        if (n == ngx_process_slot) {  
  19.            continue;  
  20.        }  
  21. /* 
  22. channel不存在,继续跳过。 
  23. */  
  24.        if (ngx_processes[n].channel[1] == -1) {  
  25.            continue;  
  26.        }  
  27. /* 
  28. ngx_processes数组中存储的是每个worker进程的资源,是master进程负责创建的。 
  29. 因此创建一个worker进程的时候,就一同将这些资源复制过来了,所以此处就关闭 
  30. 无用的channel——其他worker进程的读端文件描述符,保留写端文件描述符做 
  31. worker间的通信之用。 
  32. */  
  33.        if (close(ngx_processes[n].channel[1]) == -1) {  
  34.            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,  
  35.                          "close() channel failed");  
  36.        }  
  37.    }  
  38. /* 
  39. 关闭本worker进程channel的写端文件描述符,因为每个worker进程只从自己的channel 
  40. 上读,而不会写。写自己channel的是master和其他worker进程。这也是上面为什么要 
  41. 关闭其他worker进程channel的读端。 
  42. */  
  43.    if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) {  
  44.        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,  
  45.                      "close() channel failed");  
  46.    }  

/* 此处循环用于关闭其他worker进程的无用channel资源 */ for (n = 0; n < ngx_last_process; n++) { /* ngx_processes数组中n位置的进程不存在。 */ if (ngx_processes[n].pid == -1) { continue; } /* 全局变量ngx_process_slot的值是创建worker进程的时候,从 master进程复制过来的,所以此处ngx_process_slot就指本worker 进程在ngx_process_slot数组中的索引位置。此处不处理本worker 进程,所以跳过。 */ if (n == ngx_process_slot) { continue; } /* channel不存在,继续跳过。 */ if (ngx_processes[n].channel[1] == -1) { continue; } /* ngx_processes数组中存储的是每个worker进程的资源,是master进程负责创建的。 因此创建一个worker进程的时候,就一同将这些资源复制过来了,所以此处就关闭 无用的channel——其他worker进程的读端文件描述符,保留写端文件描述符做 worker间的通信之用。 */ if (close(ngx_processes[n].channel[1]) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "close() channel failed"); } } /* 关闭本worker进程channel的写端文件描述符,因为每个worker进程只从自己的channel 上读,而不会写。写自己channel的是master和其他worker进程。这也是上面为什么要 关闭其他worker进程channel的读端。 */ if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "close() channel failed"); }

 针对这段代码采用直接注释代码的方式进行分析,感觉挺自然的,还不错,以后碰到比较长的代码段都采用这种方式进行了。


  1. if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,  
  2.                           ngx_channel_handler)  
  3.     == NGX_ERROR)  
  4. {  
  5.     /* fatal */  
  6.     exit(2);  
  7. }  

if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT, ngx_channel_handler) == NGX_ERROR) { /* fatal */ exit(2); }

 ngx_channel就是worker进程channel的读端,这里调用ngx_add_channel_event将channel放入Nginx关心的集合中,同时关注起这个channel上的读事件,也即这个channel上有数据到来后,就立马采取读channel操作。此处的添加一个channel的读事件是worker进程初始化的关键之处。到此,初始化过程就结束了,回到worker循环主体看看吧。


 

  1.     for ( ;; ) {  
  2. /* 
  3. ngx_exiting是在worker进程收到SIGQUIT信号后设置的,稍后就能看到庐山真面目了。 
  4. */  
  5.        if (ngx_exiting) {  
  6.            c = cycle->connections;  
  7.     /* 
  8.     worker进程退出前,先得处理完每个connection上已经发生的事件。 
  9.     */  
  10.            for (i = 0; i < cycle->connection_n; i++) {  
  11.                /* THREAD: lock */  
  12.                if (c[i].fd != -1 && c[i].idle) {  
  13.                    c[i].close = 1;  
  14.                    c[i].read->handler(c[i].read);  
  15.                }  
  16.            }  
  17.           
  18.     /* 
  19.     处理完所有事件后,调用ngx_worker_process_exit函数,worker进程退出。 
  20.     */  
  21.            if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)  
  22.            {  
  23.                ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");  
  24.                ngx_worker_process_exit(cycle);  
  25.            }  
  26.        }  
  27.        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");  
  28. /* 
  29. 这里是worker进程处理事件的核心开始。也即是,worker进程从里开始做一些特定的事情了, 
  30. 我们完全可以修改此处的代码,让Nginx为我们做一些其他的事情,呵呵。 
  31. */  
  32.        ngx_process_events_and_timers(cycle);  
  33. /* 
  34. worker进程收到了SIGINT信号,退出。 
  35. */  
  36.        if (ngx_terminate) {  
  37.            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");  
  38.            ngx_worker_process_exit(cycle);  
  39.        }  
  40.       
  41. /* 
  42. worker进程收到了SIGQUIT信号,如果此时worker进程不是出于exiting状态, 
  43. 就将设置ngx_exiting为1,让其进入exiting状态;同时关闭监听套接口。 
  44. */  
  45.        if (ngx_quit) {  
  46.            ngx_quit = 0;  
  47.            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,  
  48.                          "gracefully shutting down");  
  49.            ngx_setproctitle("worker process is shutting down");  
  50.            if (!ngx_exiting) {  
  51.                ngx_close_listening_sockets(cycle);  
  52.                ngx_exiting = 1;  
  53.            }  
  54.        }  
  55. /* 
  56. worker进程收到了SIGUSR1信号 
  57. */  
  58.        if (ngx_reopen) {  
  59.            ngx_reopen = 0;  
  60.            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");  
  61.            ngx_reopen_files(cycle, -1);  
  62.        }  
  63.    }  

for ( ;; ) { /* ngx_exiting是在worker进程收到SIGQUIT信号后设置的,稍后就能看到庐山真面目了。 */ if (ngx_exiting) { c = cycle->connections; /* worker进程退出前,先得处理完每个connection上已经发生的事件。 */ for (i = 0; i < cycle->connection_n; i++) { /* THREAD: lock */ if (c[i].fd != -1 && c[i].idle) { c[i].close = 1; c[i].read->handler(c[i].read); } } /* 处理完所有事件后,调用ngx_worker_process_exit函数,worker进程退出。 */ if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel) { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); ngx_worker_process_exit(cycle); } } ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle"); /* 这里是worker进程处理事件的核心开始。也即是,worker进程从里开始做一些特定的事情了, 我们完全可以修改此处的代码,让Nginx为我们做一些其他的事情,呵呵。 */ ngx_process_events_and_timers(cycle); /* worker进程收到了SIGINT信号,退出。 */ if (ngx_terminate) { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); ngx_worker_process_exit(cycle); } /* worker进程收到了SIGQUIT信号,如果此时worker进程不是出于exiting状态, 就将设置ngx_exiting为1,让其进入exiting状态;同时关闭监听套接口。 */ if (ngx_quit) { ngx_quit = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "gracefully shutting down"); ngx_setproctitle("worker process is shutting down"); if (!ngx_exiting) { ngx_close_listening_sockets(cycle); ngx_exiting = 1; } } /* worker进程收到了SIGUSR1信号 */ if (ngx_reopen) { ngx_reopen = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs"); ngx_reopen_files(cycle, -1); } }

 通过上述分析发现worker进程的cycle比master简单不少啊,到此,worker进程的大体框架就差不多了。

转载于:https://www.cnblogs.com/li-hao/archive/2013/03/06/2945604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值