User mode and kernel mode (thread level)

The main reason for the distinction between user mode and kernel mode is that, for the normal and safe operation of the computer system, some hardware resources (such as interrupt devices) and privileged instructions cannot be opened to user processes, so the two modes are distinguished.

Kernel mode and user mode

The kernel space stores the operating system kernel code and data, which are shared by all programs . Modifying the data in the kernel space in a program will not only affect the stability of the operating system itself, but also affect other programs. This is a very dangerous behavior. , So the operating system prohibits user programs from directly accessing the kernel space.

To access the kernel space, you must use the API functions provided by the operating system to execute the code provided by the kernel and allow the kernel to access it by itself, so as to ensure that the data in the kernel space will not be arbitrarily modified to ensure the stability of the operating system itself and other programs Sex.

Kernel Mode : The user program calling system API functions is called System Call; when a system call occurs, the user program will be suspended, and the kernel code will be executed (the kernel is also a program) to access the kernel space, which is called the kernel Kernel Mode.

Tasks can execute privileged instructions, have full access to any I/O device, and can also access any virtual address and control virtual memory hardware.

User Mode : The user space saves the code and data of the application program, which is private to the program and generally cannot be accessed by other programs. When the application's own code is executed, it is called User Mode.

The hardware prevents the execution of privileged instructions, and checks the access operations of the memory and I/O space, and can enter the kernel mode access through a certain gate mechanism in the operating system.

Switch between kernel mode and user mode

When an application running in user mode requires lower-level operations such as input and output, memory application, etc., it must call the API function provided by the operating system to enter the kernel mode; after the operation is completed, continue to execute the application code, and then return To the user mode.

User mode is to execute application-level code and access user space; kernel mode is to execute kernel code and access kernel space (of course, it also has permission to access user space).

The following table shows the process of switching between the two modes:

User mode to kernel mode Kernel mode to user mode
Triggered by interruption/exception/system call interrupting the execution of the user process.
1. The processor mode is changed to kernel mode.
2. Save the PC/PSW value of the current process to the core stack.
3. Turn to interrupt/abnormal/system call handler.
The OS executes the interrupt return instruction to return the control right to the user process to trigger it.
1. Pop the PC/PSW value from the core stack of the process to be run.
2. The processor mode is changed to user mode.

Kernel-level threads (KLT) and user-level threads (ULT)

Process is the basic unit of resource ownership. Process switching needs to save process state, which will cause resource consumption. Threads in the same process share part of the resources acquired by the process. In the same process, thread switching does not cause process switching, and thread switching requires less resources than process switching, which can improve efficiency.

Kernel-Level Threads (Kernel-Level Threads), KLT also has threads called kernel support.
  • All work (creation and cancellation) of thread management is done by the operating system kernel
  • The operating system kernel provides an application programming interface API for developers to use KLT
User-Level Threads ULT
  • User space runs the thread library, and any application can be designed as a multi-threaded program by using the thread library. The thread library is a routine package for user-level thread management. It provides a support environment for the development and operation of multi-threaded applications. It includes: code for creating and destroying threads, code for transferring data and messages between threads, and scheduling The code executed by the thread and the code to save and restore the thread context.
  • Therefore, thread creation, message passing, scheduling, and saving/restoring context are all completed by the thread library. The kernel does not perceive the existence of multithreading. The kernel continues to use the process as the scheduling unit and assigns an execution state (ready, running, blocked, etc.) to the process.
Kernel-level thread characteristics Features of user-level threads
1. One thread in the process is blocked, and the kernel can schedule other threads of the same process (ready state) to occupy the processor to run.
2. In a multi-processor environment, the kernel can schedule multiple threads of the same process at the same time, and map these threads to different processor cores to improve the execution efficiency of the process.
3. The application thread runs in user mode, and thread scheduling and management are implemented in the kernel. During thread scheduling, the control power is changed from one thread to another thread, which requires mode switching, and the system overhead is relatively high.
1. Thread switching does not require kernel mode, which can save mode switching overhead and kernel resources.
2. Allow the process to select different scheduling algorithms to schedule threads according to specific needs. The scheduling algorithm needs to be implemented by itself.
3. Since it does not need kernel support, it can run across OS.
4. Cannot use multi-core processors. The OS schedules processes, and each process has only one ULT that can execute.
5. One ULT blocking will cause the entire process to block.

Jacketing technology can solve the user-level thread ULT one thread blocking causes the entire process to block.

The goal of jacketing is to convert a blocking system call into a non-blocking system call. For example, when a thread in the process calls IO interrupt money, first call an application-level I/O jacket routine instead of directly calling a system I/O. Let this jacket routine check and determine if the I/O device is busy. If it is busy, jacketing transfers control to the thread scheduler of the process, determines that the thread enters the blocking state and transfers the control to another thread (if there is no ready state, the thread may perform process switching).

Combination strategy for thread implementation

It can be seen that user-level threads and kernel-level threads have their own advantages and disadvantages, which are mainly manifested in applications:

  • User-level multithreading has a very good effect on dealing with logic parallelism. Not good at solving physical concurrency problems.
  • Kernel-level multithreading is suitable for solving physical parallelism problems.

Combination strategy:
kernel-level multithreading is supported by the operating system kernel, user-level multithreading is supported by the operating system library, thread creation is created entirely in user space, ready-made scheduling is also performed inside the application, and then user-level multithreading Mapped to (or bound to) some kernel-level multithreading.

Programmers can adjust the number of kernel-level threads for different application characteristics to achieve the best solution for physical and logical parallelism.
Insert picture description here

Reference article

  • https://blog.csdn.net/winterfeng123/article/details/79788714
  • https://blog.csdn.net/sinat_38104725/article/details/98474760
  • https://blog.csdn.net/winterfeng123/article/details/79784430
  • https://docs.microsoft.com/zh-cn/windows-hardware/drivers/gettingstarted/user-mode-and-kernel-mode
  • https://www.kanzhun.com/msh/post/1691.html

Pay attention to the public account and focus on the offline and real-time technical dry goods in the field of java big data to share regularly! Personal website www.lllpan.top

Insert picture description here

Guess you like

Origin blog.csdn.net/lp284558195/article/details/115346904
Recommended