日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php管道邮件,php进程通信-PIPE管道通信

發(fā)布時(shí)間:2025/3/20 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php管道邮件,php进程通信-PIPE管道通信 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一篇文章講到了php進(jìn)程通信的進(jìn)程信號(hào)通信方法,本文介紹的是有名管道:

管道通信,主要是利用文件,寫入以及讀取來(lái)進(jìn)行通信的,

通俗來(lái)講,就是A進(jìn)程在1.txt寫入1,B進(jìn)程讀取1.txt,就能讀取到這個(gè)1,這樣就通信成功了.

當(dāng)然,php進(jìn)程管道通信沒(méi)有這么簡(jiǎn)單

注意:多進(jìn)程系列文章,都建立在linux環(huán)境,php-cli運(yùn)行模式下

一:創(chuàng)建個(gè)專屬管道的文件:$fifoPath?=?"tmp/$name".getmypid()?;//定義文件名

if?(!file_exists($fifoPath))?{

if?(!posix_mkfifo($fifoPath,?0666))?{//posix函數(shù),創(chuàng)建一個(gè)特殊的pipe文件

//????????????????error("create?new?pipe?($name)?error.");

return?false;

}

}?else?{

//????????????error("pipe?($name)?has?exit.");

return?false;

}

二:讀取數(shù)據(jù)$r_pipe?=?fopen($fifoPath,?'r');//正常讀取文件

if?($r_pipe?==?NULL)?{

return?false;

}

$data?=?fread($r_pipe,?1024);

三:寫入數(shù)據(jù)$w_pipe?=?fopen($fifoPath,?'w');//正常操作文件一樣打開該管道

if?($w_pipe?==?NULL)?{

throw?new?Exception('打開管道錯(cuò)誤!');

}

$result?=?fwrite($w_pipe,?$data);//寫入文件一樣寫入數(shù)據(jù)

四:刪除管道unlink($tfifoPath);//刪除文件

五:封裝類<?php

/**

*?Created?by?PhpStorm.

*?User:?tioncico

*?Date:?18-5-21

*?Time:?下午11:51

*/

class?Pipe

{

public?$fifoPath;

private?$w_pipe;

private?$r_pipe;

/**

*?自動(dòng)創(chuàng)建一個(gè)管道

*

*?@param?string?$name?管道名字

*?@param?int?$mode?管道的權(quán)限,默認(rèn)任何用戶組可以讀寫

*/

function?__construct($name?=?'pipe',?$mode?=?0666)

{

$fifoPath?=?"tmp/$name".getmypid()?;

if?(!file_exists($fifoPath))?{

if?(!posix_mkfifo($fifoPath,?$mode))?{

//????????????????error("create?new?pipe?($name)?error.");

return?false;

}

}?else?{

//????????????error("pipe?($name)?has?exit.");

return?false;

}

$this->fifoPath?=?$fifoPath;

}

///

//??寫管道函數(shù)開始

///

function?open_write()

{

$this->w_pipe?=?fopen($this->fifoPath,?'w');

//????????var_dump($this->w_pipe);

if?($this->w_pipe?==?NULL)?{

throw?new?Exception('打開管道錯(cuò)誤!');

//????????????return?false;

}

return?true;

}

function?write($data)

{

return?fwrite($this->w_pipe,?$data);

}

function?write_all($data)

{

$w_pipe?=?fopen($this->fifoPath,?'w');

fwrite($w_pipe,?$data);

fclose($w_pipe);

}

function?close_write()

{

return?fclose($this->w_pipe);

}

/

///?讀管道相關(guān)函數(shù)開始

function?open_read()

{

$this->r_pipe?=?fopen($this->fifoPath,?'r');

if?($this->r_pipe?==?NULL)?{

//????????????error("open?pipe?{$this->fifoPath}?for?read?error.");

return?false;

}

return?true;

}

function?read($byte?=?1024)

{

return?fread($this->r_pipe,?$byte);

}

function?read_all()

{

$r_pipe?=?fopen($this->fifoPath,?'r');

$data???=?'';

while?(!feof($r_pipe))?{

//echo?"read?one?K\n";

$data?.=?fread($r_pipe,?1024);

}

fclose($r_pipe);

return?$data;

}

function?close_read()

{

return?fclose($this->r_pipe);

}

/**

*?刪除管道

*

*?@return?boolean?is?success

*/

function?rm_pipe()

{

return?unlink($this->fifoPath);

}

}

六:注意事項(xiàng)

1:管道與普通文件有一點(diǎn)非常不同的就是:管道需要先有個(gè)進(jìn)程讀取進(jìn)程,才可以寫入,否則按寫入模式打開文件時(shí)阻塞,以下是測(cè)試截圖:

本文為仙士可原創(chuàng)文章,轉(zhuǎn)載無(wú)需和我聯(lián)系,但請(qǐng)注明來(lái)自仙士可博客www.php20.cn

總結(jié)

以上是生活随笔為你收集整理的php管道邮件,php进程通信-PIPE管道通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。