PHP Open Multi-process method
The example in this article describes how PHP opens a multi-process approach. Share to everyone for your reference. The implementation method is as follows:
The code is as follows:
$IP = ' 192.168.1.1 ';//windows computer's IP
$Port = ' 5900 '; Port used by VNC
$ServerPort = ' 9999 ';//linux server out-of-use port
$RemoteSocket =false;//Connect to VNC's socket
function Signalfunction ($Signal) {
This is the message function for the main process.
Global $PID;//child process PID
Switch ($Signal)
{
Case SIGTRAP:
Case SIGTERM:
Received the signal of the end program
if ($PID)
{
Send a SIGTERM message to child and tell him to hurry up and get rid of cola.
Posix_kill ($PID, SIGTERM);
Wait for child process to end, avoid zombie
Pcntl_wait ($Status);
}
Close the socket of the main process
Destroysocket ();
Exit (0); The end of the main process
Break
Case SIGCHLD:
/*
When child process is finished, the child sends a SIGCHLD message to Parrent
When Parrent received SIGCHLD, she knew that the child process had ended cola, and should do some
The end of the motion
Unset ($PID); Empty the $pid to indicate that the child process has ended
Pcntl_wait ($Status); Avoid Zombie
Break
Default
}
}
function Childsignalfunction ($Signal) {
This is the information function for child process
Switch ($Signal)
{
Case SIGTRAP:
Case SIGTERM:
Child process receives the message
Destroysocket (); Close socket
Exit (0); End Child Process
Default
}
}
function Processsocket ($ConnectedServerSocket) {
Child Process Socket Processor function
External socket $ConnectedServerSocket-I
Global $ServerSocket, $RemoteSocket, $IP, $Port;
$ServerSocket = $ConnectedServerSocket;
DECLARE (ticks = 1); This line must be added, otherwise there is no way to set the message function.
Set Message function
if (!pcntl_signal (SIGTERM, "childsignalfunction")) return;
if (!pcntl_signal (SIGTRAP, "childsignalfunction")) return;
Create a connection to the VNC socket
$RemoteSocket =socket_create (Af_inet, sock_stream,sol_tcp);
Connect to the internal VNC
@ $RemoteConnected =socket_connect ($RemoteSocket, $IP, $Port);
if (! $RemoteConnected) return; You can't connect to VNC.
Nonblock the socket to prevent the program from being blocked
if (!socket_set_nonblock ($RemoteSocket)) return;
if (!socket_set_nonblock ($ServerSocket)) return;
while (true)
{
This way we use pooling to get the information.
$NoRecvData =false; This variable is used to determine whether external connections have read the information
$NoRemoteRecvData =false;//This variable to determine if the VNC connection has read the information
@ $RecvData =socket_read ($ServerSocket, 4096,php_binary_read);
Read 4096 bytes information from external connections
@ $RemoteRecvData =socket_read ($RemoteSocket, 4096,php_binary_read);
Read 4096 bytes information from a VNC connection
if ($RemoteRecvData = = = ")
{
The VNC connection is interrupted, the end of the COLA
echo "Remote Connection close\n";
Return
}
if ($RemoteRecvData ===false)
{
/*
Since we are using NONBLOBK mode
The situation here is that the VNC connection has no access to the information
*/
$NoRemoteRecvData =true;
Clear out last Errror
Socket_clear_error ($RemoteSocket);
}
if ($RecvData = = = ")
{
The external connection is interrupted, the end of the beam cola
echo "Client Connection close\n";
Return
}
if ($RecvData ===false)
{
/*
Since we are using NONBLOBK mode
The situation here is that the external connection has no access to the information
*/
$NoRecvData =true;
Clear out last Errror
Socket_clear_error ($ServerSocket);
}
if ($NoRecvData && $NoRemoteRecvData)
{
If external connections and VNC connections are not available to read,
Let the program sleep for 0.1 seconds to avoid long-term CPU resources
Usleep (100000);
After waking up, continue to act as pooling to read the socket.
Continue
}
Recv Data
if (! $NoRecvData)
{
External access to Information
while (true)
{
Transfer the information from the external connection to the VNC connector
@ $WriteLen =socket_write ($RemoteSocket, $RecvData);
if ($WriteLen ===false)
{
Because of the problem of Internet transport, it is currently not available to write information
Go to sleep for 0.1 seconds and then continue experimenting.
Usleep (100000);
Continue
}
if ($WriteLen ===0)
{
In the remote connection, the program ends.
echo "Remote Write Connection close\n";
Return
}
The information you read from an external connection has been completely sent to the VNC connection, which interrupts this loop.
if ($WriteLen ==strlen ($RecvData)) break;
If the information is delivered at one time, it has to be broken down several times, until all the information is sent out as a stop.
$RecvData =substr ($RecvData, $WriteLen);
}
}
if (! $NoRemoteRecvData)
{
This is the information that is read from the VNC connection and then sent back to the external connection
The principle is almost no longer baggage described above
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 () {
To close the opened socket
Global$serversocket, $RemoteSocket;
if ($RemoteSocket)
{
If you have already opened the VNC connection
The socket must be shutdown before the close socket is not known to you.
@socket_shutdown ($RemoteSocket, 2);
Socket_clear_error ($RemoteSocket);
Close socket
Socket_close ($RemoteSocket);
}
Close the external connection
@socket_shutdown ($ServerSocket, 2);
Socket_clear_error ($ServerSocket);
Socket_close ($ServerSocket);
}
This is the beginning of the whole program, and the program starts here.
This is the first time to perform a fork
$PID =pcntl_fork ();
if ($PID ==-1) die ("Could not fork");
If $pid does not represent 0, this is parrent Process
$PID is child Process.
This is parrent Process itself, and let the child become a daemon.
if ($PID) die ("Daemon PID: $PID \ n");
From here on, that's daemon mode.
To detach the current process from the terminal into daemon mode.
if (!posix_setsid ()) Die ("Could not detach from terminal\n");
Set the message function for 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 ");
Create an external connector socket
$ServerSocket =socket_create (Af_inet, sock_stream,sol_tcp);
Set the IP of the external connection and the Port,ip linked fields bit 0, which means that all interfaces are heard
if (!socket_bind ($ServerSocket, 0, $ServerPort)) die ("Cannot bind socket!\n");
Start to listen to port
if (!socket_listen ($ServerSocket)) Die ("Cannot listen!\n");
Set the socket to Nonblock mode
if (!socket_set_nonblock ($ServerSocket)) Die ("Cannot set Server sockets to block!\n");
Empty the $pid to indicate that there is no child Process
Unset ($PID);
while (true)
{
Enter the pooling mode and check to see if there is a connection every 1 seconds.
Sleep (1);
Check to see if there's a connection.
@ $ConnectedServerSocket =socket_accept ($ServerSocket);
if ($ConnectedServerSocket!==false)
{
Someone came Cola
Start a child process to use the connection
$PID =pcntl_fork ();
if ($PID ==-1) die ("Could not fork");
if ($PID) continue;//This is daemon process, continue to go back to the listener.
Here is the child process beginning
Perform a socket function
Processsocket ($ConnectedServerSocket);
After the socket has been processed, the socket is dropped.
Destroysocket ();
End Child Process
Exit (0);
}
}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/963999.html www.bkjia.com true http://www.bkjia.com/PHPjc/963999.html techarticle PHP Open Multi-Process Method This article describes how PHP opens a multi-process approach. Share to everyone for your reference. The implementation method is as follows: The code is as follows: Php $IP = ' 192.168.1.1 ';//w