Home Backend Development PHP Tutorial php开启多进程的方法_php技巧

php开启多进程的方法_php技巧

May 16, 2016 pm 08:22 PM
php multi-Progress turn on method

本文实例讲述了php开启多进程的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

 $IP='192.168.1.1';//Windows電腦的IP
 $Port='5900';        //VNC使用的Port
 $ServerPort='9999';//Linux Server對外使用的Port
 $RemoteSocket=false;//連線到VNC的Socket
 function SignalFunction($Signal){
  //這是主Process的訊息處理函數
 global $PID;//Child Process的PID
 switch ($Signal)
 {
  case SIGTRAP:
  case SIGTERM:
   //收到結束程式的Signal
   if($PID)
   {
    //送一個SIGTERM的訊號給Child告訴他趕快結束掉嘍
    posix_kill($PID,SIGTERM);
    //等待Child Process結束,避免zombie
    pcntl_wait($Status);
   }
   //關閉主Process開啟的Socket
   DestroySocket();
   exit(0); //結束主Process
   break;
  case SIGCHLD:
   /*
當Child Process結束掉時,Child會送一個SIGCHLD訊號給Parrent
當Parrent收到SIGCHLD,就知道Child Process已經結束嘍 ,該做一些
結束的動作*/
   unset($PID); //將$PID清空,表示Child Process已經結束
   pcntl_wait($Status); //避免Zombie
   break;
  default:
 }
 }
 function ChildSignalFunction($Signal){
//這是Child Process的訊息處理函數
 switch ($Signal)
 {
  case SIGTRAP:
  case SIGTERM:
//Child Process收到結束的訊息
   DestroySocket(); //關閉Socket
   exit(0); //結束Child Process
  default:
 }
 }
 function ProcessSocket($ConnectedServerSocket){
 //Child Process Socket處理函數
 //$ConnectedServerSocket -> 外部連進來的Socket
 global $ServerSocket,$RemoteSocket,$IP,$Port;
 $ServerSocket=$ConnectedServerSocket;
 declare(ticks = 1); //這一行一定要加,不然沒辦法設定訊息處理函數。
//設定訊息處理函數
 if(!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;
 if(!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;
//建立一個連線到VNC的Socket
 $RemoteSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//連線到內部的VNC
 @$RemoteConnected=socket_connect($RemoteSocket,$IP,$Port);
 if(!$RemoteConnected) return; //無法連線到VNC 結束
//將Socket的處理設為Nonblock,避免程式被Block住
 if(!socket_set_nonblock($RemoteSocket)) return;
 if(!socket_set_nonblock($ServerSocket)) return;
 while(true)
 {
//這邊我們採用pooling的方式去取得資料
  $NoRecvData=false;   //這個變數用來判別外部的連線是否有讀到資料
  $NoRemoteRecvData=false;//這個變數用來判別VNC連線是否有讀到資料
  @$RecvData=socket_read($ServerSocket,4096,PHP_BINARY_READ);
//從外部連線讀取4096 bytes的資料
  @$RemoteRecvData=socket_read($RemoteSocket,4096,PHP_BINARY_READ);
//從vnc連線連線讀取4096 bytes的資料
  if($RemoteRecvData==='')
  {
//VNC連線中斷,該結束嘍
   echo"Remote Connection Close\n";
   return;  
  }
  if($RemoteRecvData===false)
  {
/*
由於我們是採用nonblobk模式
這裡的情況就是vnc連線沒有可供讀取的資料
*/
   $NoRemoteRecvData=true;
//清除掉Last Errror
   socket_clear_error($RemoteSocket);
  }
  if($RecvData==='')
  {
//外部連線中斷,該結束嘍
   echo"Client Connection Close\n";
   return;
  }
  if($RecvData===false)
  {
/*
由於我們是採用nonblobk模式
這裡的情況就是外部連線沒有可供讀取的資料
*/
   $NoRecvData=true;
//清除掉Last Errror
   socket_clear_error($ServerSocket);
  }
  if($NoRecvData&&$NoRemoteRecvData)
  {
//如果外部連線以及VNC連線都沒有資料可以讀取時,
//就讓程式睡個0.1秒,避免長期佔用CPU資源
   usleep(100000);
//睡醒後,繼續作pooling的動作讀取socket
   continue;
  }
  //Recv Data
  if(!$NoRecvData)
  {
//外部連線讀取到資料
   while(true)
   {
//把外部連線讀到的資料,轉送到VNC連線上
    @$WriteLen=socket_write($RemoteSocket,$RecvData);
    if($WriteLen===false)
    {
//由於網路傳輸的問題,目前暫時無法寫入資料
//先睡個0.1秒再繼續嘗試。
     usleep(100000);
     continue;
    }
    if($WriteLen===0)
    {
//遠端連線中斷,程式該結束了
     echo"Remote Write Connection Close\n";
     return;
    }
//從外部連線讀取的資料,已經完全送給VNC連線時,中斷這個迴圈。
    if($WriteLen==strlen($RecvData)) break;
//如果資料一次送不完就得拆成好幾次傳送,直到所有的資料全部送出為止
    $RecvData=substr($RecvData,$WriteLen);
   }
  }
  if(!$NoRemoteRecvData)
  {
//這邊是從VNC連線讀取到的資料,再轉送回外部的連線
//原理跟上面差不多不再贅述
   while(true)
   {
    @$WriteLen=socket_write($ServerSocket,$RemoteRecvData);
    if($WriteLen===false)
    {
     usleep(100000);
     continue;
    }
    if($WriteLen===0)
    {
     echo"Remote Write Connection Close\n";
     return;
    }
    if($WriteLen==strlen($RemoteRecvData)) break;
    $RemoteRecvData=substr($RemoteRecvData,$WriteLen);
   }
  }
 }
 }
 function DestroySocket(){
//用來關閉已經開啟的Socket
 global$ServerSocket,$RemoteSocket;
 if($RemoteSocket)
 {
//如果已經開啟VNC連線
//在Close Socket前必須將Socket shutdown不然對方不知到你已經關閉連線了
  @socket_shutdown($RemoteSocket,2);
  socket_clear_error($RemoteSocket);
//關閉Socket
  socket_close($RemoteSocket);  
 }
//關閉外部的連線
 @socket_shutdown($ServerSocket,2);
 socket_clear_error($ServerSocket);
 socket_close($ServerSocket);
 }
//這裡是整個程式的開頭,程式從這邊開始執行
//這裡首先執行一次fork
 $PID=pcntl_fork();
 if($PID==-1) die("could not fork");
//如果$PID不為0表示這是Parrent Process
//$PID就是Child Process
//這是Parrent Process 自己結束掉,讓Child成為一個Daemon。
 if($PID) die("Daemon PID:$PID\n");
//從這邊開始,就是Daemon模式在執行了
//將目前的Process跟終端機脫離成為daemon模式
 if(!posix_setsid()) die("could not detach from terminal\n");
//設定daemon 的訊息處理函數
 declare(ticks = 1);
 if(!pcntl_signal(SIGTERM, "SignalFunction")) die("Error!!!\n");
 if(!pcntl_signal(SIGTRAP, "SignalFunction")) die("Error!!!\n");
 if(!pcntl_signal(SIGCHLD, "SignalFunction")) die("Error!!!\n");
//建立外部連線的Socket
 $ServerSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//設定外部連線監聽的IP以及Port,IP欄位設0,表示經聽所有介面的IP
 if(!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!\n");
//開始監聽Port
 if(!socket_listen($ServerSocket)) die("Cannot Listen!\n");
//將Socket設為nonblock模式
 if(!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!\n");
//清空$PID變數,表示目前沒有任何的Child Process
 unset($PID);
 while(true)
 {
//進入pooling模式,每隔1秒鐘就去檢查有沒有連線進來。
  sleep(1);
//檢查有沒有連線進來
  @$ConnectedServerSocket=socket_accept($ServerSocket);
  if($ConnectedServerSocket!==false)
  {
//有人連進來嘍
//起始一個Child Process用來處理連線
   $PID=pcntl_fork();
   if($PID==-1) die("could not fork");
   if($PID) continue;//這是daemon process,繼續回去監聽。
   //這裡是Child Process開始
   //執行Socket裡函數
   ProcessSocket($ConnectedServerSocket);
  //處理完Socket後,結束掉Socket
   DestroySocket();
  //結束Child Process
   exit(0);
  }
 }

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles