最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實(shí)現(xiàn) master-worker 守護(hù)多進(jìn)程模式的實(shí)例代碼

 更新時間:2019年07月20日 09:47:25   作者:碼緣  
這篇文章主要介紹了php實(shí)現(xiàn) master-worker 守護(hù)多進(jìn)程模式的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

具體代碼如下所示:

<?php
class Worker{
  public static $count = 2;
  public static function runAll(){
    static::runMaster();
    static::moniProcess();
  }
  //開啟主進(jìn)程
  public static function runMaster(){
    //確保進(jìn)程有最大操作權(quán)限
    unmask(0);
    $pid = pcntl_fork();
    if($pid > 0){
      echo "主進(jìn)程進(jìn)程 $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)建主進(jìn)程失敗");
    }
  } 
  //開啟子進(jìn)程
  public static function runWorker(){
    unmask(0);
    $pid = pcntl_fork();
    if($pid > 0){
      // echo "創(chuàng)建子進(jìn)程 $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)建子進(jìn)程失敗");
    }
  }
  //監(jiān)控worker進(jìn)程
  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,殺死進(jìn)程 65 則master_process 進(jìn)程會再自動開啟一個子進(jìn)程

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

總結(jié)

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

相關(guān)文章

最新評論

庆城县| 育儿| 资源县| 隆林| 宁城县| 平阴县| 贡嘎县| 五原县| 台南市| 托里县| 张家川| 永安市| 太康县| 绍兴市| 泽州县| 开江县| 平阴县| 广水市| 河曲县| 建水县| 莱芜市| 甘肃省| 信丰县| 西宁市| 东明县| 铁力市| 衡阳县| 中超| 交口县| 宝丰县| 仙桃市| 邹城市| 北票市| 阿拉善右旗| 精河县| 行唐县| 南城县| 太仆寺旗| 龙南县| 万年县| 开阳县|