PHP surface the difference between thread and process (incidentally)

Source: Internet
Author: User
Tags php programming sapi

This article introduces the content of the PHP surface of the question one thread and process of the difference (incidentally), has a certain reference value, now share to everyone, the need for friends can refer to

I. What is a process


A process is an instance of program execution that can be allocated to resources such as CPU and memory . Processes typically include instruction sets and system resources , where the instruction set is your code, and system resources refer to CPU, memory, I/O, and so on.

A process is a process of dynamic execution of a program in a data set that can be simply understood as an "executing program,"which is a separate unit of CPU resource allocation and scheduling.
The process is generally composed of three parts: program, data set and process control block . The program we write is used to describe what the process is going to accomplish and how it is done, and the data set is the resource that the program needs to use in its execution, and the process control block is used to document the external characteristics of the process, to describe the process of its execution, and it can be used to control and manage the process, which is the only sign of
The limitation of the process is that the cost of creating, revoking, and switching is relatively high.

Second, what is a thread

A thread is an execution flow of a process, and a thread cannot allocate system resources, it is part of a process, and is a unit of independent operation that is smaller than the process.
Explain: The process has two characteristics: one is the ownership of the resource, one is the dispatch execution (instruction set), the thread is a part of the schedule execution, refers to the process execution process path, also called the program execution flow. Threads are also sometimes called lightweight processes.

A thread is a concept developed after a process. A thread is also called a lightweight process, which is a basic CPU execution unit and the smallest unit in a program's execution, consisting of a thread ID, a program counter, a register collection, and a stack. A process can contain multiple threads.
The advantage of the thread is to reduce the cost of concurrent execution of the program, improve the concurrency of the operating system, the disadvantage is that the thread does not have its own system resources, only the resources necessary at runtime, but the same process of the threads can share the system resources owned by the process, if the process compared to a workshop, Then threads are like workers in a workshop. However, for some exclusive resources there is a locking mechanism, improper handling may result in a "deadlock."

Third, what is the association process

The process is a user-state lightweight thread, also known as micro-threading, English name Coroutine, the scheduling of the process is entirely user-controlled. People usually compare the process and subroutine (function) to understand.
Subroutine calls are always one entry, one return, and the execution of the subroutine is completed once exiting.
The start of the process is the first entry point, and in the process, the return point is followed by the entry point. In Python, the co-process can invoke other threads through yield. The process of transferring execution through yield is not the relationship between the caller and the callee, but is symmetric and equal to each other and accomplishes the task by mutual cooperation. The approximate process for its operation is as follows:
In the first step, the process a begins execution.
In the second step, the process a executes halfway, enters the pause, and transfers the execution to the process B through the yield command.
The third step, (after a period of time), the return of the executive power.
Fourth step, the process a resumes execution.

The feature of the co-process is that it is a thread execution, compared with multithreading, its advantages are as follows:
* The execution efficiency of the process is very high . Because the subroutine switch is not a thread switch, but is controlled by the program itself, therefore, there is no thread switching overhead, and multithreading ratio, the more the number of threads, the performance advantage of the association is more obvious.
* The process does not require a multi-threaded locking mechanism. In the process of controlling shared resources without locking, only need to determine the status of the good.
Tips: The simplest way to take advantage of multicore CPUs is to multi-process + co-processes that take full advantage of multicore and give full play to the high-efficiency of the coprocessor, which can achieve very good performance.

Iv. relationship of processes and threads

The process is like a landlord with land (system resources), and threads are like tenants (threads that perform farming processes). Each landlord (process) has only one tenant (thread) to work with.
Process-the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but the process of switching costs resources, less efficient.
Thread-Program execution of the smallest unit, no independent address space, a thread dead may be the entire process will die, but save resources, switching efficiency.

V. Common processes and threads for PHP programming

1, in the Web application, every time we access PHP, we build a PHP process , of course, will also build at least one PHP thread.
2. PHP uses pcntl for multi-process programming
3. Use pthreads for multithreaded programming in PHP
4, Nginx each process only one thread , each thread can handle the access of multiple clients
5. PHP-FPM uses a multi-process model , with only one thread per process, and each thread can handle only one client access .
6, Apache may use a multi-process model, or the use of multithreaded models, depending on which sapi to use.
7, the process is the smallest unit of CPU resource allocation , the thread is the smallest unit of CPU scheduling

I. What is a process

A process is an instance of program execution that can be allocated to resources such as CPU and memory . Processes typically include instruction sets and system resources , where the instruction set is your code, and system resources refer to CPU, memory, I/O, and so on.

A process is a process of dynamic execution of a program in a data set that can be simply understood as an "executing program,"which is a separate unit of CPU resource allocation and scheduling.
The process is generally composed of three parts: program, data set and process control block . The program we write is used to describe what the process is going to accomplish and how it is done, and the data set is the resource that the program needs to use in its execution, and the process control block is used to document the external characteristics of the process, to describe the process of its execution, and it can be used to control and manage the process, which is the only sign of
The limitation of the process is that the cost of creating, revoking, and switching is relatively high.

Second, what is a thread

A thread is an execution flow of a process, and a thread cannot allocate system resources, it is part of a process, and is a unit of independent operation that is smaller than the process.
Explain: The process has two characteristics: one is the ownership of the resource, one is the dispatch execution (instruction set), the thread is a part of the schedule execution, refers to the process execution process path, also called the program execution flow. Threads are also sometimes called lightweight processes.

A thread is a concept developed after a process. A thread is also called a lightweight process, which is a basic CPU execution unit and the smallest unit in a program's execution, consisting of a thread ID, a program counter, a register collection, and a stack. A process can contain multiple threads.
The advantage of the thread is to reduce the cost of concurrent execution of the program, improve the concurrency of the operating system, the disadvantage is that the thread does not have its own system resources, only the resources necessary at runtime, but the same process of the threads can share the system resources owned by the process, if the process compared to a workshop, Then threads are like workers in a workshop. However, for some exclusive resources there is a locking mechanism, improper handling may result in a "deadlock."

Third, what is the association process

The process is a user-state lightweight thread, also known as micro-threading, English name Coroutine, the scheduling of the process is entirely user-controlled. People usually compare the process and subroutine (function) to understand.
Subroutine calls are always one entry, one return, and the execution of the subroutine is completed once exiting.
The start of the process is the first entry point, and in the process, the return point is followed by the entry point. In Python, the co-process can invoke other threads through yield. The process of transferring execution through yield is not the relationship between the caller and the callee, but is symmetric and equal to each other and accomplishes the task by mutual cooperation. The approximate process for its operation is as follows:
In the first step, the process a begins execution.
In the second step, the process a executes halfway, enters the pause, and transfers the execution to the process B through the yield command.
The third step, (after a period of time), the return of the executive power.
Fourth step, the process a resumes execution.

The feature of the co-process is that it is a thread execution, compared with multithreading, its advantages are as follows:
* The execution efficiency of the process is very high . Because the subroutine switch is not a thread switch, but is controlled by the program itself, therefore, there is no thread switching overhead, and multithreading ratio, the more the number of threads, the performance advantage of the association is more obvious.
* The process does not require a multi-threaded locking mechanism. In the process of controlling shared resources without locking, only need to determine the status of the good.
Tips: The simplest way to take advantage of multicore CPUs is to multi-process + co-processes that take full advantage of multicore and give full play to the high-efficiency of the coprocessor, which can achieve very good performance.

Iv. relationship of processes and threads

The process is like a landlord with land (system resources), and threads are like tenants (threads that perform farming processes). Each landlord (process) has only one tenant (thread) to work with.
Process-the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but the process of switching costs resources, less efficient.
Thread-Program execution of the smallest unit, no independent address space, a thread dead may be the entire process will die, but save resources, switching efficiency.

V. Common processes and threads for PHP programming

1. In a Web application, we build a PHP process every time we access PHP, and of course we build at least one PHP thread.
2, PHP use pcntl for multi-process programming
3, PHP using pthreads for multithreaded Programming
4, Nginx Each process has only one thread , each thread can handle multiple client access
5, PHP-FPM uses the multi-process model , and each process only a thread, each thread can handle only one client access .
6, Apache may use a multi-process model, or it may use a multithreaded model, depending on which type of SAPI is used.
7, the process is the smallest unit of the CPU resource allocation , the thread is the smallest unit of CPU scheduling

Related Article

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.