PHP Open Multi-process method _php Tips

Source: Internet
Author: User
Tags strlen usleep

The example in this article describes the way PHP opens multiple processes. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:

<?php
$IP = ' 192.168.1.1 ';//windows IP of the computer
$Port = ' 5900 '; The port used by VNC
$ServerPort = ' 9999 ';//linux server to use port
$RemoteSocket =false;//connector to VNC socket
function Signalfunction ($Signal) {
This is the information processing function of 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 the child and tell him to finish off the 嘍.
Posix_kill ($PID, sigterm);
Wait for child process to end, avoid zombie
Pcntl_wait ($Status);
}
Closes the socket that the main process opens
Destroysocket ();
Exit (0); The end of the main process
Break
Case SIGCHLD:
/*
When the child process is finished, the child sends a SIGCHLD message to Parrent
When Parrent received SIGCHLD, he knew that the child process had been finished 嘍
The end of the motion
Unset ($PID); Clears the $pid, indicating that the child process has been closed
Pcntl_wait ($Status); Avoid Zombie
Break
Default
}
}
function Childsignalfunction ($Signal) {
This is the message processing function for the child process.
Switch ($Signal)
{
Case SIGTRAP:
Case Sigterm:
The child process receives the end message.
Destroysocket (); About shutting down the socket
Exit (0); End Child Process
Default
}
}
function Processsocket ($ConnectedServerSocket) {
Child Process Socket processing function
$ConnectedServerSocket-> External socket
Global $ServerSocket, $RemoteSocket, $IP, $Port;
$ServerSocket = $ConnectedServerSocket;
DECLARE (ticks = 1); This line must be added, otherwise there is no way to set the information processing function.
Set the information processing function
if (!pcntl_signal (Sigterm, "childsignalfunction")) return;
if (!pcntl_signal (Sigtrap, "childsignalfunction")) return;
Create a socket that connects to VNC
$RemoteSocket =socket_create (Af_inet, sock_stream,sol_tcp);
VNC connected to the inside
@ $RemoteConnected =socket_connect ($RemoteSocket, $IP, $Port);
if (! $RemoteConnected) return; 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 is where we use the pooling method to get the information.
$NoRecvData =false; This variable is used to determine whether or not the external link has read data
$NoRemoteRecvData =false;//This variable to determine if the VNC link has read data
@ $RecvData =socket_read ($ServerSocket, 4096,php_binary_read);
Read 4096 bytes from an external link
@ $RemoteRecvData =socket_read ($RemoteSocket, 4096,php_binary_read);
Read 4096 bytes from the VNC link
if ($RemoteRecvData = = = ")
{
The VNC connection is interrupted, and the end 嘍
echo "Remote Connection close\n";
Return
}
if ($RemoteRecvData ===false)
{
/*
Since we are using the NONBLOBK model
The situation here is that the VNC link has no data to read.
*/
$NoRemoteRecvData =true;
Clear off last Errror
Socket_clear_error ($RemoteSocket);
}
if ($RecvData = = = ")
{
The external connection is interrupted by the end of the 嘍
echo "Client Connection close\n";
Return
}
if ($RecvData ===false)
{
/*
Since we are using the NONBLOBK model
The situation here is that there is no data available for external connections
*/
$NoRecvData =true;
Clear off last Errror
Socket_clear_error ($ServerSocket);
}
if ($NoRecvData && $NoRemoteRecvData)
{
If there is no information available for external connections and VNC connections,
Let the program sleep for 0.1 seconds to avoid long term CPU resource
Usleep (100000);
After waking up, continue to do the pooling to read and draw the socket
Continue
}
Recv Data
if (! $NoRecvData)
{
External connections to access data
while (true)
{
To transfer the data from the external link to the VNC connector
@ $WriteLen =socket_write ($RemoteSocket, $RecvData);
if ($WriteLen ===false)
{
Due to the problem of network transport, there is no way to write data at this time.
Go to sleep for 0.1 seconds and then continue to try.
Usleep (100000);
Continue
}
if ($WriteLen ===0)
{
The end of the remote line, the program is over.
echo "Remote Write Connection close\n";
Return
}
The data that is read from an external connection has been completely sent to the VNC link, which interrupts this loop.
if ($WriteLen ==strlen ($RecvData)) break;
If the data is not delivered at once, it will have to be sent down several times until all the data is sent out.
$RecvData =substr ($RecvData, $WriteLen);
}
}
if (! $NoRemoteRecvData)
{
This is the data that is read from the VNC link and then transferred back to the external connection.
The principle is almost no longer 贅 with the 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 connector
You must shutdown the socket before the close socket or you don't know you've closed the line.
@socket_shutdown ($RemoteSocket, 2);
Socket_clear_error ($RemoteSocket);
About shutting down the socket
Socket_close ($RemoteSocket);
}
Close external connections
@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 is not 0, that's parrent Process.
$PID is the child Process.
This is the parrent Process itself, and let the child become a daemon.
if ($PID) die ("Daemon PID: $PID \ n");
Starting here, the daemon mode is being executed.
To leave the current process and end machine as daemon mode
if (!posix_setsid ()) Die ("Could not detach from terminal\n");
Set the Daemon information processing function
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 ");
To establish a socket for external connections
$ServerSocket =socket_create (Af_inet, sock_stream,sol_tcp);
Set the IP for external connections and the Port,ip 0, which means that you listen to all the interface IP
if (!socket_bind ($ServerSocket, 0, $ServerPort)) die ("Cannot bind socket!\n");
Start listening 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 socket to block!\n");
Empty the $pid variable to indicate that there are no child Process
Unset ($PID);
while (true)
{
Go into the pooling mode and check every 1 seconds for any connections.
Sleep (1);
Check if there's a link coming in.
@ $ConnectedServerSocket =socket_accept ($ServerSocket);
if ($ConnectedServerSocket!==false)
{
Someone connected to 嘍
Start a child process to work with the connection
$PID =pcntl_fork ();
if ($PID ==-1) die ("Could not fork");
if ($PID) continue;//This is daemon process, continue to listen.
This is where the child process begins
Execute socket function
Processsocket ($ConnectedServerSocket);
After the socket is finished, the socket is ended
Destroysocket ();
End Child Process
Exit (0);
}
}

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.