国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-05-05 00:10:27
字體:
供稿:網(wǎng)友

具體代碼如下所示:

<?phpclass 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 -auxUSER    PID %CPU %MEM  VSZ  RSS TTY   STAT START  TIME COMMANDroot     1 0.0 0.0 18200 3076 pts/0  Ss+ 14:05  0:00 bashroot     6 0.0 0.0 18208 3252 pts/1  Ss  14:06  0:00 bashroot    19 0.0 0.0 18204 3248 pts/2  Ss+ 14:11  0:00 bashroot    64 0.0 0.2 348488 8320 ?    Ss  15:32  0:00 master_processroot    65 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_processroot    66 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_processroot    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)程會(huì)再自動(dòng)開啟一個(gè)子進(jìn)程

USER    PID %CPU %MEM  VSZ  RSS TTY   STAT START  TIME COMMANDroot     1 0.0 0.0 18200 3076 pts/0  Ss+ 14:05  0:00 bashroot     6 0.0 0.0 18208 3252 pts/1  Ss  14:06  0:00 bashroot    19 0.0 0.0 18204 3248 pts/2  Ss+ 14:11  0:00 bashroot    64 0.0 0.2 348488 8320 ?    Ss  15:32  0:00 master_processroot    66 0.0 0.2 348488 8400 ?    Ss  15:32  0:00 worker_processroot    68 0.0 0.1 348488 5796 ?    Ss  15:34  0:00 worker_processroot    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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 三门县| 镇康县| 辽中县| 平谷区| 英山县| 安岳县| 庆安县| 宁陵县| 葫芦岛市| 蒲城县| 安多县| 晋中市| 武陟县| 五华县| 县级市| 银川市| 宽城| 海南省| 红河县| 虞城县| 景东| 泸溪县| 上虞市| 乌兰察布市| 新兴县| 嘉祥县| 阜新市| 合阳县| 舟曲县| 金坛市| 泾源县| 东平县| 聂荣县| 六盘水市| 泰和县| 双鸭山市| 荥经县| 龙口市| 万源市| 保德县| 万载县|