特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> PHP教程> php多进程通信之管道的介绍

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

时间:2022-05-25 10:47:51 作者:互联网

说明

1、管道是比较常用的多进程通信手段,管道分为无名管道与有名管道。

2、无名管道只能用于具有亲缘关系的进程间通信,而有名管道可以用于同一主机上任意进程。

实例

$pipe_path = '/d***/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多进程通信之管道的介绍,希望对大家有所帮助。

相关文章

相关应用

热门文章

猜你喜欢

返回顶部