php进程间通信 php多进程通信之管道的介绍


php进程间通信 php多进程通信之管道的介绍

文章插图
说明
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多进程通信之管道的介绍,希望对大家有所帮助 。


    以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

    「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: