PHP interview question 1: The difference between threads and processes (by the way, coroutines are mentioned)

不言
Release: 2023-03-24 09:14:02
Original
5835 people have browsed it

The content of this article is about the difference between threads and processes (incidentally mentioning coroutines) in PHP interview question 1. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1. What is a process


A process is an instance of program execution. A process can be allocated to cpu and memory and other resources. The process generally includes instruction set and system resources, where the instruction set is your code, and system resources refer to CPU, memory, I/O, etc.

A process is a dynamic execution process of a program in a data set. It can be simply understood as "the executing program". It is an independent unit of CPU resource allocation and scheduling.
The process generally consists of program, data set, and process control block. The program we write is used to describe what functions the process needs to complete and how to complete it; the data set is the resources that the program needs to use during execution; the process control block is used to record the external characteristics of the process, describe the execution change process of the process, and the system It can be used to control and manage processes. It is the only sign that the system senses the existence of a process.
The limitation of the process is that the overhead of creation, cancellation and switching is relatively high.

2. What is a thread

A thread is an execution stream of a process. A thread cannot allocate system resources. It is a part of the process and is a smaller unit that runs independently than the process.
Explain: The process has two characteristics: one is the ownership of the resource, and the other is the scheduling execution (instruction set). The thread is part of the scheduling execution, which refers to the path of the process execution process, also called the program execution flow. Threads are sometimes called lightweight processes.

Threads are a concept developed after processes. A thread is also called a lightweight process. It is a basic CPU execution unit and the smallest unit in the program execution process. It is composed of thread ID, program counter, register set and stack. A process can contain multiple threads. The advantage of threads is that it reduces the overhead of concurrent execution of programs and improves the concurrency performance of the operating system. The disadvantage is that threads do not have their own system resources and only have resources that are essential during runtime, but each thread in the same process Threads can share the system resources owned by the process. If the process is compared to a workshop, then the threads are like workers in the workshop. However, there is a lock mechanism for some exclusive resources, and improper handling may cause "deadlock".
3. What is a coroutine?

A coroutine is a lightweight thread in user mode, also known as a micro-thread. The English name is Coroutine. The scheduling of the coroutine is completely controlled by the user. People usually compare

coroutines and subroutines

(functions). A subroutine call always has an entrance and returns once. Once exited, the execution of the subroutine is completed. The starting point of the coroutine is the first entry point. In the coroutine, after the return point is the next entry point. In python, coroutines can call other coroutines through yield. The relationship between the coroutines that transfer execution rights through yield is not the relationship between the caller and the callee, but is symmetrical and equal to each other, and they work together to complete the task through mutual cooperation. The general process of its operation is as follows:
In the first step, coroutine A starts to execute.
In the second step, coroutine A is executed halfway and enters a pause. The execution right is transferred to coroutine B through the yield command.
The third step, (after a period of time) Coroutine B returns the execution rights.
The fourth step is to resume execution of coroutine A.

The characteristic of coroutine is that it is executed in one thread. Compared with multi-threading, its advantages are reflected in:

* The

execution efficiency of coroutine is very high
. Because subroutine switching is not thread switching, but is controlled by the program itself, there is no overhead of thread switching. Compared with multi-threading, the greater the number of threads, the more obvious the performance advantages of coroutines. * Coroutines do not require multi-threaded locking mechanisms. There is no need to lock the shared resources in the coroutine, you only need to determine the status. Tips: The simplest way to use multi-core CPU is to use multi-process coroutines, which not only makes full use of multi-cores, but also gives full play to the high efficiency of coroutines, and can achieve extremely high performance.

4. The relationship between processes and threads

The process is like a landlord, with land (system resources), and the thread is like a tenant (thread, executing the farming process). Each landlord (process) only needs one working tenant (thread).

Process - the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but switching processes consumes resources and is less efficient.

Thread - the smallest unit of program execution. There is no independent address space. If one thread dies, the entire process may die, but it saves resources and has high switching efficiency.

5. Common processes and threads in PHP programming

1. In web applications, we create a PHP process every time we access PHP, and of course we will also create at least one PHP thread.
2. PHP uses pcntl for multi-process programming.
3. PHP uses pthreads for multi-thread programming.
4. nginx’s per Each process has only one thread, and each thread can handle the access of multiple clients
5. php-fpm uses the multi-process model, each processThere is only one thread, and each thread can only handle one client access .
6. Apache may use a multi-process model or a multi-thread model, depending on which SAPI is used.
7. The process is the smallest unit of cpu resource allocation, and the thread is The smallest unit of cpu scheduling

