Operating System (2.8)--Thread Implementation

Table of contents

Implementation of threads

1. Kernel Support Thread (KST)

2. User-level threads (ULT)

3. Combination method

Implementation of threads

1. Implementation of kernel support thread

2. Implementation of user-level threads

Thread creation and termination


 

Implementation of threads

1. Kernel Support Thread (KST)

The kernel supports threads, which are the same as processes and run with the support of the kernel, that is, whether it is a thread in a user process or a thread in a system process, their creation, cancellation, and switching also rely on the kernel and are implemented in the kernel space .

This thread implementation mainly has the following four advantages:

  • (1) In a multiprocessor system, the kernel can simultaneously schedule multiple threads in the same process to execute in parallel
  • (2) If a thread in the process is blocked, the kernel can schedule other threads in the process to occupy the processor to run, and can also run threads in other processes;
  • (3) The kernel supports threads with small data structures and stacks, so the switching of threads is faster and the switching overhead is small;
  • (4) The kernel itself can also adopt multi-threading technology, which can improve the execution speed and efficiency of the system.

The main disadvantages of kernel support for threads are:

  • The overhead of mode switching is relatively large. In the same process, when switching from one thread to another, it is necessary to switch from user mode to kernel mode. This is because the threads of the user process run in user mode, and thread scheduling and management are implemented in the kernel. The system The overhead is high.

kernel-level thread

2. User-level threads (ULT)

User-level threads exist only in user space. There is no need to use system calls to implement such functions as thread creation, cancellation, synchronization and communication between threads. The kernel is completely unaware of the existence of user-level threads. (round-robin scheduling algorithm)

Note: For systems with user-level threads set, the scheduling is still performed in units of processes. If the kernel supports threads in the system, scheduling is performed in units of threads.

Using user-level threads mainly has the following three advantages:

  • (1) Thread switching does not need to switch to the kernel space, thereby saving the overhead of mode switching and saving the precious resources of the kernel.
  • (2) The scheduling algorithm can be process-specific. Without interfering with the scheduling of the operating system, different processes can choose different scheduling algorithms to manage and schedule their own threads according to their own needs, regardless of the low-level scheduling algorithms of the operating system.
  • (3) The implementation of user-level threads has nothing to do with the operating system platform, because the code for thread management is in the user program and is a part of the user program, and all applications can share it. Therefore, user-level threads can be implemented even on operating system platforms that do not support threading mechanisms.

The main disadvantages of the user-level thread implementation are:

  • (1) The blocking problem of the system call. In an operating system based on the process mechanism, most system calls will block the process. Therefore, when a thread executes a system call, not only the thread is blocked, but all threads in the process are also blocked. In the kernel-supported thread mode, other threads in the process can still run.
  • (2) In a purely user-level thread implementation, multithreaded applications cannot take advantage of the multiprocessing advantages of multiprocessors. The kernel allocates only one CPU to a process at a time, so only one thread in the process can execute, and other threads can only wait before the thread gives up the CPU.

user-level thread

3. Combination method

Some operating systems combine user-level threads and kernel-supported threads, and provide a combined ULT/KST thread. In the combined thread system, the kernel supports the establishment, scheduling and management of multiple KST threads, and at the same time, allows user applications to create, schedule and manage user-level threads. Some kernel support threads correspond to multiple user-level threads. Programmers can adjust the number of kernel support threads according to application needs and machine configurations to achieve better results. In combined threads, multiple threads in the same process can be executed in parallel on multiple processors at the same time, and when one thread is blocked, the entire process does not need to be blocked. Therefore, the combined multithreading mechanism can combine the advantages of both KST and ULT, and overcome their respective shortcomings.

mixed thread

Due to the different connection between user-level threads and kernel-controlled threads

  • 1) One-to-one model This model is to set up a kernel control thread to connect to each user thread, and when one thread is blocked, another thread is allowed to be scheduled to run.
  • 2) Many-to-one model This model maps multiple user threads to one kernel control thread. For the convenience of management, these user threads generally belong to one process.
  • 3) Many-to-many model This model combines the advantages of the above two models, and maps multiple user threads to multiple kernel control threads. The number of kernel control threads can vary according to application processes and systems, and can be less than user threads , which can also be the same.

Implementation of threads

1. Implementation of kernel support thread

The kernel supports threads to directly use system calls to serve it, and the control is simple. When the system creates a new process, it allocates a task data area PTDA (Per Task Data Area), which includes several thread control block TCB spaces, and the information in these TCBs is stored in the kernel space. The creation, cancellation, scheduling, and switching of threads supported by the kernel are similar to those of processes.

2. Implementation of user-level threads

User-level threads are implemented in user space, and they all run on top of an intermediate system. There are currently two intermediate systems implemented in two ways, the runtime system and the kernel control thread.

1) Runtime system

The so-called "runtime system" is essentially a collection of functions (processes) used to manage and control threads, including functions for creating and canceling threads, functions for thread synchronization and communication, and functions for implementing thread scheduling wait. It is because of these functions that user-level threads are independent of the kernel. All functions in the runtime system reside in user space and serve as an interface between user-level threads and the kernel. User-level threads are implemented in user space, and they all run on top of an intermediate system. There are currently two intermediate systems implemented in two ways, the runtime system and the kernel control thread.

2) Kernel control thread

Kernel control thread This kind of thread is also called light process LWP. Each process can have multiple LWPs. Like user-level threads, each LWP has its own data structure (such as TCB). They can also share resources owned by the process. LWP can obtain the services provided by the kernel through system calls, so that when a user-level thread is running, as long as it is connected to an LWP, it has all the attributes of the kernel-supported thread. This thread implementation is the combination method. The isolation between the kernel and user-level threads is achieved by the LWP, making user-level threads independent of the kernel.

Leverage lightweight processes as intermediate systems

Thread creation and termination

In a multi-threaded OS environment, when an application program is started, usually only one thread is executing, and this thread is called an "initialization thread". It can create several threads as needed. There are two ways to terminate a thread: one is to exit voluntarily after the thread has finished its work; the other is that the thread has an error during operation or is forcibly terminated by other threads for some reason. But some threads (mainly system threads), once they are established, keep running without being terminated. A thread that has been terminated but has not yet released resources can still be called by a thread that needs it, so that the terminated thread can resume running.

Mailbox communication is a (B) way.

A. Direct communication

B. Indirect communication

C. Low-level communication.

D. Semaphore

In message buffering communication, a message queue is a (A) resource.

A. critical

b. to share

c. permanent

D. Deprivable

(A) is not a means of communication between processes.

A. Procedure call

B. Messaging

C. Shared memory

D. Mailbox communication

(D) is not how threads are implemented.

A. User-level threads

B. Kernel-level threads

C. The combination of user-level threads and kernel-level threads

D. Lightweight threads

Guess you like

Origin blog.csdn.net/weixin_53197693/article/details/130905753