phper 請(qǐng)了解進(jìn)程調(diào)度策略,CPU 時(shí)間片,進(jìn)程控制【創(chuàng)建,銷(xiāo)毀,回收,進(jìn)程信號(hào)】與及進(jìn)程運(yùn)行流程和基本的進(jìn)程組,信號(hào)中斷原理,以及進(jìn)程之間的關(guān)系。
擼過(guò) tcp 的話應(yīng)該知道要能處理多個(gè)客戶(hù)端,就得用 IO 復(fù)用技術(shù)【事件多路分發(fā)器】或是多進(jìn)程以及多線程,每來(lái)一個(gè)客戶(hù)端就 fork 一個(gè)進(jìn)程或是線程,那樣的話上下文切換成本特別高,所以咱們先創(chuàng)建好一組進(jìn)程【進(jìn)程池】,等客戶(hù)端連接上來(lái)的時(shí)候,通過(guò)某種算法【我們用的輪詢(xún)】來(lái)選擇某個(gè)進(jìn)程投遞任務(wù)來(lái)干活,這樣的話就不用創(chuàng)建又銷(xiāo)毀來(lái)回折騰了,提升它的效率。下面是 PHP 代碼版本的實(shí)現(xiàn)
?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 "信號(hào)中斷處理".PHP_EOL;
}
function processPool(instance $instance,$num)
{
if (!$instance||$num==0){
fprintf(STDERR,"%s","參數(shù)錯(cuò)誤");
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]->pid0){
exit("進(jìn)程創(chuàng)建失敗");
}
else if ($instance->proc[$i]->pid>0){
//nothing
continue;
}else{
worker($instance);
}
}
master($instance);
$exitProcess= [];
while (1){
for ($i=1;$i=$num;$i++){
//非阻塞方式回收子進(jìn)程
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);
}
}
//簡(jiǎn)單的輪詢(xún)算法 自己可以用隊(duì)列,隨機(jī),鏈表,棧鏈,二叉樹(shù)啥的折騰
function roundRobin($instance,$roll)
{
/** @var instance $instance */
return $instance->proc[$roll%$instance->processNum+1];
}
function master($instance)
{
/** @var instance $instance */
fprintf(STDOUT,"master 進(jìn)程 %d\n",$instance->processIdx);
global $flag;
$roll = 0;
while ($flag){
pcntl_signal_dispatch();
/** @var process $process */
$process = roundRobin($instance,$roll++);
echo "輪詢(xún)的進(jìn)程:".$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讀取的內(nèi)容:%s file=%d\n",posix_getpid(),$content,$file);
}
exit(0);
}
$instance = new instance();
processPool($instance,5);