[Operating system] Threads, multi-threads

Why introduce threads?

      Traditional processes can only execute a series of programs serially, and threads increase concurrency. The same process is divided into multiple threads.

 

Thread is the basic unit of scheduling, the smallest unit of program execution flow, and the basic CPU execution unit.

Process is the basic unit of resource allocation.

d93dd0ff291946329c7fb2441d074a56.png

 

How threads are implemented

user-level thread

       For threads implemented using the thread library in the code, the operating system still only sees the process.

Advantages: The overhead of managing user-level threads is small, and there is no need to switch between user mode and kernel mode.

Disadvantages: If one thread is blocked, the entire process is blocked, and the parallelism is poor.

Kernel level threads

      The operating system kernel manages kernel-level threads, which needs to be completed in the core state. Threads can be seen from the perspective of the operating system kernel.

The advantages and disadvantages are opposite to the former.

 

Kernel-level threads are the allocation unit of the processor.

 

multi-threaded model

 

133b22b4222b4c67bf8af550c6b2df95.png

User-level threads are the carrier of "code logic"

Kernel-level threads are the carrier of "running opportunities"

 

The status and transitions of threads are the same as those of processes

TCB (Thread Control Block)

Thread identifier TID, program counter PC, other registers, stack pointer, thread running status, priority.

Thread table: a table that stores multiple TCBs

 

 

 

 

 

Guess you like

Origin blog.csdn.net/l203018/article/details/133237924