


Detailed explanation of PHP thread processes and concurrent instances
Process
What is a process? A process is an executing program; a process is an instance of a program being executed on a computer; a process is an entity that can be assigned to and executed by a processor. A process generally includes an instruction set and a system resource set. The instruction set here refers to the program code, and the system resource set here refers to I/O, CPU, memory, etc. To sum up, we can also understand that a process is a running activity of a program with certain independent functions on a certain data collection. A process is an independent unit for resource allocation and scheduling in the system.
When a process is executed, the process can be uniquely represented, consisting of the following elements:
Process descriptor: the unique identifier of the process, used to Other processes are distinguished. It is called process ID in Linux and is generated during the system call fork. However, what we return through getpid is not its pid field, but its thread group number tgid.
Process status: The states we often refer to as suspend, running, etc., represent the current state.
Priority: related to execution scheduling between processes, relative to other processes.
Program counter: The address of the next instruction to be executed in the program. This address is the memory address in the kernel or user memory space.
Memory pointers: including pointers to program code and process-related data, as well as pointers to shared memory blocks with other processes.
Context data: Data in the processor's registers when the process is executed.
I/O status information: including explicit I/O requests, I/O devices assigned to processes, etc.
Accounting Information: It may include the total processor time, the total number of clocks used, time limits, etc.
The above elements will be placed in a data structure called the process control block. The process control block is a structure that enables the operating system to support multiple processes and provide multiprocessing. When the operating system switches processes, it performs two steps: one is to interrupt the process in the current processor, and the other is to execute the next process. Regardless of interrupt or execution, the program counter, context data, and process state in the process control block will change. When the process is interrupted, the operating system will save the program counter and processor register (corresponding to the context data in the process control block) to the corresponding location in the process control block. The process state will also change, and it may enter a blocking state, or it may enter a blocking state. Enter the ready state. When executing the next process, the operating system sets the next process to the running state according to rules and loads the program context data and program counter of the process to be executed.
Threads
A process has two characteristic parts: resource ownership and scheduling execution. Resource ownership means that the process includes the memory space, I/O and other resources required for the process to run. Scheduling execution refers to the execution path in the middle of the process execution, or the instruction execution flow of the program. These two feature parts can be separated. After separation, the one with data ownership is usually called a process, and the one with the dispatchable part of the execution code is called a thread or lightweight process.
Threads have the meaning of "execution clues", and the process is defined as the resource owner in a multi-threaded environment, and it will still store the process control block of the process. The structure of threads is different from that of processes. Each thread includes:
Thread status: The current status of the thread.
An execution stack
Private data area: Static storage space for local variables of each thread
Register Set: Stores some state of the processor
Each process has a process control block and user address space, and each thread has a Independent stacks and independent control blocks have their own independent execution context. Its structure is shown in Figure 8.1.
Figure 8.1 Process model diagram
Threads are somewhat different from processes during execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must exist in the process, and the process provides execution control of multiple threads. From a logical point of view, the meaning of multi-threading is that in a process, multiple execution parts can be executed at the same time. At this time, the process itself is not the basic running unit, but the container of threads.
The advantage of threads over processes is that they are faster. Whether it is creating a new thread or terminating a thread; whether it is switching between threads or sharing data or communication between threads, its speed is faster than that of a process. Big advantage.
Concurrency and parallelism
Concurrency, also known as co-concurrency, refers to the ability to handle multiple simultaneous activities. Concurrent events do not necessarily have to be the same happens all the time. For example, modern computer systems can load multiple programs into memory in the form of processes at the same time, and use time-division multiplexing of the processor to present the feeling of running simultaneously on one processor.
Parallel refers to two concurrent events that occur at the same time, which has the meaning of concurrency, but concurrency is not necessarily parallel.
The difference between concurrency and parallelism is that a processor processes multiple tasks at the same time and multiple processors or multi-core processors process multiple different tasks at the same time. The former is logical simultaneous, while the latter is physical simultaneity.
Various concurrency models of PHP
Since there are two models, which one does PHP use? The answer is that they all are supported, which means that PHP supports the multi-threaded model. In multi-threaded situations, the problems of resource sharing and isolation usually need to be solved. PHP itself is thread-safe.
The specific model depends on which SAPI is used. For example, in Apache, then the multi-thread model or the multi-process model may be used. And php-fpm uses the multi-process model.
The currently recommended way is to use the php-fpm model, because this model has many advantages for PHP:
Memory release is simple, When using the multi-process model, the process can easily release memory by exiting. Since PHP has many extensions, memory leaks may occur if you are not careful. FPM solves the problem simply by exiting the process.
Strong disaster recovery capability. With the same problem, extensions or PHP may cause segfaults. If it is a single-process multi-thread model, then the entire PHP will hang. This will affect the service. If there are multiple processes, the death of a certain process will not affect the overall service.
Multiple processes have the advantages of multiple processes, and multi-threading also has the advantages of multi-threading. For example, HHVM chooses the multi-threading model. The biggest benefit of the multi-threading model is the convenience of information sharing and communication, because pointers can be used directly in the same process space.
For example, the opcode cache tool. In PHP, apc, opcache, etc. use shared memory to share opcode. In HHVM, there is no need to use shared memory. Another problem with shared memory is that the storage is complicated. Data structures are inconvenient because of pointer issues. Data structures in C/C++ can be shared in multi-threaded situations. This is also helpful for improving efficiency.
There is another obvious model difference between multi-process and multi-thread: the logic when processing requests.
In the case of multiple processes, it is difficult to transfer the FD connection across processes. So many processes usually use listen()
in the parent process, and then accept()
in each child process to achieve load balancing. There may be a herd problem under such a model.
Under the multi-threading model, an independent thread can be used to accept the request and then dispatch it to each worker thread.
Related recommendations:
Detailed explanation of single-threaded JS execution issues
PHP’s support and use of multi-threaded programming
A case of implementing PHP multi-threading class
The above is the detailed content of Detailed explanation of PHP thread processes and concurrent instances. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
