Heim  >  Artikel  >  Backend-Entwicklung  >  Der PHP-Prozesspool und der Polling-Planungsalgorithmus realisieren Multitasking

Der PHP-Prozesspool und der Polling-Planungsalgorithmus realisieren Multitasking

藏色散人
藏色散人nach vorne
2019-11-13 14:09:122889Durchsuche

phper Bitte verstehen Sie die Prozessplanungsstrategie, die CPU-Zeitscheibe, die Prozesssteuerung [Erstellung, Zerstörung, Recycling, Prozesssignale] und den Prozessablauf und die grundlegenden Prozessgruppen, Signalunterbrechungsprinzipien und die Beziehung zwischen Prozessen.

Prozesskommunikation:

Anonyme Pipe, Named Pipe, Nachrichtenwarteschlange, Speicherfreigabe, Socketpair. Bitte testen Sie den Code selbst

Prozessplanungsalgorithmus:

Abfragen, Zufallsverteilung, Scoreboard und andere Strategien oder grundlegende Algorithmen wie Prioritätsstange, Warteschlange oder Stapel [selbst verwenden]

Prozesspool:

Wenn Sie Erfahrung mit TCP haben, sollten Sie wissen, dass Sie die E/A-Multiplexing-Technologie [Ereignismultiplexer] oder mehrere Prozesse und Multithreads verwenden müssen, um mehrere Clients verwalten zu können . Jedes Mal, wenn ein Client kommt, wird ein Prozess oder Thread geforkt. In diesem Fall sind die Kosten für den Kontextwechsel sehr hoch. Daher erstellen wir zunächst eine Gruppe von Prozessen [Prozesspool], und wenn der Client eine Verbindung herstellt, wird dies der Fall sein Der Algorithmus [wir verwenden Abfragen] wird verwendet, um einen Prozess auszuwählen, der Aufgaben zur Erledigung der Arbeit ausführt. Auf diese Weise besteht keine Notwendigkeit, zwischen Erstellung und Zerstörung hin und her zu wechseln, was seine Effizienz verbessert.

Das Folgende ist die Implementierung der PHP-Codeversion

<?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,&#39;sigHandler&#39;);
    pcntl_signal(SIGTERM,&#39;sigHandler&#39;);
    $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);

Effekt

Der PHP-Prozesspool und der Polling-Planungsalgorithmus realisieren Multitasking

Das obige ist der detaillierte Inhalt vonDer PHP-Prozesspool und der Polling-Planungsalgorithmus realisieren Multitasking. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:learnku.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen