illustrate
1. Pipes are a commonly used means of multi-process communication. Pipes are divided into unnamed pipes and named pipes.
2. Unnamed pipes can only be used for communication between processes that are related, while named pipes can be used for any process on the same host.
Example
$pipe_path = '/data/test.pipe'; if(!file_exists($pipe_path)){ if(!posix_mkfifo($pipe_path,0664)){ exit("create pipe error!"); } } $pid = pcntl_fork(); if($pid == 0){ // Child process, writes data to the pipe $file = fopen($pipe_path,'w'); while (true){ fwrite($file,'hello world'); $rand = rand(1,3); sleep($rand); } exit('child end!'); }else{ // Parent process, read data from the pipe $file = fopen($pipe_path,'r'); while (true){ $rel = fread($file,20); echo "{$rel}n"; $rand = rand(1,2); sleep($rand); } }
The above is an introduction to the pipeline of PHP multi-process communication. I hope it will be helpful to everyone.