濮阳杆衣贸易有限公司

主頁 > 知識庫 > php實現(xiàn) master-worker 守護多進程模式的實例代碼

php實現(xiàn) master-worker 守護多進程模式的實例代碼

熱門標簽:高德地圖標注家 廣東地市地圖標注 長春人工外呼系統(tǒng)服務商 怎么向銷售公司推銷外呼系統(tǒng) 哪里辦理400電話 廣州防封卡外呼系統(tǒng)多少錢一個月 仁和怎么申請400開頭的電話 江西手機自動外呼防封系統(tǒng)是什么 外呼系統(tǒng)撥打暫時無法接通

具體代碼如下所示:

?php
class Worker{
  public static $count = 2;
  public static function runAll(){
    static::runMaster();
    static::moniProcess();
  }
  //開啟主進程
  public static function runMaster(){
    //確保進程有最大操作權限
    unmask(0);
    $pid = pcntl_fork();
    if($pid > 0){
      echo "主進程進程 $pid \n";
      exit;  
    }else if($pid == 0){
      if(-1 === posix_setsid()){
          throw new Exception("setsid fail");
      }
      for ($i=0; $i  self::$count; $i++) {
        static::runWorker();
      }
      @cli_set_process_title("master_process");
    }else{
      throw new Exception("創(chuàng)建主進程失敗");
    }
  } 
  //開啟子進程
  public static function runWorker(){
    unmask(0);
    $pid = pcntl_fork();
    if($pid > 0){
      // echo "創(chuàng)建子進程 $pid \n";
    }else if($pid == 0){
      if(-1 === posix_setsid()){
        throw new Exception("setsid fail");
      }
      @cli_set_process_title("worker_process");
      while(1){
        sleep(1);
      }
    }else{
      throw new Exception("創(chuàng)建子進程失敗");
    }
  }
  //監(jiān)控worker進程
  public function moniProcess(){
    while( $pid = pcntl_wait($status)){
      if($pid == -1){
        break;
      }else{
        static::runWorker();
      }
    }
  }
}
Worker::runAll();
ps -aux
USER    PID %CPU %MEM  VSZ  RSS TTY   STAT START  TIME COMMAND
root     1 0.0 0.0 18200 3076 pts/0  Ss+ 14:05  0:00 bash
root     6 0.0 0.0 18208 3252 pts/1  Ss  14:06  0:00 bash
root    19 0.0 0.0 18204 3248 pts/2  Ss+ 14:11  0:00 bash
root    64 0.0 0.2 348488 8320 ?    Ss  15:32  0:00 master_process
root    65 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_process
root    66 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_process
root    67 0.0 0.0 36640 2804 pts/1  R+  15:32  0:00 ps -aux

執(zhí)行命令 kill 65,殺死進程 65 則master_process 進程會再自動開啟一個子進程

USER    PID %CPU %MEM  VSZ  RSS TTY   STAT START  TIME COMMAND
root     1 0.0 0.0 18200 3076 pts/0  Ss+ 14:05  0:00 bash
root     6 0.0 0.0 18208 3252 pts/1  Ss  14:06  0:00 bash
root    19 0.0 0.0 18204 3248 pts/2  Ss+ 14:11  0:00 bash
root    64 0.0 0.2 348488 8320 ?    Ss  15:32  0:00 master_process
root    66 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_process
root    68 0.0 0.1 348488 5796 ?    Ss  15:34  0:00 worker_process
root    69 0.0 0.0 36640 2728 pts/1  R+  15:34  0:00 ps -aux

總結

以上所述是小編給大家介紹的php實現(xiàn) master-worker 守護多進程模式的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

您可能感興趣的文章:
  • PHP守護進程化在C和PHP環(huán)境下的實現(xiàn)
  • PHP守護進程的兩種常見實現(xiàn)方式詳解
  • php腳本守護進程原理與實現(xiàn)方法詳解
  • PHP程序員玩轉Linux系列 使用supervisor實現(xiàn)守護進程
  • PHP高級編程實例:編寫守護進程
  • shell腳本作為保證PHP腳本不掛掉的守護進程實例分享
  • php守護進程 加linux命令nohup實現(xiàn)任務每秒執(zhí)行一次
  • PHP程序守護進程化實現(xiàn)方法詳解

標簽:湘西 海北 梅河口 惠州 濮陽 黔東 文山 廈門

巨人網(wǎng)絡通訊聲明:本文標題《php實現(xiàn) master-worker 守護多進程模式的實例代碼》,本文關鍵詞  php,實現(xiàn),master-worker,守護,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php實現(xiàn) master-worker 守護多進程模式的實例代碼》相關的同類信息!
  • 本頁收集關于php實現(xiàn) master-worker 守護多進程模式的實例代碼的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    望都县| 津南区| 罗定市| 忻州市| 遂昌县| 翁源县| 乐亭县| 青海省| 凌源市| 高青县| 洛浦县| 阿勒泰市| 南京市| 清镇市| 泰顺县| 丰宁| 拉萨市| 凌云县| 永吉县| 昔阳县| 女性| 佛教| 田阳县| 阜阳市| 会理县| 安福县| 茌平县| 贺兰县| 和田县| 尉氏县| 吕梁市| 綦江县| 镇坪县| 辽阳市| 诏安县| 伊春市| 成武县| 富宁县| 策勒县| 永和县| 宜宾市|