具体代码如下所示:
<?phpclass worker{ public static $count = 2; public static function runall(){ static::runmaster(); static::moniprocess(); } //开启主进程 public static function runmaster(){ //确保进程有最大操作权限 unmask(0); $pid = pcntl_fork();her什么意思 if($pid 名著小说推荐> 0){ echo "主进程进程 $pid \n"; exit; }el if($pid == 0){ if(-1 === posix_tsid()){ throw new exception("tsid fail"); } for ($i=0; $i < s快乐作文网elf::$count; $i++) { static::runworker(); } @cli_t_process_title("master_process"); }el{ throw new exception("创建主进程失败"); } } //开启子进程 public static function runworker(){ unmask(0); $pid = pcntl_fork(); if($pid > 0){ // echo "创建子进程 $pid \n"; }el if($pid == 0){ if(-1 === posix_tsid()){ throw new exception("tsid fail"); } @cli_t_process_title("worker_process"); while(1){ sleep(1); } }el{ throw new exception("创建子进程失败"); } } //监控worker进程 public function moniprocess(){ while( $pid = pcntl_wait($status)){ if($pid == -1){ break; }el{ static::runworker(); } } }}worker::runall();
ps -auxur 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_processroo绵阳职业技术学校t 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
执行命令 kill 65,杀死进程 65 则master_process 进程会再自动开启一个子进程
ur 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
总结
以上所述是www.887551.com给大家介绍的php实现 master-worker 守护多进程模式的实例代码,希望对大家有所帮助
本文发布于:2023-04-07 14:34:15,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5b8f2c76b62758597befe478af612e82.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php实现 master.doc
本文 PDF 下载地址:php实现 master.pdf
留言与评论(共有 0 条评论) |