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
   | static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, ngx_int_t type) {
      for (i = 0; i < n; i++) { 		//ngx_worker_process_cycle 是每个work进程执行的函数         ngx_spawn_process(cycle, ngx_worker_process_cycle, (void *) (intptr_t) i, "worker process", type);     } } static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) {     ngx_int_t worker = (intptr_t) data;
      ngx_uint_t         i;     ngx_connection_t  *c;
      ngx_process = NGX_PROCESS_WORKER;     ngx_worker_process_init(cycle, worker);     ngx_setproctitle("worker process");     //忽略了其他的代码     for ( ;; ) {
          if (ngx_exiting) {
              c = cycle->connections;
              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_pid_t ngx_spawn_process(ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data,char *name, ngx_int_t respawn) { 	//     pid = fork();
      switch (pid) {
      case -1:         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,                       "fork() failed while spawning \"%s\"", name);         ngx_close_channel(ngx_processes[s].channel, cycle->log);         return NGX_INVALID_PID;
      case 0:         ngx_pid = ngx_getpid();         proc(cycle, data);         break;
      default:         break;     }
  }
   |