总体情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ngx_master_process_cycle() ngx_setproctitle() ngx_start_worker_processes() for (i = 0; i < threadnums; i++) ngx_spawn_process(i,"worker process"); pid = fork(); ngx_worker_process_cycle(inum,pprocname); ngx_worker_process_init(); sigemptyset(&set); sigprocmask(SIG_SETMASK, &set, NULL); Create(threadnums) ngx_epoll_init(); ngx_setproctitle(pprocname); for ( ;; ) { ngx_process_events_and_timers(); }. .... StopAll(); sigemptyset(&set); for ( ;; ) {}.
|
父进程
master进程创建work进程的流程图如上所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| void ngx_master_process_cycle() { sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD); sigaddset(&set, SIGALRM); sigaddset(&set, SIGIO); sigaddset(&set, SIGINT); sigaddset(&set, SIGHUP); sigaddset(&set, SIGUSR1); sigaddset(&set, SIGUSR2); sigaddset(&set, SIGWINCH); sigaddset(&set, SIGTERM); sigaddset(&set, SIGQUIT); if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { ngx_log_error_core(NGX_LOG_ALERT,errno,"ngx_master_process_cycle()中sigprocmask()失败!"); }
size_t size; int i; size = sizeof(master_process); size += g_argvneedmem; if(size < 1000) { char title[1000] = {0}; strcpy(title,(const char *)master_process); strcat(title," "); for (i = 0; i < g_os_argc; i++) { strcat(title,g_os_argv[i]); } ngx_setproctitle(title); ngx_log_error_core(NGX_LOG_NOTICE,0,"%s %P 【master进程】启动并开始运行......!",title,ngx_pid); } CConfig *p_config = CConfig::GetInstance(); int workprocess = p_config->GetIntDefault("WorkerProcesses",1); ngx_start_worker_processes(workprocess);
sigemptyset(&set); for ( ;; ) {
sigsuspend(&set);
sleep(1);
} return; }
|
一些细节:
读取配置信息是用的一个单例类
子进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
static void ngx_worker_process_cycle(int inum,const char *pprocname) { ngx_process = NGX_PROCESS_WORKER;
ngx_worker_process_init(inum); ngx_setproctitle(pprocname); ngx_log_error_core(NGX_LOG_NOTICE,0,"%s %P 【worker进程】启动并开始运行......!",pprocname,ngx_pid); for(;;) { ngx_process_events_and_timers();
}
g_threadpool.StopAll(); g_socket.Shutdown_subproc(); return; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| static void ngx_worker_process_init(int inum) { sigset_t set;
sigemptyset(&set); if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { ngx_log_error_core(NGX_LOG_ALERT,errno,"ngx_worker_process_init()中sigprocmask()失败!"); }
CConfig *p_config = CConfig::GetInstance(); int tmpthreadnums = p_config->GetIntDefault("ProcMsgRecvWorkThreadCount",5); if(g_threadpool.Create(tmpthreadnums) == false) { exit(-2); } sleep(1);
if(g_socket.Initialize_subproc() == false) { exit(-2); } g_socket.ngx_epoll_init(); return; }
|