The concept, attributes and implementation of operating system threads

Preface

This article combs the concept and implementation of threads from the perspective of operating systems.
image.png

What are threads and why are threads introduced

Thread can be understood as a lightweight process.
Thread is a basic CPU execution unit and the smallest unit of program execution.
The introduction of threads further improves the concurrency of the system, so that various tasks can be processed concurrently in a process.
Process is the basic unit of system allocation of resources, with independent address space; thread is the basic unit of CPU scheduling.

Thread attributes

  • Thread is the unit of processor scheduling
  • In a multi-CPU computer, each thread can occupy a different CPU
  • Each thread has a thread ID, thread control block TCB
  • Each thread also has three basic states: ready, blocked, and running
  • Threads have almost no system resources
  • Sharing process resources between different threads of the same process
  • Due to the shared memory address space, inter-thread communication in the same process does not even require system intervention
  • Thread switching in the same process will not cause process switching
  • Thread switching in different processes will cause process switching
  • Switch threads in the same process, with low system overhead
  • High system overhead for switching processes

Thread implementation

User-level threads

User-level threads are implemented by the application through the thread library.

  • All thread management work is the responsibility of the application, including thread switching.
  • In user-level threads, thread switching can be completed in user mode without switching to the core mode and without operating system intervention.
  • From the user's perspective, there are multiple threads, but from the kernel's perspective, there is only one thread. That is, user-level threads are opaque to users and transparent to the operating system.
  • It can be understood that user-level threads are threads that can be seen from the user's perspective.

image.png

Kernel-level thread

The management of kernel-level threads is done by the operating system.

  • The kernel is responsible for thread scheduling and switching, so the switching of kernel-level threads must be completed in the core state.
  • It can be understood that kernel-level threads are threads that can be seen from the perspective of the operating system kernel.

image.png

Combination method

In a system that supports both user-level threads and kernel-level threads, a combination of the two can be used to map n user-level threads to m kernel-level threads n>=m.

  • The operating system can only see kernel-level threads, so only kernel-level threads are the basic unit of CPU scheduling.

image.png

Multithreaded model

In a system that supports both user-level threads and kernel-level threads, n user-level threads are mapped to m kernel-level threads in a combined manner, n>=m. This leads to the multithreading model.

Many-to-one model

Multiple user-level threads are mapped to a kernel-level thread, that is, each process corresponds to only one kernel-level thread.

  • Advantages:
    user-level threads switch in user mode, no need to switch to core mode, low system overhead and high efficiency;

  • Disadvantages
    When a user-level thread is blocked, the entire process will be blocked, and the concurrency is not high;
    multiple threads cannot run in parallel on a multi-core CPU.

One-to-one model

A user-level thread is mapped to a kernel-level thread. Each process has a kernel thread that is organized the same as the user thread.

  • Advantages:
    when a thread is blocked, other threads can continue to execute, with high concurrency;
    multi-threads can run in parallel on multi-core CPUs;

  • Disadvantages:
    A user process will occupy multiple kernel-level threads. The thread switching is completed by the operating system and needs to be switched to the core mode. The thread management cost is high and the overhead is high.

Many-to-many model

n user-level threads are mapped to m kernel-level threads (n >= m). Each user process corresponds to m kernel-level threads.

Collect the strong points of the first two and avoid the shortcomings of the two. Not only can high concurrency, but also avoid high system overhead.

Guess you like

Origin blog.csdn.net/u014099894/article/details/112993024