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.
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-in
iOSOperating system, my development computer,
Unofficial description of operating system: operating system is the system software that manages computer hardware and software resources.
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:
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 skillfully
Manchester Mark IMachine.
- User exclusive
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 than
CPUThe 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.
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 can
I2And 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.
And how to control it
PVOperation, semaphore, process synchronization and mutual exclusion are beyond the scope of this article. You can learn by yourself.
Many years later, people found that the original process was designed with some shortcomings.
There are two characteristics of the original process:
- The basic unit of resource allocation.
- Basic unit of dispatch.
Therefore, in a process only system, to achieve concurrency, you need to do the following:
- Create process: create process
PCB, and assign resources to it.
- Undo process: recycle system resources, undo
- Process switching: switching from one process environment to another.
For example, I need to create
12Processes, and then process control, let them execute concurrently.
12Secondly, 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, cancel
12Secondary 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.
Or this picture, if we create
12What about threads?
Program start, create a process, allocate resources
12Note 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, which
12Each 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 thread
12Times, undo process, recycle resources
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 technology
8Thread? Why Intel’s
4Core can run
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 is
4The core processor, so in the user’s view, the
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.