Multi-process named pipe communication [unrelated processes] PHP version

藏色散人
Release: 2023-04-07 17:32:01
forward
2191 people have browsed it

Process communication:

1. Interrupt system

2. Unnamed pipe [half-duplex communication]

can only be used for parent and child processes , communication between sibling processes without a name is called an unnamed pipe. The transmitted data is unformatted. Both parties can define the format themselves and do not rely on the file system.

Recommendation: "PHP Tutorial"

3. Named pipes

are used for troublesome operations that cannot be achieved by unnamed pipes, even for processes that have nothing to do with them. Can communicate [it depends on a file descriptor, but this file is a FIFO type file, that is, a pipeline file, and adheres to the FIFO principle, that is, the queue is first in, first out]

PS: Be sure to understand process blocking, non Blocking principle, pipeline file, process [running status transfer]

Examples of application scenarios: If you use PHP's socket API to write a network framework similar to Workerman, you can perform unified interrupt signal event processing

The interrupt handler is only responsible for accepting interrupt requests and writing data through the pipe write end. The main process listens for events on the file descriptor [readable events]. You can privately check out the stream socket related API and learn about the IO multiplexing function. How to use】Reprocess. The interrupt handler will not be left waiting for too long.

Of course, communication between cross-language processes can also be achieved through named pipes.

$filePath = $argv[1];
if (posix_mkfifo($filePath,0666)<0){
    fprintf(STDOUT,"命名管道创建错误");
    exit(0);
}
$fd = fopen($filePath,"w");
while (is_resource($fd)){
    fprintf(STDOUT,">");
    $data = fgets(STDIN);
    if ($data){
        fwrite($fd,$data,strlen($data));
    }
}
exit(0);
Copy after login
rrree

The above is the detailed content of Multi-process named pipe communication [unrelated processes] PHP version. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!