Threads for operating system review

Table of contents

(1) The basic concept of thread

(2) Attributes of threads

(3) Implementation of threads

 (4) Multithreading model

(5) Comparison between threads and processes

【example】


(1) The basic concept of thread

We know that the introduction of processes is to enable multi-programs to execute concurrently, improve resource utilization and system throughput; and the introduction of threads is to reduce the time and space overhead of concurrent execution of programs and improve the concurrency performance of the operating system .

Def: A thread is an entity in a process. It is the smallest unit that can perform operation scheduling in the operating system. It consists of a thread ID, a program counter, a register set, and a stack. Multiple threads can run concurrently in a process, and each thread can perform different tasks in parallel, and a thread can also create and revoke another thread.

Therefore, after the thread is introduced, the process is only used as an independent unit with resources , and the thread is used as the basic unit of processor scheduling and allocation .

(2) Attributes of threads

①Light entity. Entities in threads basically do not own system resources, but only have a few essential resources that can ensure their independent operation. Like a process, the thread control block is the unique identifier of the thread control operation. Since the thread is very "light", the switching of the thread is very fast and the overhead is small (in the same process).

② The basic unit of independent scheduling and dispatching. In a multi-threaded OS, a thread is the basic unit that can run independently, and thus is also the basic unit of independent scheduling and dispatch.

③ It can be executed concurrently. Multiple threads in a process can be executed concurrently, and even all threads in a process can be executed concurrently ; similarly, threads in different processes can also be executed concurrently , making full use of the ability of the processor and peripheral devices to work in parallel.

④ Shared process resources. All threads in the same process can share the resources owned by the process. In the same process, all threads have the same address space (the address space of the process), that is, the thread can access every virtual address in the address space; at the same time, it can also access the opened files, timers, and semaphore mechanisms owned by the process. Since threads in the same process share memory and files, threads do not need to call the kernel to communicate with each other.

(3) Implementation of threads

① User-level thread ULT

A user-level thread refers to a thread that does not depend on the operating system core and is controlled by the application process using the thread library to provide functions for creating, synchronizing, scheduling, and managing threads . Since the maintenance of user-level threads is completed by the application process, the operating system kernel does not need to know the existence of user-level threads, so it can be used for multi-process operating systems or single-user operating systems that do not support kernel-level threads. User-level thread switching does not require kernel privileges, nor does it require user mode/kernel mode switching, so it is extremely fast. Of course, since the operating system kernel is unaware of the existence of user threads, when a thread blocks, the entire process must wait . At this time, the processor time slice is allocated to the process. When there are multiple threads in the process, the execution time of each thread is relatively reduced.

②Kernel-level thread KTL

Kernel-level threads refer to threads that depend on the kernel and are created and revoked by the operating system kernel. In an operating system that supports kernel-level threads, the kernel maintains context information of processes and threads and completes thread switching. When a kernel-level thread is blocked due to an I/O operation, it will not affect the operation of other threads. Only kernel-level threads are the unit of processor allocation.

③Multi-thread implementation of combination mode

In some systems, thread creation is completely done in user space, and thread scheduling and synchronization are also carried out in the application program. Multiple user-level threads in an application are mapped to kernel-level threads that are less than or equal to the number of user-level threads.

 (4) Multithreading model

The multithreading model is used to realize different connections between user-level threads and kernel-level threads, mainly including:

①Many to one 

Multiple user-level threads are mapped to one kernel-level thread, and thread management is done in user space. In this mode, user-level threads are invisible (that is, transparent) to the operating system

Advantages: Thread management is performed in user space, so the efficiency is relatively high.

Disadvantages: If a thread is blocked when using kernel services, the entire process will be blocked, and multiple threads cannot run in parallel on a multiprocessor.

② One to one

Maps each user-level thread to a kernel-level thread.

Advantages: When a thread is blocked, another thread is allowed to continue execution, so the concurrency capability is strong.

Disadvantages: Every time a user-level thread is created, a kernel-level thread needs to be created to correspond to it , so the overhead of creating a thread is relatively large, which will affect the performance of the application.

③ many to many

Many-to-many model. Map n user-level threads to m kernel-level threads, requiring m<=n.

Features: The many-to-many model not only overcomes the shortcomings of the many-to-one model with low concurrency, but also overcomes the shortcomings of the one-to-one model where one user process occupies too many kernel-level threads and consumes too much overhead. There are advantages to both many-to-one and one-to-one models.

(5) Comparison between threads and processes

① Scheduling. In an operating system that introduces threads, a thread is the basic unit of independent scheduling, and a process is the basic unit that owns resources; thread switching in the same process will not cause process switching. Switching threads in different processes, such as switching from a thread in one process to a thread in another process, will cause a process switch.

