phper Please understand the process scheduling strategy, CPU time slice, process control [creation, destruction, recycling, process signals] and process running flow and basic process groups, signal interruption principles, and the relationship between processes.
Communication of processes:
Anonymous pipes, named pipes, message queues, memory sharing, socketpair. Please test the code yourself
Process scheduling algorithm:
Polling, random distribution, scoreboard and other strategies, or basic algorithms such as a priority pole, a queue, or a stack [use it yourself]
Process pool:
If you have played with tcp, you should know that in order to be able to handle multiple clients, you have to use IO multiplexing technology [event multiplexer] or multiple Processes and multi-threads. Every time a client comes, a process or thread will be forked. In that case, the cost of context switching is very high, so we first create a group of processes [process pool], and when the client connects, it will be The algorithm [we use polling] is used to select a process to deliver tasks to do the work. This way, there is no need to go back and forth between creation and destruction, which improves its efficiency.
The following is the implementation of the PHP code version
<?php /** * Created by PhpStorm. * User: 1655664358@qq.com * Date: 2019/1/12 * Time: 16:18 */ $flag = 1; class process { public $pid; public $name; public $file; public $num; } class instance { public $processIdx; public $proc = []; public $processNum; } function sigHandler($sigNo) { global $flag; $flag = 0; echo "信号中断处理".PHP_EOL; } function processPool(instance &$instance,$num) { if (!$instance||$num==0){ fprintf(STDERR,"%s","参数错误"); return 1; } $instance->processIdx = 0; $instance->processNum = $num; pcntl_signal(SIGINT,'sigHandler'); pcntl_signal(SIGTERM,'sigHandler'); $process = new process(); for ($i=1;$i<=$num;$i++){ $instance->proc[$i] = clone $process; $instance->proc[$i]->file = $i; $instance->proc[$i]->pid = pcntl_fork(); $instance->processIdx = $i; if ($instance->proc[$i]->pid<0){ exit("进程创建失败"); } else if ($instance->proc[$i]->pid>0){ //nothing continue; }else{ worker($instance); } } master($instance); $exitProcess= []; while (1){ for ($i=1;$i<=$num;$i++){ //非阻塞方式回收子进程 pcntl_waitpid($instance->proc[$i]->pid,$status,WNOHANG); if ($status){ $exitProcess[] = $instance->proc[$i]->pid; fwrite(STDOUT,"worker#".$instance->proc[$i]->pid."-".$status,30); } } if (count($exitProcess)==$instance->processNum){ exit(0); } usleep(1000); } } //简单的轮询算法 自己可以用队列,随机,链表,栈链,二叉树啥的折腾 function roundRobin(&$instance,$roll) { /** @var instance $instance */ return $instance->proc[$roll%$instance->processNum+1]; } function master(&$instance) { /** @var instance $instance */ fprintf(STDOUT,"master 进程 %d\n",$instance->processIdx); global $flag; $roll = 0; while ($flag){ pcntl_signal_dispatch(); /** @var process $process */ $process = roundRobin($instance,$roll++); echo "轮询的进程:".$process->pid.PHP_EOL; $file = $process->file; posix_mkfifo($file,0666); $fd = fopen($file,"w"); fwrite($fd,"hi",2); sleep(1); } for ($i=1;$i<=$instance->processNum;$i++){ posix_kill($instance->proc[$i]->pid,9); } fprintf(STDOUT,"master shutdown %d\n",$instance->processIdx); } function getProcess(&$instance) { /** @var instance $instance */ return $instance->proc[$instance->processIdx]; } function worker(&$instance) { /** @var process $process */ $process = getProcess($instance); while (1){ $file = $process->file; posix_mkfifo($file,0666); $fd = fopen($file,"r"); $content = fread($fd,10); fprintf(STDOUT,"worker#%d读取的内容:%s file=%d\n",posix_getpid(),$content,$file); } exit(0); } $instance = new instance(); processPool($instance,5);
Effect
The above is the detailed content of PHP process pool and polling scheduling algorithm realize multi-tasking. For more information, please follow other related articles on the PHP Chinese website!