品易云推流 关闭
文章详情页
文章 > PHP教程 > php多进程通信之管道的介绍

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

php多进程通信 管道

头像

小妮浅浅

2021-08-20 09:45:474508浏览 · 0收藏 · 0评论

说明

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多进程通信之管道的介绍,希望对大家有所帮助。更多php学习指路:php教程

推荐操作系统:windows7系统、PHP5.6、DELL G3电脑

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图