This is the third day of my participation in Gwen Challenge
I. Background introduction
1. Process generation
The first computers could only accept certain commands, and each time a user typed a command, the computer performed an operation. While the user is thinking or typing, the computer is waiting. This is very inefficient, and most of the time, the computer is in a waiting state.
Then came batch operating systems, in which a list of instructions to be performed was written down and handed to the computer in one go. However, the operation mode is still serial, there is always only one program running in memory, and the efficiency is not high.
As people have higher and higher requirements for computer performance, batch processing obviously cannot meet the demand. Thus the concept of process was introduced. A process is the space allocated by an application program in memory, that is, a running program, each process does not interfere with each other. At the same time, the process saves the running state of the program at each moment.
💻 If a computer CPU is a factory, processes are individual workshops within the factory. Due to the limited power (CPU processing capacity) of the plant, only one workshop can work at a time.
2. Generation of threads
As the requirements of the operating system to further improve Even though process, to enhance the performance of the operating system, but as time goes on, people do not satisfy a process over a period of time can only do one thing, if you have any more child tasks, a process can only be one by one to perform these subtasks, very affect efficiency. So people put forward the concept of thread, let a thread to perform a sub-task, so that a process contains multiple threads, each thread responsible for a separate sub-task.
📡 If processes are a workshop, threads are workshop workers who work together to complete certain tasks.
Two. Thread design
1. Shared memory
Workshop Spaces are shared by workers, with many rooms accessible to each worker. This indicates that the memory space of a process is shared, and each thread can use the shared memory. ,
However, each room is different in size, and some rooms can only accommodate one person at most, such as the toilet. When there are people inside, no one else can get in. This means that when a thread uses some shared memory, other threads must wait for it to finish before they can use it.
2. The mutex
A simple way to keep people out is to add a lock to the door. Those who arrive first lock the door, and those who arrive later, when they see the lock, line up at the door and wait for the lock to open before they enter. Known as “Mutual exclusion,” or Mutex, this prevents multiple threads from simultaneously reading and writing an area of memory.
3. The semaphore
There are also rooms that can accommodate up to n people, such as the kitchen. In other words, if the number of people is greater than n, the extra people have to wait outside. This is like some memory area that can only be used by a fixed number of threads.
The solution, in this case, is to hang n keys at the door. He who goes in takes a key, and hangs it back on his way out. When the last person to arrive finds his keys on the rack, he knows he must wait in line at the door. This practice, called Semaphore, is used to ensure that multiple threads do not conflict with each other.
Context switch
Context switching (sometimes called process switching or task switching) is when the CPU switches from one process (or thread) to another. Context refers to the contents of CPU registers and program counters at a point in time.
Registers are the small amount of fast flash memory inside the CPU that usually stores and accesses the intermediate values of a computing process to speed up a computer program.
A program counter is a special register that indicates where in the sequence of instructions the CPU is executing. The value is either the location of the instruction being executed or the location of the instruction to be executed next, depending on the system.
An example is thread A – B
1. Suspend thread A and save its STATUS in the CPU to the memory.
2. Retrieve the context of the next thread B from the memory, restore it to the CPU register, and execute thread B.
3. When B is finished, restore thread A according to the position indicated in the program counter.
The CPU implements multithreading by allocating CPU time slices to each thread. The CPU uses the time slice allocation algorithm to execute tasks in a cycle. After the current task executes a time slice, it switches to the next task.
However, the state of the previous task is saved before switching, so that the state of the task can be loaded again when switching back to the task. So the process from saving to reloading a task is a context switch.
Context switching is usually computationally intensive, meaning that this operation consumes a lot of CPU time, so more threads are not always better. How to reduce the number of context switching in the system is a key issue to improve the performance of multithreading.
Hyper-threading technology
With the advancement of technology, the number of cores and threads is increasing.
Take the current state-of-the-art CPU, AMD Ryzen TR 3990X, for example. It has 64 cores and 128 threads.
According to the truth, a core has only one thread, why the above CPU a core can correspond to two threads?
Because of the use of hyperthreading technology, the two logical cores are simulated into two physical chips, so that a single processor can use thread-level parallel computing, and compatible with multi-threaded operating system and software, reduce the IDLE time of THE CPU, improve the running efficiency of the CPU.