PHP code sharing: Open the multi-process PHP login Interface code PHP code codes PHP Registration page code

Source: Internet
Author: User
Tags usleep
Here to share a process of opening the PHP code, not more explanations, are in the comments.

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:

  Externally connected socket 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;//establish a connector to the VNC socket $RemoteSocket =socket_create (af_inet, SOCK_STREAM,SOL_TCP);//Connect to the internal VNC @ $RemoteC if (! $RemoteConnected) return; No connection to VNC end//The socket is set to Nonblock to avoid the program being blocked if (!socket_set_nonblock ($RemoteSocket)) return; if (!socket_set_nonblock ($ServerSocket)) return;   while (true) {//This side we use pooling way to obtain information $NoRecvData =false; This variable is used to determine if the external connection has 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 from external connections @ $RemoteRecvData =socket_read ($RemoteSocket, 4096,php_binary_read);//   Read 4096 bytes's information from the VNC connection ($RemoteRecvData = = =) {//vnc, the end cola echo "Remote Connection close\n";     Return } if ($RemoteRecvData ===false) {/* Since we are using NONBLOBK mode here is VNThe C connection does not have the information to read */$NoRemoteRecvData =true;//Remove the last Errror socket_clear_error ($RemoteSocket);   } if ($RecvData = = =) {//external connection interrupted, the end bundle cola echo "Client Connection close\n";  Return if ($RecvData ===false) {/* Since we are using NONBLOBK mode this is where the external connection does not have the information to read */$NoRecvData =true;//erase last Errror socket_clear_e  Rror ($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 usleep (100000);  /After waking up, continue to make pooling movements to read the socket continue; }//recv Data if (! $NoRecvData) {//external connection read to data while (true) {////external connection read information, transferred to VNC connection @ $WriteLen =socket_write ($RemoteS    Ocket, $RecvData);     if ($WriteLen ===false) {//due to the problem of Internet transport, currently can not be written into the information//sleep for 0.1 seconds before continuing to try.     Usleep (100000);    Continue     } if ($WriteLen ===0) {//Remote connection is interrupted, the program should end up with the echo "Remote Write Connection close\n";    Return    The information}//from the external connection has been completely sent to the VNC connection, which interrupts this loop.   if ($WriteLen ==strlen ($RecvData)) break;//If the information can be sent in a few times after the delivery, until all the information is sent out for $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 (true) {@ $WriteLen =socket_write ($ServerSocket, $RemoteRec    VData);     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 () {//used to close the opened socket global$serversocket, $RemoteSocket; if ($RemoteSocket) {//If the VNC connection has been opened  The socket must be shutdown before the close socket is not known that you have closed the connection @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); }//here is the beginning of the whole program, and the program starts from here.//This first performs a fork $PID =pcntl_fork (); if ($PID ==-1) die ("Could not fork");//If $pid is not 0 means that this is parrent process//$PID is child process//This is the parrent Process itself, Let child become a daemon. if ($PID) die ("Daemon PID: $PID\ n ");//Starting from this, the daemon mode is executed//the current process and the terminal are detached into daemon mode if (!posix_setsid ()) Die (" Could not detach from terminal\n ") ;//Set Daemon's message 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 ");//Establish external connection Socket $ServerSocket =socket_create (af_inet, sock_stream,sol_tcp);//Set External monitoring IP and Port,ip linked fields bit 0, Indicates that the IP if (!socket_bind ($ServerSocket, 0, $ServerPort)) die ("Cannot bind socket!\n") that listens to all interfaces, and/or starts to listen to port if (!socket_listen ($ServerSocket)) Die ("Cannot listen!\n");//sets the socket to Nonblock mode if (!socket_set_nonblock ($ServerSocket)) Die ("Cannot set Server socket to block!\n ");//emptying $pid, indicating that there is no child Process unset ($PID);  while (true) {//Enter pooling mode and check for any connections every 1 seconds.   Sleep (1);//Check to see if there is a connection at @ $C if ($ConnectedServerSocket!==false) {//a child process is connected to cola//start with a connection $PID =pcntl_fork ();   if ($PID ==-1) die ("Could not fork"); if ($PID) continue;//This is daemon process,Go back and listen.  Here is the child process start//Execute socket function processsocket ($ConnectedServerSocket);  After the socket is finished, the socket Destroysocket () is dropped;  End Child Process exit (0); } }

The above is the way to open a multi-process PHP, I hope to help you.

The above describes the PHP code sharing: Open the multi-process, including the content of the PHP code, I hope the PHP tutorial interested in a friend helpful.

  • 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.