② Concurrency. In operating systems that introduce threads. Not only processes can be executed concurrently, but also multiple threads can be executed concurrently, so that the operating system has better concurrency. The throughput of the system is improved.

③ Possess resources. Whether it is a traditional operating system or an operating system without threads, a process is the basic unit that owns resources; while a thread does not own system resources (only a few essential resources), but a thread can access the system resources that belong to the process.

④ System overhead. Since the system must allocate or reclaim resources, such as memory space, IO devices, etc., when creating or canceling a process, the overhead paid by the operating system is much greater than the overhead when creating or canceling a thread; while thread switching only needs to save and set a small amount of register content, the overhead is very small. Since multiple threads in the same process have the same address space, the realization of synchronization and communication between them becomes easier. And without the intervention of the operating system.

【example】:

1. In the following description about user-level threads and kernel-level threads, the error is ( ). B

A. Using the round-robin scheduling algorithm, the effects of setting kernel-level threads and user-level threads in the process are completely different

B. Cross-process user-level thread scheduling does not require kernel participation, and the control is simple

C. User-level threads can run in any operating system

D. If there are only user-level threads in the system, the scheduling object of the processor is a process

Cross-process threads need to be scheduled by the processor and require the participation of the kernel

The maintenance of user-level threads is completed by the application process, and the operating system kernel does not need to know the existence of user-level threads, so it can run in any operating system

If the system has only user-mode threads, the threads are invisible to the operating system, and the operating system can only schedule processes; if there are kernel-mode threads in the system, the operating system can schedule according to threads

2. In the following description of the advantages of kernel-level threads over user-level threads, the error is ( ) A

A. Thread switching within the same process, the system overhead is small

B. When a kernel thread is blocked, the CPU will schedule other kernel threads in the same process to execute

C. Program entities of kernel-level threads can run in kernel mode

D. For multiprocessor systems, the core can simultaneously schedule multiple threads of the same process to run in parallel

In the kernel-level thread, the thread switching of the same process needs to be carried out from the user state to the core state, and the system overhead is large

In the kernel-level thread, scheduling is a thread-level process. The kernel can schedule multiple threads of the same process to run on multiple CPUs at the same time. BD is correct

When the kernel-level thread in the process runs in the kernel state, it means that the process is also in the kernel state

3. In the following description of the advantages of user- level threads over kernel-level threads, the error is ( ) A

A. The blocking of one thread does not affect the operation of another thread

B. The scheduling of threads does not require the direct participation of the kernel, and the control is simple

C. The cost of thread switching is small

D. Allow each process to customize its own scheduling algorithm, and thread management is more flexible

·If the user-level thread is blocked, the entire process will be blocked, so other threads in the process will also be blocked, A error

User-level threads are scheduled in user space, which saves the overhead of mode switching. Different processes can choose different scheduling algorithms for their own threads according to their own needs.

[True question]:

1. [2011 Unified Exam Questions] In a system that supports multi-threading, the threads created by process P cannot be shared ( ). D

A. The code segment of process P B. The file opened in process P C. The global variable of process P D. The stack pointer of a thread in process P

The threads in the process share all the resources of the process, but the stack pointer of a thread is transparent to other threads and cannot be shared with other threads

2. [2012 Unified Exam Questions] Among the following descriptions about processes and threads, the correct one is ( ). A

A. Regardless of whether the system supports threads or not, a process is the basic unit of resource allocation

B. Thread is the basic unit of resource allocation, and process is the basic unit of scheduling

C. Switching between system-level threads and user-level threads requires kernel support

D. Each thread in the same process has its own different address space

Concept confusion in B. In a system that supports threads, a process is the basic delay of resource allocation, and a thread is the basic unit of scheduling and allocation; in C, user-level thread switching is in the user space process, and the management of related threads is completed by the application program; in D, threads in the same process share the address space of the process

3. [2019 Unified Examination Questions] In the following description about threads, the error is ( ). B

A. The scheduling of kernel-level threads is determined by the operating system

B. The operating system creates a thread control block for each user-level thread

C. Switching between user-level threads is more efficient than switching between kernel-level threads

D. User-level threads can be implemented on operating systems that do not support kernel-level threads

In the multi-threaded model, there is only one-to-one model in which the operating system creates a thread control block B error for each user thread

User-level thread switching is performed in user space, and kernel-level thread switching requires the operating system to help schedule CD correctly

Supongo que te gusta

Origin blog.csdn.net/weixin_46516647/article/details/124448684
Recomendado
Clasificación