精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明出处:
锐英源,www.wisestudy.cn,孙老师作品,联系电话13803810136。
make_child(slot) { [...] pid = fork();//分配 if ( pid == 0 ) { /* Child Process: worker */ scoreboard[slot].status = STARTING; child_main(slot);//子进程执行 exit(0); } [...] scoreboard[i].pid = pid;//修改记分牌 }//演示对子进程管理
manage_worker_pool() { [...] scoreboard = create_scoreboard();//创建记分牌 /* create workers 预分配一些子进程*/ for (i = 0 ; i < to_start ; ++i ) { make_child(i); }//修改没有和实际进程对应的记分牌条目状态为死亡状态
for (i = to_start ; i < limit ; ++i ) { scoreboard[i].status = DEAD; } /* Control Loop 控制循环*/ while(!shutdown) { /* wait for termination signal or let some time pass 等待结束信号或者让时间过去*/ pid = wait_or_timeout(); if ( pid != 0 ) { /* replace dead worker 替换死去的子进程*/ slot = find_child_by_pid(pid); make_child(slot); } else { /* check number of workers 检查子进程数目*/ 16 idle_count = 0; for (i = 0 ; i < limit ; ++i ) { if (scoreboard[i].status == IDLE) { idle_count++; to_kill = i; } if (scoreboard[i].status == DEAD) free_slot = i; } //如果空闲的不够,则分配 if (idle_count < min_idle) { make_child(free_slot); } //如果空闲的多了,则用信号释放 if (idle_count > max_idle) { kill(scoreboard[to_kill].pid); scoreboard[to_kill].status = DEAD; } } /* while(!shutdown) */ /* Terminate server 结束时,把所有子进程都释放了*/ for (i = 0 ; i < limit ; ++i ) { if (scoreboard[i].status != DEAD) kill(scoreboard[i].pid); } }