Home  >  Article  >  Backend Development  >  The difference between php processes and threads

The difference between php processes and threads

(*-*)浩
(*-*)浩Original
2019-09-25 10:22:283545browse

The difference between php processes and threads

Process: In a narrow sense, a process is an instance of a running program. In a broad sense, a process is a program with certain independent functions about a certain data set. Run the activity once. (Recommended learning: PHP programming from entry to proficiency)

It is the basic unit of dynamic execution of the operating system. In the traditional operating system, the process is both the basic allocation unit and the basic execution unit. (A few words summarize: active, entity, running program);

Thread: is called a lightweight process, which is the smallest unit of program execution flow. A thread is the An entity is a basic unit that is independently scheduled and dispatched by the system.

The thread itself basically does not own system resources. It only owns some resources that are essential in operation (such as a program counter, a set of registers and a stack), but it can be shared with other threads belonging to the same process. All resources owned by the process (summarized in a few words: lightweight, entity, process, smallest unit);

Difference:

0) Process It is the smallest unit of CPU resource allocation, and thread is the smallest unit of CPU scheduling

1) In short, a program has at least one process, and a process has at least one thread.

2) The division scale of threads is smaller than that of processes, making multi-threaded programs highly concurrency.

3) In addition, the process has an independent memory unit during execution, and multiple threads share memory, thus greatly improving the running efficiency of the program.

4) There are differences between threads and 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 application program, and the application program provides multiple thread execution control.

5) From a logical point of view, the meaning of multi-threading is that in an application, multiple execution parts can be executed at the same time. However, the operating system does not regard multiple threads as multiple independent applications to implement process scheduling and management and resource allocation. This is the important difference between processes and threads.

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. Specifically, the 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:

1. Memory release is simple and easy to use. In the multi-process model, processes 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.

2. Strong disaster tolerance. With the same problem, extensions or PHP may cause segmentation faults. 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, even if a certain process dies, it will not affect the overall service.

Multiple processes have the advantages of multi-processes, and multi-threads also have the advantages of multi-threads. For example, HHVM chooses the multi-thread 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. Then in HHVM, there is no need to use shared memory. There is another problem with shared memory. It is inconvenient to store complex data structures 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.

The above is the detailed content of The difference between php processes and threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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