Home Backend Development PHP Tutorial Detailed introduction of multi-process echo server in PHPSocket programming (picture and text)

Detailed introduction of multi-process echo server in PHPSocket programming (picture and text)

Sep 13, 2018 pm 05:11 PM
linux php socket

This article brings you a detailed introduction (pictures and text) about the multi-process echo server in PHPSocket programming. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

The last echo service program has a big shortcoming, that is, it can only connect to one client at the same time, which is obviously unreasonable.

So this time we use a multi-process approach to provide services to multiple clients at the same time.

The following is the final effect:

Detailed introduction of multi-process echo server in PHPSocket programming (picture and text)

(The worker process we created can be viewed through the ps command)

Detailed introduction of multi-process echo server in PHPSocket programming (picture and text)

(Open multiple terminals through xshell and connect multiple clients to the service program)

Begin coding Before, let’s introduce what a process is.

The process, that is, the running program that occupies memory, is an independent memory space. For example, when we open Notepad software on Windows, this operation is equivalent to opening a process.

Multi-process is to create multiple processes to complete one thing together.

We only need to implement the server-side program this time, and the client still uses the previous one. If you haven’t read the previous articles, you can check out the historical articles.

In PHP, we can use the pcntl_fork function to create a process.

The following is the prototype of the function:

int pcntl_fork ( void )
Copy after login

Official explanation:

When successful, the PID of the generated child process is returned in the execution thread of the parent process, and the PID of the child process is returned in the execution thread of the child process. Returns 0 within. On failure, -1 is returned in the parent process context, the child process is not created, and a PHP error is raised.

Why is it said to be the thread executed by the parent process? Because a process contains at least one thread, and this thread is the main thread of the process.

When the parent process calls pcntl_fork, it also copies an independent child process. This child process has the same context as the parent process. In other words, the two processes just share the same code.

Enter the encoding process:

Detailed introduction of multi-process echo server in PHPSocket programming (picture and text)

By executing the pcntl_fork function, a child process is copied at the same time. At this time, if the context is The execution environment of the parent process, the return value is the process number of the child process. If it is the execution environment of a child process, 0 is returned. Therefore, in the following if program structure, both branches are executed.

The child process still has the same logic as before, used to receive messages from the client and send them to the client at the same time.

The penultimate line calls the pcntl_waitpid function. Before explaining this function, let us first understand what a zombie process is.

Under normal circumstances, the child process is created through the parent process. Since processes are independent memory structures, the parent process does not know the running status of the child process. After the child process completes its task, it cannot exit by itself. At this time, the parent process needs to obtain the status of the child process through the operating system to recycle the child process. Otherwise, our child process will become a garbage resource, that is, a zombie process.

The following is the function prototype of this function:

int pcntl_waitpid ( int $pid , int &$status [, int $options = 0 ] )
Copy after login

Official explanation:

Wait for or return the child process status of fork.

In fact, there is a problem with the above code. After calling pcntl_waitpid for the first time in foreach, the main process is actually blocked at this time, waiting for the first child process to exit, while other child processes If it exits abnormally at this time, it will not be recycled by the main process, and a zombie process will be generated. In actual development, the main process still has to do other things.

So the non-blocking method is recommended here. It is very simple. Just add the third parameter: WNOHANG. That is: pcntl_waitpid ($pid, $status, WNOHANG)

In this way, it can return immediately without the child process exiting, thus continuing to execute subsequent code.

Related recommendations:

Server-side PHP multi-process programming practice

Server-side PHP multi-process programming practice_PHP tutorial

The above is the detailed content of Detailed introduction of multi-process echo server in PHPSocket programming (picture and text). For more information, please follow other related articles on the PHP Chinese website!

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 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)

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to switch Chinese mode with vscode How to switch Chinese mode with vscode Apr 15, 2025 pm 11:39 PM

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

See all articles