一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看

php語(yǔ)言

PHP多線(xiàn)程編程之管道通信實(shí)例

時(shí)間:2025-03-16 02:45:36 php語(yǔ)言 我要投稿
  • 相關(guān)推薦

PHP多線(xiàn)程編程之管道通信實(shí)例

  PHP多線(xiàn)程編程之管道通信,實(shí)例分析了管道通信的原理與相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下.

  本文實(shí)例講述了PHP多線(xiàn)程編程之管道通信用法。分享給大家供大家參考。具體分析如下:

  一個(gè)線(xiàn)程如果是個(gè)人英雄主義,那么多線(xiàn)程就是集體主義,你不再是一個(gè)獨行俠,而是一個(gè)指揮家。

  管道通信:

  1. 管道可以認為是一個(gè)隊列,不同的線(xiàn)程都可以往里面寫(xiě)東西,也都可以從里面讀東西。寫(xiě)就是

  在隊列末尾添加,讀就是在隊頭刪除。

  2. 管道一般有大小,默認一般是4K,也就是內容超過(guò)4K了,你就只能讀,不能往里面寫(xiě)了。

  3. 默認情況下,管道寫(xiě)入以后,就會(huì )被阻止,直到讀取他的程序讀取把數據讀完。而讀取線(xiàn)程也會(huì )被阻止,

  直到有進(jìn)程向管道寫(xiě)入數據。當然,你可以改變這樣的默認屬性,用stream_set_block  函數,設置成非阻斷模式。

  下面是我分裝的一個(gè)管道的類(lèi)(這個(gè)類(lèi)命名有問(wèn)題,沒(méi)有統一,沒(méi)有時(shí)間改成統一的了,我一般先寫(xiě)測試代碼,最后分裝,所以命名上可能不統一):

  <?php

  class Pipe

  {

  public $fifoPath;

  private $w_pipe;

  private $r_pipe;

  /**

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

  *

  * @param string $name 管道名字

  * @param int $mode 管道的權限,默認任何用戶(hù)組可以讀寫(xiě)

  */

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

  {

  $fifoPath = "/tmp/$name." . posix_getpid();

  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;

  }

  ///////////////////////////////////////////////////

  // 寫(xiě)管道函數開(kāi)始

  ///////////////////////////////////////////////////

  function open_write()

  {

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

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

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

  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)函數開(kāi)始

  ////////////////////////////////////////////////////////

  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);

  }

  }

  ?>

  /*

  有了這個(gè)類(lèi),就可以實(shí)現簡(jiǎn)單的管道通信了。*/

【PHP多線(xiàn)程編程之管道通信實(shí)例】相關(guān)文章:

JAVA多線(xiàn)程之線(xiàn)程間的通信方式解析07-14

PHP的Socket通信之UDP通信實(shí)例07-27

java多線(xiàn)程-線(xiàn)程通信實(shí)例詳細解讀07-07

PHP實(shí)現多線(xiàn)程的方法03-19

PHP實(shí)現多線(xiàn)程的方法08-02

PHP多線(xiàn)程的實(shí)現方法03-13

php畫(huà)圖實(shí)例07-16

PHP實(shí)現多線(xiàn)程的方法技巧08-16

php查詢(xún)mysql的實(shí)例04-03

一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看