


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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
