(Study Notes - Process Management) Thread

In the early operating systems, processes were the basic unit of independent operation. Until later, computer scientists proposed a smaller basic unit that can run independently: thread


 

Why use threads?

For example, suppose you want to write a video playback software, then there are three core modules of software functions:

  • read data from video file
  • Decompress the read data
  • Play the compressed video data

For a single-process implementation:

 For this method of single process, there are the following problems:

  • The played picture and sound will be incoherent, because when the CPU capacity is not strong enough, the process may be waiting here during Read, which will lead to waiting half a day for data decompression and playback
  • Functions are not executed concurrently, which affects the efficiency of resource usage

Then change to a multi-process method:

 For this multi-process approach, there are still problems:

  • How do processes communicate and share data?
  • The system overhead of maintaining a process is relatively large, such as when creating a process, allocate resources and build a PCB ; when terminating a process, reclaim resources and revoke PCB; when switching between processes, save the status information of the current process;

So how to solve it? There needs to be a new entity that satisfies the following properties:

  • Entities can run concurrently
  • Entities share the same address space

This new entity is a thread (Thread) , which can run concurrently and share the same address space.


What are threads?

A thread is a flow of execution in a process.

Multiple threads in the same process can share resources such as code segments, data segments, and open files, but each thread has its own set of independent registers and stacks, which ensures that the control flow of the threads is relatively independent.

 Advantages and disadvantages of threads?

Advantages of threads:

  • Multiple threads can exist in a process at the same time
  • Each thread can execute concurrently
  • Resources such as address space and files can be shared between threads

Disadvantages of threads:

  • When a thread in the process crashes, it will cause all threads of the process it belongs to to crash (here for C/C++ language).

For example, for the user design of the game, multi-threading should not be used, otherwise, if a user hangs up, it will affect other threads of the same process.


Comparing threads and processes

Comparison of threads and processes:

  • A process is a unit for resource allocation (including memory, open files, etc.), and a thread is a unit for CPU scheduling
  • Processes have a complete resource platform, while threads only share essential resources, such as registers and stacks
  • Threads also have three basic states of ready, blocked, and executing, and also have transition relationships between states
  • Threads can reduce the time and space overhead of concurrent execution

Compared with processes, threads can reduce overhead, which is reflected in:

  • The thread creation time is faster than the process, because the process also needs resource management information during the creation process, such as memory management information, file management information, and the thread will not design these resource management information during the creation process, but share them
  • The termination time of the thread is faster than that of the process, because the resources released by the thread are much smaller than that of the process
  • Thread switching within the same process is faster than process switching, because threads have the same address space (virtual memory space), which means that threads of the same process have the same page table, so there is no need to switch pages when switching surface. For switching between processes, the page table must be switched when switching, and the overhead of switching the page table is relatively large.
  • Since the threads of the same process share memory and file resources, the data transfer between threads does not need to go through the kernel, which makes the data interaction between threads more efficient.

So whether it is time efficiency or space efficiency, threads are higher than processes.


Thread context switching

We know that the biggest difference between processes and threads is that threads are the basic unit of scheduling, while processes are the basic unit of resource ownership.

The so-called task scheduling of the operating system actually schedules threads, and processes only provide threads with resources such as virtual memory and global variables.

For threads and processes, it can be understood as:

  • When a process has only one thread, it can be considered that the process is equal to the thread
  • When a process has multiple threads, these threads will share the same resources such as virtual memory and global variables, and these resources do not need to be modified during context switching

In addition, threads also have their own private data, such as stacks and registers, which also need to be saved during context switching.

What is thread context switching?

It also depends on whether the threads belong to the same process:

  • When two threads do not belong to the same process, the switching process is the same as process context switching
  • When two threads belong to the same process, because virtual memory is shared, resources such as virtual memory remain unchanged when switching, and only data that is not shared, such as private data and registers of threads, need to be switched

Therefore, thread context switching is much less expensive than process overhead.


Implementation of threads

There are three main implementations of threads:

  • User threads: Threads implemented in user space are not threads managed by the kernel, but are managed by the application-level thread library. The kernel cannot perceive the existence of user threads.
  • Kernel thread: A thread implemented in the kernel is a thread managed by the kernel
  • Lightweight processes: in the kernel to support user threads

Correspondence between user threads and kernel threads

First of all, the first relationship is a many-to -one relationship, that is, multiple user threads correspond to the same kernel thread:

 The second is a one-to- one relationship, that is, one user thread corresponds to one kernel thread:

 The third is a many-to-many relationship, that is, multiple user threads correspond to multiple kernel threads:

 How to understand the user thread? What advantages and disadvantages exist?

