前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【多进程】php实现 master-worker 守护多进程模式

【多进程】php实现 master-worker 守护多进程模式

作者头像
码缘
发布2019-07-23 14:22:44
3900
发布2019-07-23 14:22:44
举报
文章被收录于专栏:PHP修行之路PHP修行之路
代码语言:javascript
复制
<?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("创建主进程失败");
        }
    } 

    //开启子进程
    public static function runWorker(){
        unmask(0);
        $pid = pcntl_fork();
        if($pid > 0){
            // echo "创建子进程 $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("创建子进程失败");
        }
    }

    //监控worker进程
    public function moniProcess(){
        while( $pid = pcntl_wait($status)){
            if($pid == -1){
                break;
            }else{
                static::runWorker();
            }
        }
    }
}

Worker::runAll();
代码语言:javascript
复制
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

执行命令 kill 65,杀死进程 65 则master_process 进程会再自动开启一个子进程

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档