This time, really understand the process and thread



As a software engineer, process and thread should be our necessary knowledge, which can be seen from the interview questions of major enterprises every year!

The difference between process and thread

This semester, I learned the course of operating system, and the biggest achievement is that I learned the “must test”.

At the beginning, I felt that I had learned. When I wrote this article, I felt that there were many things involved here.

operating system

What is an operating system?

When it comes to the operating system, we are all familiar with it. The mobile phone we use is built-inAndroid/iOSOperating system, my development computer,Mac OSLinuxWindowsEtc.

This time, really understand the process and thread

Unofficial description of operating system: operating system is the system software that manages computer hardware and software resources.

This time, really understand the process and thread

History of development

To really understand processes and threads, we need to understand the history of computer operating systems.

Manual operation stage

At the very beginning, computers had no operating system, only separate machines, like the following:

This time, really understand the process and thread

In the eyes of a modern man: without an operating system, how can this machine be used? Anyway, give me a bunch of hardware. I can’t let these hardware run.

The former people were also very powerful. They use cards that record programs and data (cards used in the early stage, later gradually developed to use punched paper tape) to operate the machine.

After the program is read into the machine, the machine starts to work until the program stops. It is said that Turing can operate in this way very skillfullyManchester Mark IMachine.


  1. User exclusive
  2. CPUFast speed, waiting for manual operation, low resource utilization

Batch operating system

In order to solve the problem that the speed of manual operation is much slower thanCPUThe offline I / O system is introduced.

The computer can automatically and batch process one or more users’ jobs, which does not need manual input, which is fast.

Single channel batch operating system

Only one job is loaded to execute in memory at a time.

batch processing system

Each time multiple jobs are loaded into memory and executed concurrently, each job uses the processor and other system resources in turn, and finally completes in turn.

Multiple independent programs are stored in the memory at the same time. These programs share system resources and execute alternately on the CPU under the control of the operating system.

We don’t need to know about the future development of the operating system. It’s enough to know about it.

Processes and threads

Concurrent program execution

When a program is executed concurrently, many programs share system resources. At this time, the running of the program loses its closeness.

When a program is executed concurrently, many programs share all kinds of resources in the system, so the state of these resources will be changed by many programs, resulting in the loss of the closeness of the program. In this way, when a program is executed, it will inevitably be affected by other programs.

When multiple programs are executed concurrently, we expect to run faster and increase system throughput.

This time, really understand the process and thread

However, as shown in the figure, some programs can only be executed after some programs are executed. We cannot let this program execute as soon as it wants. We need to control it.

In order to control more conveniently, we introduce the concept of process.


In the early days, process is the basic unit of resource allocation and scheduling.

By introducing the process, we found that the above problems have been solved, and we canI1I2And so on, we establish the process, and constrain the process execution according to the previous trend chart of our ideas, so as to achieve the expected effect.

Then the program schedules our to achieve the effect of concurrent execution.

This time, really understand the process and thread

And how to control itPVOperation, semaphore, process synchronization and mutual exclusion are beyond the scope of this article. You can learn by yourself.

Insufficient process

Many years later, people found that the original process was designed with some shortcomings.

There are two characteristics of the original process:

  1. The basic unit of resource allocation.
  2. Basic unit of dispatch.

Therefore, in a process only system, to achieve concurrency, you need to do the following:

  1. Create process: create processPCB, and assign resources to it.
  2. Undo process: recycle system resources, undoPCB
  3. Process switching: switching from one process environment to another.

This time, really understand the process and thread

For example, I need to create12Processes, and then process control, let them execute concurrently.

Create process12Secondary distribution12Secondly, the allocation of resources consumes a lot of resources.

Because the resources between processes are independent of each other, the inter process switching is a resource switching, which costs a lot.

At the end of the procedure, cancel12Secondary resources, there is also a lot of overhead when recycling resources.

Is there a way to reduce the cost of creating, undoing, and switching?

Then, the concept of thread is introduced.


The process carries resources, which is too expensive to switch, so we need to separate resource allocation and scheduling.

In modern operating system, process is the basic unit of resource allocation, and thread is the basic unit of scheduling.

There are multiple threads in a process, and multiple threads share the resources of the process,CPUSchedule threads directly.

This time, really understand the process and thread

Or this picture, if we create1A process,12What about threads?

Program start, create a process, allocate resources1Second time.

Establish12Note that this is to create a thread in a process. Therefore, a thread does not need to allocate resources. It can directly use the resources of its process. This overhead is very small.

12Threads executing concurrently, which12Each thread shares a block of system resources, so it is unnecessary to switch resources during thread switching, and the cost is greatly reduced.

What about cancellation? Undo thread12Times, undo process, recycle resources1Second time.


So, this is the development of processes and threads. After learning these histories, I don’t think those answers need to be recited any more?

CPUOne core can only execute one thread, and multiple cores can execute concurrently, but the maximum number of threads parallel is no more than the number of cores.

4Core, most parallel without adding new technology4A thread.

Uh huh?Core i7No4nucleus8Thread? Why Intel’s4Core can run8A thread?

This is Intel’s famous hyper threading technology, which makes use of the unused resources in the processor core, and then simulates a core, although it is4The core processor, so in the user’s view, theCPUYes8Logical core.

It’s very useful on the desktop, but on the server side, hyper threading technology has some problems. This is just my opinion after reading a lot of operation and maintenance blogs about criticizing hyper threading on the server side. We will discuss this technology later.