phpマルチプロセスを開く方法
本論文の実例は、phpがマルチプロセスを開始する方法を述べている。皆さんの参考にしてください。具体的な実現方法は以下の通りです。
<?php
$IP='192.168.1.1';//WindowsX IP
$Port='5900'; //VNC Port
$ServerPort='9999';//Linux Serverν Port
$RemoteSocket=false;//B VNC Socket
function SignalFunction($Signal){
//@ Process
global $PID;//Child Process PID
switch ($Signal)
{
case SIGTRAP:
case SIGTERM:
// Y Signal
if($PID)
{
// SIGTERM oChild V s Y D
posix_kill($PID,SIGTERM);
// Child ProcessY , zombie
pcntl_wait($Status);
}
//P] Process_⒌Socket
DestroySocket();
exit(0); //Y Process
break;
case SIGCHLD:
/*
Child ProcessY r,Child SIGCHLDoParrent
Parrent SIGCHLD, Child Process Y D ,
Y */
unset($PID); //$PID , Child Process Y
pcntl_wait($Status); // Zombie
break;
default:
}
}
function ChildSignalFunction($Signal){
//@ Child Process
switch ($Signal)
{
case SIGTRAP:
case SIGTERM:
//Child Process Y
DestroySocket(); //P]Socket
exit(0); //Y Child Process
default:
}
}
function ProcessSocket($ConnectedServerSocket){
//Child Process Socket
//$ConnectedServerSocket -> BM Socket
global $ServerSocket,$RemoteSocket,$IP,$Port;
$ServerSocket=$ConnectedServerSocket;
declare(ticks = 1); //@ , ]k O
//O
if(!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;
if(!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;
// B VNC Socket
$RemoteSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//B VNC
@$RemoteConnected=socket_connect($RemoteSocket,$IP,$Port);
if(!$RemoteConnected) return; //o B VNC Y
//Socket ONonblock, Block
if(!socket_set_nonblock($RemoteSocket)) return;
if(!socket_set_nonblock($ServerSocket)) return;
while(true)
{
//@ pooling Y
$NoRecvData=false; //@ e B x Y
$NoRemoteRecvData=false;//@ eVNCB x Y
@$RecvData=socket_read($ServerSocket,4096,PHP_BINARY_READ);
// Bx 4096 bytes Y
@$RemoteRecvData=socket_read($RemoteSocket,4096,PHP_BINARY_READ);
//vncBBx 4096 bytes Y
if($RemoteRecvData==='')
{
//VNCB Y D
echo"Remote Connection Close
";
return;
}
if($RemoteRecvData===false)
{
/*
nonblobk
@e r vncB] x Y
*/
$NoRemoteRecvData=true;
// Last Errror
socket_clear_error($RemoteSocket);
}
if($RecvData==='')
{
// B Y D
echo"Client Connection Close
";
return;
}
if($RecvData===false)
{
/*
nonblobk
@e r B] x Y
*/
$NoRecvData=true;
// Last Errror
socket_clear_error($ServerSocket);
}
if($NoRecvData&&$NoRemoteRecvData)
{
// B VNCB ] Y x r,
// 0.1 , L CPUY
usleep(100000);
// ,^m pooling x socket
continue;
}
//Recv Data
if(!$NoRecvData)
{
// Bx Y
while(true)
{
// Bx Y ,D VNCB
@$WriteLen=socket_write($RemoteSocket,$RecvData);
if($WriteLen===false)
{
// W }, ro Y
// 0.1 ^mL。
usleep(100000);
continue;
}
if($WriteLen===0)
{
//h B Y
echo"Remote Write Connection Close
";
return;
}
// Bx Y , oVNCBr, @ 。
if($WriteLen==strlen($RecvData)) break;
// Y Y
$RecvData=substr($RecvData,$WriteLen);
}
}
if(!$NoRemoteRecvData)
{
//@ VNCBx Y , D B
//
while(true)
{
@$WriteLen=socket_write($ServerSocket,$RemoteRecvData);
if($WriteLen===false)
{
usleep(100000);
continue;
}
if($WriteLen===0)
{
echo"Remote Write Connection Close
";
return;
}
if($WriteLen==strlen($RemoteRecvData)) break;
$RemoteRecvData=substr($RemoteRecvData,$WriteLen);
}
}
}
}
function DestroySocket(){
// P] _⒌Socket
global$ServerSocket,$RemoteSocket;
if($RemoteSocket)
{
// _VNCB
// Close Socket Socket shutdown Ψ P]B
@socket_shutdown($RemoteSocket,2);
socket_clear_error($RemoteSocket);
//P]Socket
socket_close($RemoteSocket);
}
//P] B
@socket_shutdown($ServerSocket,2);
socket_clear_error($ServerSocket);
socket_close($ServerSocket);
}
//@e _^, @_
//@e fork
$PID=pcntl_fork();
if($PID==-1) die("could not fork");
// $PID 0 @ Parrent Process
//$PID Child Process
//@ Parrent Process Y ,Child Daemon。
if($PID) die("Daemon PID:$PID
");
// @_ , Daemon
//⒛ Process K Cx daemon
if(!posix_setsid()) die("could not detach from terminal
");
//O daemon
declare(ticks = 1);
if(!pcntl_signal(SIGTERM, "SignalFunction")) die("Error!!!
");
if(!pcntl_signal(SIGTRAP, "SignalFunction")) die("Error!!!
");
if(!pcntl_signal(SIGCHLD, "SignalFunction")) die("Error!!!
");
// B Socket
$ServerSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//O BO IP Port,IP O0, IP
if(!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!
");
//_ OPort
if(!socket_listen($ServerSocket)) die("Cannot Listen!
");
//SocketOnonblock
if(!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!
");
// $PID ] Child Process
unset($PID);
while(true)
{
//M pooling , 1 z ] BM
sleep(1);
//z ] BM
@$ConnectedServerSocket=socket_accept($ServerSocket);
if($ConnectedServerSocket!==false)
{
// BMD
// Child Process B
$PID=pcntl_fork();
if($PID==-1) die("could not fork");
if($PID) continue;//@ daemon process,^m O。
//@e Child Process_
// Sockete
ProcessSocket($ConnectedServerSocket);
// Socket ,Y Socket
DestroySocket();
//Y Child Process
exit(0);
}
}
本論文で述べたように、皆さんのphpプログラムの設計に役に立ちます。