The difference between process and thread: a simple and difficult problem

The difference between process and thread: a simple and difficult problem

If you simply remember, the process is the smallest unit of resources allocated by the operating system, and the thread is the smallest unit of CPU scheduling . In fact, it is very difficult to win the favor of the interviewer, because the interviewer asks 100 people, 90 people will say this sentence, which does not show the difference , so this article starts from the operating system principle and details the difference between process and thread After that, maybe your answers to the questions may have a new understanding. You can collect it first and then watch it.

What is process execution

A program is a set of instructions that is stored statically on disk. When a program is executed, the operating system loads it into memory to form a logical instance, that is, a process , and allocates required resources including CPU, files, and network handles. So it can also be concluded that the process is the smallest unit of resources allocated by the operating system. The image structure of the process is shown below.

The execution speed of the CPU far exceeds that of disk and memory IO. Therefore, the general CPU execution uses the time slice rotation strategy. After a process executes its acquired CPU time, it needs to be switched and wait for the next CPU scheduling.

When the resources required by the process are ready, you can wait for the CPU scheduling, in addition to the CPU, other resources owned by the process constitute the execution environment of the process, which is the process context. After a process executes its acquired CPU time, it needs to be switched. Before switching, it needs to save its context and wait for the next time the CPU is lucky.

What is thread

At the same time, the process can only perform one task (when reading file A, it cannot read file B at the same time), which is in line with the environment of a computer's single-core CPU. But with the development of technology, the industry began to equip computers with multi-core CPUs, so in order to make full use of the performance of the CPU, the operating system introduced threads. A process can create multiple threads, each thread has a private stack, program counter, etc. However, the resources of the parent process can be shared between threads, so it is necessary to control the way of thread synchronization (such as locks in java).

Process execution is actually a thread in the process that runs after acquiring CPU time, so the thread is the smallest unit of CPU scheduling. A process can create multiple threads, and a multi-core CPU can schedule multiple threads at the same time, so a process can execute multiple tasks concurrently.

The difference between process and thread

Through the above introduction, you can easily answer this question.

  • Processes are independent of each other and do not affect each other. Threads have their own private resources (stack, program counter, etc.), but share the resources of the parent process (memory, files, network handles, etc.) with other threads.
  • A process can create multiple threads, and threads cannot exist independently of the process.
  • Creating a thread consumes less resources than a process.
  • Process is the smallest unit of resources allocated by the operating system, and thread is the smallest unit of CPU scheduling
Published 201 original articles · praised 601 · 160,000 views +

Guess you like

Origin blog.csdn.net/zycxnanwang/article/details/105626093