PHP 多进程与信号中断实现多任务常驻内存管理【Master/Worker 模型】

本文章基于 pcntl 扩展做的多进程测试。

进程调度策略

父子进程的调度由操作系统来负责,具体先调度子进程还是父进程由系统的调度算法决定,当然可以在父进程加上延时或是调用进程回收函数 pcntl_wait 可以先让子进程先运行,进程回收的目的是释放进程创建时占用的内存空间,防止变成僵尸进程。

信号:

信号称为软中断系统或是叫软中断,功能是向进程发送异步事件通知。

立即学习PHP免费学习笔记(深入)”;

信号编号: 【源码基于 SIGINT,SIGTERM,SIGUSR1 信号,含义请自行查看 kill 命令手册,不在描述】

linux 支持 64 个,有一半为实时信号,一半为非时实信号,这些信号都有自己的编号和对应的整数值。每个信号的编号含义读者可以参阅 linux 相关手册【man 手册看看就知道了】

信号处理函数:

信号一般会绑定相应的功能,有的是默认动作如 SIGKILL,SIGTERM,SIGINT 操作默认操作就是干掉进程,当然我们可以重写覆盖掉,就是通过 pcntl_signal 来覆盖掉。

信号的概念:与硬件中断一个道理,请读者自行参考本人前面撸过的文章或是查看芯片硬件中断原理。

信号的发送:

kill 信号编号 进程 或是按键产品的中断信号或是在源码里可以使用 posix_kill 等函数。

进程是相互隔离的,拥有自己的空间,除了一些公用的正文【代码区】,同时也有自己的可执行代码,进程运行时,将占用 cpu 的资源,其它进程将无权运行,此时其它进程将为阻塞状态【比如前面撸过的 tcp 服务】,当进程运行结束后【运行到代码的最后一句或是遇到 return 或是遇到 exit 退出进程函数或是遇到信号事件时将会退出】让出权限并释放掉内存,其它进程就有机会运行了。

进程拥有的自己进程描述符,其中比较常用的是进程号 PID,进程运行时会在系统 /proc/PID 下生成相应的进程文件,用户可以自行查看。

每个进程都拥有所属的进程组【进程的集合】,多个进程组集合则是一个会话,创建一个会话是通过一个进程进行创建的,并且此进程不可以为组长进程,此进程将成为会话期的会话首进程,也会成为进程组的进程组长,同时将会脱离控制终端,即使之前的进程绑定了控制终端也会脱离【守护进程的创建】。

文件描述权限掩码【权限屏蔽字】:

umask () 你可以在 linux 运行这个命令,然后创建文件,并查看它的权限【如果你跑完啥也没有发现,说明你还是训练不够 ^_^】

