php interprocess communication message queuing

Keywords: PHP less

Why process is needed

Process is the basic unit of resource allocation and scheduling. As the carrier of the independent operation of the program, the process ensures the normal execution of the program. The existence of processes makes the utilization of operating system resources greatly improved.

Message queue

Message queuing is a container that holds messages during their transmission. The message queue manager acts as a mediator between the sender and the receiver of a message. The main purpose of message queuing is to create a route and ensure reliable delivery of messages; if the receiver is unavailable when sending a message, message queuing will keep the message until someone receives it.

Message queuing can provide temporary storage function and ensure the reliable delivery of messages. We just use it to realize inter process communication. Of course, message queuing is not only used for inter process communication, but also widely used. For example, message queuing is very suitable for solving the problems of consumers and producers, because there is always a "speed difference" between producers and consumers. For example, if the number of producers is suddenly less than 10, the processing speed of both sides will be unbalanced, which will lead to queue blocking and service unavailability. This is certainly not what we want to see. If message queuing is introduced at this time to decouple the two systems, no matter who is slow, it will not affect the overall business.

code implementation

<?php
$childList = [];
$id = ftok(__FILE__,'m');//Convert pathnames and project identifiers to System V IPC keys
$msgQueue = msg_get_queue($id);//create a message queue
const MSG_TYPE = 1;
//Producer
function producer()
{
    global $msgQueue;
    $pid = posix_getpid();//Get current process pid
    $i = rand(1,10);
    while ($i){
        $message = "Producer process ID:{$pid},round:{$i}";
        msg_send($msgQueue,MSG_TYPE,$message);
        $i--;
    }
    echo "producer success\n";
}

//Consumer
function consume()
{
    global $msgQueue;
    $pid = posix_getpid();
    while (msg_receive($msgQueue,MSG_TYPE,$msgType,1024,$message)){
        echo "Consumer process ID: {$pid}´╝îreceived message:{$message}\n";
    }
}

//Create process
function createProgress($callBack){
    global $childList;
    //Branch (child process) at the current position of the current process
    $pid = pcntl_fork();
    //Both the parent and child processes execute the following code
    if ($pid == -1){
        //Error handling: Return - 1 when failed to create child process
        exit('fork error');
    }elseif ($pid == 0){
        //The sub process gets $pid of 0, so here is the logic of the sub process execution.
        $pid = posix_getpid();
        echo "child progress:{$pid}\n";
        $callBack();
        exit("{$pid}:child progress exit\n");
    }else{
        //The parent process will get the child process number, so here is the logic of the parent process execution
        $childList[$pid] = 1;
    }
}

createProgress('producer');
createProgress('consume');

//Wait for the subprocess to be interrupted to prevent the subprocess from becoming a zombie process.
if (!empty($childList)){
    $pid = pcntl_wait($status);
    if ($pid > 0){
        unset($childList[$pid]);
        echo "{$pid}\n";
    }
}

Operation result

Environmental Science

  • pcntl extension: the main process extension, which completes the process creation and waits for the operation.
  • sysvmsg extension: it implements message queuing of interprocess communication in system v mode.

Posted by bronzemonkey on Wed, 06 Nov 2019 06:02:32 -0800