User threads are implemented based on the user-mode thread management library, and the thread control block (Thread Control Block, TCB ) is also implemented in the library. The operating system cannot see this TCB, and the kernel cannot perceive the user. level thread exists , it can only see the entire process PCB.

Therefore, the operating system does not directly participate in the entire thread management and scheduling of user threads, but user-level thread library functions complete thread management, including thread creation, termination, synchronization, and scheduling .

The model of user-level threads is similar to the many-to- one relationship mentioned above , that is, multiple user threads correspond to the same kernel thread, as shown in the following figure:

 Advantages of user threads:

  • Each process needs to have its private thread control block (TCB) list, which is used to track and record its thread state information (PC, stack pointer, register). technical operating system;
  • The switching of user threads is also completed by the thread library function, without switching between user mode and kernel mode, so the speed is very fast;

Disadvantages of user threads:

  • Since the operating system does not participate in thread scheduling, if a thread initiates a system call and blocks, the user threads contained in the process cannot be executed, and true concurrency cannot be achieved.
  • When a thread starts running, unless it actively surrenders the right to use the CPU, other threads in the process it is in cannot run, because user-mode threads cannot interrupt the currently running thread, it does not have this privilege, only The operating system has it, but user threads are not managed by the operating system
  • The allocation of kernel resources is allocated according to the process. The process where the user-level thread resides can compete for system resources, while each user thread can only compete for the internal resources of the process . For a process, there may be thousands of user-level threads, but they have no impact on the system's resources.

How to understand the kernel thread? What advantages and disadvantages exist?

The kernel thread is managed by the operating system, and the TCB corresponding to the thread is naturally placed in the operating system, so the creation, termination and management of the thread are the responsibility of the operating system .

  • Kernel-level threads can compete for resources throughout the system
  • A thread control block (TCB) is set for each kernel support thread in the kernel space, and the kernel senses the existence of the thread and controls it according to the control block.

The model of the kernel thread is similar to the one-to- one relationship mentioned above , that is, one user thread corresponds to one kernel thread, as shown in the following figure:

Advantages of kernel threads:

  • In a process, if a kernel thread initiates a system call and is blocked, it will not affect the operation of other kernel threads
  • Assigned to threads, multi-threaded processes get more CPU running time

Disadvantages of kernel threads:

  • In operating systems that support kernel threads, the kernel maintains process and thread context information, such as PCB and TCB
  • The creation, termination and switching of threads are all carried out through system calls, so for the system, the system overhead is relatively large

 How to understand lightweight process?

A lightweight process (Light-weight process, LWP) is a user thread supported by the kernel. A process can have one or more LWPs. Each LWP is mapped one-to-one with a kernel thread, that is, each LWP is created by a kernel thread. Threads are supported, and LWPs are managed by the kernel and scheduled like normal processes .

On most systems, an LWP differs from a normal process in that it has only a minimal execution context and statistics required by the scheduler . Generally speaking, a process represents an instance of the program, and LWP represents the execution thread of the program, because an execution thread does not need as much state information as a process, so LWP does not carry such information.

User threads can also be used on top of LWP, so there are three types of correspondence between LWP and user threads:

  •  1 : 1, that is, one LWP corresponds to one user thread
  •  N : 1, that is, one LWP corresponds to multiple user threads
  •  M : N, that is, multiple LWPs correspond to multiple user threads

  1 : 1 mode

A thread corresponds to an LWP and then corresponds to a kernel thread, as shown in process 4, which belongs to this model

  • Advantages: achieve parallelism, when one LWP is blocked, it will not affect other LWPs
  • Disadvantages: Each user thread generates a kernel thread, and the overhead of creating threads is relatively high.

 N : 1 mode

Multiple user threads correspond to one LWP and then one kernel thread, as shown in process 2 in the above figure. Thread management is done in user space. In this mode, user threads are invisible to the operating system.

  • Advantages: It is no problem to open several user threads, and context switching occurs in user space, so the switching efficiency is high
  • Disadvantages: If a user thread is blocked, the entire process will be blocked. In addition, in a multi-core CPU, there is no way to make full use of the CPU

 M : N mode

This mode provides two levels of control. First, multiple user threads correspond to multiple LWPs, and LWPs correspond to kernel threads one by one, as shown in process 3 in the above figure.

  • Advantages: Combining the advantages of the first two, most of the thread context occurs in user space, and multiple threads can make full use of multi-core CPU resources

combination mode

As in process 5 in the figure above, this process combines  1:1 models and  M:N models. Developers can adjust the number of kernel threads according to different application characteristics to achieve the best solution of physical parallelism and logical parallelism.

Guess you like

Origin blog.csdn.net/qq_48626761/article/details/132123233