
說明
1.管道是比較常用的多進程通訊手段,管道分為無名管道與有名管道。
2.無名管道只能用於具有親緣關係的進程間通信,而有名管道可以用於同一主機上任意進程。
實例
$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){
// 子程序,寫入資料給管道 $file = fopen($pipe_path,'w');
while (true){
fwrite($file,'hello world');
$rand = rand(1,3);
sleep($rand);
}
exit('child end!');
}else{
// 父行程,從管道讀取資料 $file = fopen($pipe_path,'r');
while (true){
$rel = fread($file,20);
echo "{$rel}n";
$rand = rand(1,2);
sleep($rand);
}
}以上就是php多進程通訊之管道的介紹,希望對大家有幫助。