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.
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
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