<?php /**  * Created by PhpStorm.  * User: 1655664358@qq.com  * Date: 2018/3/26  * Time: 14:19  */ namespace ChenWorker; class Server {     public $workerPids = [];     public $workerJob = [];     public $master_pid_file = "master_pid";     public $state_file = "state_file.txt";     function run()     {         $this->daemon();         $this-&gt;worker();         $this-&gt;setMasterPid();         $this-&gt;installSignal();         $this-&gt;showState();         $this-&gt;wait();     }     function wait()     {         while (1){             pcntl_signal_dispatch();             $pid = pcntl_wait($status);             if ($pid&gt;0){                 unset($this-&gt;workerPids[$pid]);             }else{                 if (count($this-&gt;workerPids)==0){                     exit();                 }             }             usleep(100000);         }     }     function showState()     {         $state = "nMaster 信息n";         $state.=str_pad("master pid",25);         $state.=str_pad("worker num",25);         $state.=str_pad("job pid list",10)."n";         $state.=str_pad($this-&gt;getMasterPid(),25);         $state.=str_pad(count($this-&gt;workerPids),25);         $state.=str_pad(implode(",",array_keys($this-&gt;workerPids)),10);         echo $state.PHP_EOL;     }     function getMasterPid()     {         if (file_exists($this-&gt;master_pid_file)){             return file_get_contents($this-&gt;master_pid_file);         }else{             exit("服务未运行n");         }     }     function setMasterPid()     {         $fp = fopen($this-&gt;master_pid_file,"w");         @fwrite($fp,posix_getpid());         @fclose($fp);     }     function daemon()     {         $pid = pcntl_fork();         if ($pid0){             exit(0);         }else{             umask(0);             $sid = posix_setsid();             if ($sid0){                 exit(0);             }             //可以关闭标准输入输出错误文件描述符【守护进程不需要】         }     }     function worker()     {         if (count($this-&gt;workerJob)==0)exit("没有工作任务n");         foreach($this-&gt;workerJob as $job){             $pid = pcntl_fork();             if ($pidworkerInstallSignal();                 $start_time = time();                 while (1){                     pcntl_signal_dispatch();                     if ((time()-$start_time)&gt;=$job-&gt;job_run_time){                         break;                     }                     $job-&gt;run(posix_getpid());                 }                 exit(0);//子进程运行完成后退出                 /***************子进程工作范围**********************/             }else{                 $this-&gt;workerPids[$pid] = $job;             }         }     }     function workerInstallSignal()     {         pcntl_signal(SIGUSR1,[__CLASS__,'workerHandleSignal'],false);     }     function workerHandleSignal($signal)     {         switch ($signal){             case SIGUSR1:                 $state = "worker pid=".posix_getpid()."接受了父进程发来的自定义信号n";                 file_put_contents($this-&gt;state_file,$state,FILE_APPEND);                 break;         }     }     function installSignal()     {         pcntl_signal(SIGINT,[__CLASS__,'handleMasterSignal'],false);         pcntl_signal(SIGTERM,[__CLASS__,'handleMasterSignal'],false);         pcntl_signal(SIGUSR1,[__CLASS__,'handleMasterSignal'],false);     }     function handleMasterSignal($signal)     {         switch ($signal){             case SIGINT:                 //主进程接受到中断信号ctrl+c                 foreach ($this-&gt;workerPids as $pid=&gt;$worker){                     posix_kill($pid,SIGINT);//向所有的子进程发出                 }                 exit("服务平滑停止n");                 break;             case SIGTERM://ctrl+z                 foreach ($this-&gt;workerPids as $pid=&gt;$worker){                     posix_kill($pid,SIGKILL);//向所有的子进程发出                 }                 exit("服务停止n");                 break;             case SIGUSR1://用户自定义信号                 if (file_exists($this-&gt;state_file)){                     unlink($this-&gt;state_file);                 }                 foreach ($this-&gt;workerPids as $pid=&gt;$worker){                     posix_kill($pid,SIGUSR1);                 }                 $state = "master pidn".$this-&gt;getMasterPid()."n";                 while(!file_exists($this-&gt;state_file)){                     sleep(1);                 }                 $state.= file_get_contents($this-&gt;state_file);                 echo $state.PHP_EOL;                 break;         }     } }   <?php /**  * Created by PhpStorm. * User: 1655664358@qq.com  * Date: 2018/3/26 * Time: 14:37 */namespace ChenWorker; class Job {   public $job_run_time = 3600;   function run($pid)  {sleep(3);   echo "worker pid = $pid job 没事干,就在这里jobn";   } }   <?php /**  * Created by PhpStorm. * User: 1655664358@qq.com  * Date: 2018/3/26 * Time: 14:37 */namespace ChenWorker; class Talk {   public $job_run_time = 3600;   function run($pid)  {sleep(3);   echo "worker pid = $pid job 没事干,就在这里talkn";   } } <?php /**  * Created by PhpStorm. * User: 1655664358@qq.com  * Date: 2018/3/26 * Time: 15:45 */ require_once &#39;vendor/autoload.php&#39;; $process = new ChenWorkerServer(); $process->workerJob = [new ChenWorkerTalk(),new ChenWorkerJob()]; $process-&gt;run();

PHP 多进程与信号中断实现多任务常驻内存管理【Master/Worker 模型】

更多laravel相关技术文章,请访问Laravel框架入门教程栏目进行学习!

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享