1. 程式人生 > >lighttpd1.4.16 主程式邏輯 (一)觀察者和工作者

lighttpd1.4.16 主程式邏輯 (一)觀察者和工作者

一、觀察者和工作者

對程序進行管理。父程序作為觀察者,負責啟動和監聽工作者程序

程式檔案:server.c

行:970~996

原始碼:

程式碼1

  1. /* start watcher and workers */
  2.     num_childs = srv->srvconf.max_worker;
  3. if (num_childs > 0) {
  4. int child = 0;
  5. while (!child && !srv_shutdown) {
  6. if (num_childs > 0) {
  7. switch (fork()) {
  8. case -1:
  9. return -1;
  10. case 0:
  11.                     child = 1;
  12. break;
  13. default:
  14.                     num_childs--;
  15. break;
  16.                 }
  17.             } else {
  18. int status;
  19. /* ignore EINTR */
  20. if (-1 != wait(&status)) num_childs++;
  21.             }
  22.         }
  23. if (srv_shutdown) {
  24.             kill(0, SIGTERM);
  25.         }
  26. if (!child) return
     0;
  27.     }

抽取解析

以下程式碼1-1將程式碼1抽取出來

程式碼1-1

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. /**
  6.  * 程序管理
  7.  * 程序的開啟和程序的維護,包括關閉
  8.  *
  9.  * */
  10. int main(int argc,char** argv)
  11. {
  12. //是否子程序的標誌
  13. int is_child=0;
  14. //服務退出標誌
  15. int is_server_close=0;
  16. int ppid=0;
  17. int cid=0;
  18. //最大程序數
  19. int max_child_count=5;
  20. //is_child=0用來判斷是否父程序,只有父程序才能進入此迴圈
  21. while(0==is_child && 0==is_server_close)//父程序執行此程式碼
  22.         {
  23. //是否還有程序要建立
  24. if(max_child_count>0)
  25.                 {
  26.                         cid=fork();
  27. switch(cid)
  28.                         {
  29. //錯誤
  30. case -1:
  31.                                         {
  32.                                                 printf("fork process=%d error/n",max_child_count);
  33. return -1;
  34.                                         }
  35. //子程序
  36. case 0:
  37.                                         {
  38.                                                 is_child=1;
  39. break;
  40.                                         }
  41. //父程序
  42. default:
  43.                                         {
  44. //建立子程序成功
  45. //程序數減少
  46.                                                 --max_child_count;
  47.                                                 printf("sub process id =%d/n",cid);
  48.                                                 printf("parent printf is_child value:%d/n",is_child);
  49. break;
  50.                                         }
  51.                         }
  52. //所有程序都建立完畢
  53.                 }else
  54.                 {
  55. int status;
  56. //開始阻塞等待子程序是否結束
  57. if(-1!=wait(&status))
  58.                         {
  59. //結束,將程序數累加1,使迴圈可以重新啟動新的程序
  60.                                 max_child_count++;
  61.                         }
  62.                 }
  63.         }
  64. //父程序結束維護子程序後的處理
  65. if(is_child==0)
  66.         {
  67.                 printf("parent end/n");
  68. return 0;
  69. //子程序業務處理
  70.         }else{
  71.                 sleep(5);
  72.                 printf("child id=%d end/n",cid);
  73. return 0;
  74.         }
  75. }