Table of Contents
How to start multiple processes in php
Home Backend Development PHP Tutorial How to start multiple processes in php_PHP tutorial

How to start multiple processes in php_PHP tutorial

Jul 13, 2016 am 10:04 AM
php share Example turn on method of process

How to start multiple processes in php

This article explains how to start multiple processes in php. Share it with everyone for your reference. The specific implementation method is as follows:

The code is as follows:


$IP='192.168.1.1';//IP of Windows computer
$Port='5900'; //Port used by VNC
$ServerPort='9999';//Port used by Linux Server externally
$RemoteSocket=false;//Connect to VNC’s Socket
function SignalFunction($Signal){
//This is the message processing function of the main Process
global $PID;//Child Process’s PID
switch ($Signal)
{
case SIGTRAP:
case SIGTERM:
//Receive the Signal to end the program
if($PID)
{
//Send a SIGTERM signal to Child to tell him to finish it quickly
posix_kill($PID,SIGTERM);
//Wait for the Child Process to end to avoid zombie
pcntl_wait($Status);
}
//Close the Socket opened by the main Process
DestroySocket();
exit(0); //End the main Process
break;
case SIGCHLD:
/*
When the Child Process ends, Child will send a SIGCHLD signal to Parent
When Parent receives SIGCHLD, it knows that the Child Process has ended and it should do something
Ending action*/
unset($PID); //Clear $PID to indicate that the Child Process has ended
pcntl_wait($Status); //Avoid Zombie
break;
default:
}
}
function ChildSignalFunction($Signal){
//This is the message processing function of Child Process
switch ($Signal)
{
case SIGTRAP:
case SIGTERM:
//Child Process receives the end message
DestroySocket(); //Close Socket
exit(0); //End Child Process
default:
}
}
function ProcessSocket($ConnectedServerSocket){
//Child Process Socket processing function
//$ConnectedServerSocket -> Externally connected Socket
global $ServerSocket,$RemoteSocket,$IP,$Port;
$ServerSocket=$ConnectedServerSocket;
declare(ticks = 1); //This line must be added, otherwise the message processing function cannot be set.
//Set message processing function
if(!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;
if(!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;
//Establish a Socket connected to VNC
$RemoteSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//Connect to the internal VNC
@$RemoteConnected=socket_connect($RemoteSocket,$IP,$Port);
if(!$RemoteConnected) return; //Unable to connect to VNC end
//Set Socket processing to Nonblock to prevent the program from being blocked
if(!socket_set_nonblock($RemoteSocket)) return;
if(!socket_set_nonblock($ServerSocket)) return;
while(true)
{
//Here we use pooling to obtain data
$NoRecvData=false; //This variable is used to determine whether the external connection has read data
$NoRemoteRecvData=false;//This variable is used to determine whether the VNC connection has read data
@$RecvData=socket_read($ServerSocket,4096,PHP_BINARY_READ);
//Read 4096 bytes of data from external connection
@$RemoteRecvData=socket_read($RemoteSocket,4096,PHP_BINARY_READ);
//Read 4096 bytes of data from the vnc connection
if($RemoteRecvData==='')
{
//VNC connection is interrupted, it’s time to end
echo"Remote Connection Closen";
return;
}
if($RemoteRecvData===false)
{
/*
Since we are using nonblobk mode
The situation here is that the vnc connection has no data to read
*/
$NoRemoteRecvData=true;
//Clear Last Error
socket_clear_error($RemoteSocket);
}
if($RecvData==='')
{
//The external connection is interrupted, it’s time to end
echo "Client Connection Closen";
return;
}
if($RecvData===false)
{
/*
Since we are using nonblobk mode
The situation here is that the external connection has no data to read
*/
$NoRecvData=true;
//Clear Last Error
socket_clear_error($ServerSocket);
}
if($NoRecvData&&$NoRemoteRecvData)
{
//If neither the external connection nor the VNC connection has data to read,
//Let the program sleep for 0.1 seconds to avoid long-term use of CPU resources
usleep(100000);
//After waking up, continue the pooling action to read the socket
continue;
}
//Recv Data
if(!$NoRecvData)
{
//External connection reads data
while(true)
{
//Transfer the data read from the external connection to the VNC connection
@$WriteLen=socket_write($RemoteSocket,$RecvData);
if($WriteLen===false)
{
//Due to network transmission problems, data cannot be written at the moment
//Sleep for 0.1 seconds before trying again.
usleep(100000);
continue;
}
if($WriteLen===0)
{
//The remote connection is interrupted, the program should end
echo"Remote Write Connection Closen";
return;
}
//When the data read from the external connection has been completely sent to the VNC connection, this loop is interrupted.
if($WriteLen==strlen($RecvData)) break;
//If the data cannot be sent in one go, it will have to be split into several transmissions until all the data is sent
$RecvData=substr($RecvData,$WriteLen);
}
}
if(!$NoRemoteRecvData)
{
//Here is the data read from the VNC connection and then transferred back to the external connection
//The principle is almost the same as above and I won’t go into details
while(true)
{
@$WriteLen=socket_write($ServerSocket,$RemoteRecvData);
if($WriteLen===false)
{
usleep(100000);
continue;
}
if($WriteLen===0)
{
echo"Remote Write Connection Closen";
return;
}
if($WriteLen==strlen($RemoteRecvData)) break;
$RemoteRecvData=substr($RemoteRecvData,$WriteLen);
}
}
}
}
function DestroySocket(){
//Used to close the opened Socket
global$ServerSocket,$RemoteSocket;
if($RemoteSocket)
{
//If VNC connection has been enabled
//You must shut down the Socket before closing the Socket, otherwise the other party will not know that you have closed the connection
@socket_shutdown($RemoteSocket,2);
socket_clear_error($RemoteSocket);
//Close Socket
socket_close($RemoteSocket);
}
//Close external connections
@socket_shutdown($ServerSocket,2);
socket_clear_error($ServerSocket);
socket_close($ServerSocket);
}
//This is the beginning of the entire program, and the program starts executing from here
//First execute a fork
here $PID=pcntl_fork();
if($PID==-1) die("could not fork");
//If $PID is not 0, it means this is a Parent Process
//$PID is Child Process
//This is the Parent Process. End it yourself and let Child become a Daemon.
if($PID) die("Daemon PID:$PIDn");
//From here, Daemon mode is executed
//Detach the current Process from the terminal and enter daemon mode
if(!posix_setsid()) die("could not detach from terminaln");
//Set the daemon's message 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");
//Establish a Socket for external connection
$ServerSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//Set the IP and Port for external connection monitoring. Set the IP field to 0, which means listening to the IPs of all interfaces
if(!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!n");
//Start listening to Port
if(!socket_listen($ServerSocket)) die("Cannot Listen!n");
//Set Socket to nonblock mode
if(!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!n");
//Clear the $PID variable, indicating that there is currently no Child Process
unset($PID);
while(true)
{
//Enter pooling mode and check whether there is a connection coming in every 1 second.
sleep(1);
//Check if there is a connection coming in
@$ConnectedServerSocket=socket_accept($ServerSocket);
if($ConnectedServerSocket!==false)
{
//Someone is coming in
//Start a Child Process to handle connections
$PID=pcntl_fork();
if($PID==-1) die("could not fork");
if($PID) continue;//This is the daemon process, continue to monitor.
//Here is the start of Child Process
//Execute the function in Socket
ProcessSocket($ConnectedServerSocket);
//After processing the Socket, end the Socket
DestroySocket();
//End Child Process
exit(0);
}
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963999.htmlTechArticleHow to start multiple processes in php. This article describes how to start multiple processes in php. Share it with everyone for your reference. The specific implementation method is as follows: The code is as follows: ?php $IP='192.168.1.1';//W...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles