


PHP interview question 1: The difference between threads and processes (by the way, coroutines are mentioned)
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:
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:
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
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

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.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

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.