1. What is a process

A process is an instance of program execution. A process can be assigned to cpu and Memory and other resources. The process generally includes instruction set and system resources, where the instruction set is your code, and system resources refer to CPU, memory, I/O, etc.

A process is a dynamic execution process of a program in a data set. It can be simply understood as "the executing program". It is an independent unit of CPU resource allocation and scheduling.
The process generally consists of program, data set, and process control block. The program we write is used to describe what functions the process needs to complete and how to complete it; the data set is the resources that the program needs to use during execution; the process control block is used to record the external characteristics of the process, describe the execution change process of the process, and the system It can be used to control and manage processes. It is the only sign that the system senses the existence of a process.
The limitation of the process is that the overhead of creation, cancellation and switching is relatively high.

2. What is a thread

A thread is an execution stream of a process. A thread cannot allocate system resources. It is a part of the process and is a smaller unit that runs independently than the process.
Explain: The process has two characteristics: one is the ownership of the resource, and the other is the scheduling execution (instruction set). The thread is part of the scheduling execution, which refers to the path of the process execution process, also called the program execution flow. Threads are sometimes called lightweight processes.

Threads are a concept developed after processes. A thread is also called a lightweight process. It is a basic CPU execution unit and the smallest unit in the program execution process. It is composed of thread ID, program counter, register set and stack. A process can contain multiple threads. The advantage of threads is that it reduces the overhead of concurrent execution of programs and improves the concurrency performance of the operating system. The disadvantage is that threads do not have their own system resources and only have resources that are essential during runtime, but each thread in the same process Threads can share the system resources owned by the process. If the process is compared to a workshop, then the threads are like workers in the workshop. However, there is a lock mechanism for some exclusive resources, and improper handling may cause "deadlock".
3. What is a coroutine?

A coroutine is a lightweight thread in user mode, also known as a micro-thread. The English name is Coroutine. The scheduling of the coroutine is completely controlled by the user. People usually compare

coroutines and subroutines

(functions). A subroutine call always has an entrance and returns once. Once exited, the execution of the subroutine is completed. The starting point of the coroutine is the first entry point. In the coroutine, after the return point is the next entry point. In python, coroutines can call other coroutines through yield. The relationship between the coroutines that transfer execution rights through yield is not the relationship between the caller and the callee, but is symmetrical and equal to each other, and they work together to complete the task through mutual cooperation. The general process of its operation is as follows:
In the first step, coroutine A starts to execute.
In the second step, coroutine A is executed halfway and enters a pause. The execution right is transferred to coroutine B through the yield command.
The third step, (after a period of time) Coroutine B returns the execution rights.
The fourth step is to resume execution of coroutine A.

The characteristic of coroutine is that it is executed in one thread. Compared with multi-threading, its advantages are reflected in:

* The

execution efficiency of coroutine is very high
. Because subroutine switching is not thread switching, but is controlled by the program itself, there is no overhead of thread switching. Compared with multi-threading, the greater the number of threads, the more obvious the performance advantages of coroutines. * Coroutines do not require multi-threaded locking mechanisms. There is no need to lock the shared resources in the coroutine, you only need to determine the status. Tips: The simplest way to use multi-core CPU is to use multi-process coroutines, which not only makes full use of multi-cores, but also gives full play to the high efficiency of coroutines, and can achieve extremely high performance.

4. The relationship between processes and threads

The process is like a landlord, with land (system resources), and the thread is like a tenant (thread, executing the farming process). Each landlord (process) only needs one working tenant (thread).
Process - the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but switching processes consumes resources and is less efficient.
Thread - the smallest unit of program execution. There is no independent address space. If one thread dies, the entire process may die, but it saves resources and has high switching efficiency.

5. Common processes and threads in PHP programming

1. In web applications, wecreate a PHP processevery time we access PHP, and of course we will also create At least one PHP thread.
2. PHP uses pcntl for multi-process programming.
3. PHP uses pthreads for multi-thread programming.
4. nginx’s per Each process has only one thread, and each thread can handle the access of multiple clients
5. php-fpm uses the multi-process model, each processThere is only one thread, and each thread can only handle one client access .
6. Apache may use a multi-process model or a multi-thread model, depending on which SAPI is used.
7. The process is the smallest unit of cpu resource allocation, and the thread is ## The smallest unit of #cpu scheduling

Related recommendations:

Summary of technical questions that may be asked in php interviews


The above is the detailed content of PHP interview question 1: The difference between threads and processes (by the way, coroutines are mentioned). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!