The difference between user mode and kernel mode is due to the fact that some hardware resources (such as interrupt devices) and privileged instructions cannot be opened to user processes for the safe operation of the computer system.

Kernel mode and user mode

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

To access the kernel space, the kernel must use the API functions provided by the operating system to execute the code provided by the kernel, so that the kernel itself can access the kernel space. In this way, the data in the kernel space will not be arbitrarily modified, and the stability of the operating system and other programs can be guaranteed.

Kernel Mode: A user program calls a System API function called a System Call. When a system call occurs, the user program is suspended, Kernel code is executed (the Kernel is also a program), and Kernel space is accessed. This is called Kernel Mode.

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

User Mode: The User space stores the code and data of the application program, which is private to the program and generally inaccessible to other programs. When executing the application’s own code, this is called User Mode.

The hardware prevents the execution of privileged instructions and checks access to memory and I/O space, which can be accessed in kernel mode through some kind of gate mechanism in the operating system.

Switch between kernel mode and user mode

When an application running in user mode needs low-level operations such as input/output, memory allocation, etc., it must call the API functions provided by the operating system to enter the kernel mode. Once the operation is complete, you continue executing the application’s code and return to user mode.

User mode is executing application level code, accessing user space; Kernel mode is the execution of kernel code and access to kernel space (and of course access to user space).

The following table shows the two modes switching processes:

User mode to kernel mode Kernel to user mode
Triggered by an interrupt/exception/system call that interrupts user process execution.

1. Change the processor mode to the core mode.

2. Save the PC/PSW value of the current process to the core stack.

3. Go to interrupt/exception/system call handlers.
Triggered when the OS executes an interrupt return instruction to return control to the user process.

1. Pop the PC/PSW value from the core stack of the process to be run.

2. Change the processor mode to user mode.

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

A process is the basic unit of resource ownership. Process switchover requires that process status be saved, which consumes resources. Threads in the same process share some of the resources acquired by the process. In the same process, thread switching does not cause process switching. Thread switching requires fewer resources than process switching, which improves efficiency.

KLT also has what are called kernel-level Threads.
  • All thread-managed work (creation and undoing) 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 to be multithreaded using the thread library. The thread library is a package of routines for user-level thread management that provides a development and runtime support environment for multithreaded applications, including: code to create and destroy threads, code to pass data and messages between threads, code to schedule thread execution, and code to save and restore thread context.
  • So thread creation, messaging, scheduling, save/restore context are all done by thread libraries. The kernel is unaware of multithreading. The kernel continues to schedule by process and gives the process an execution state (ready, running, blocked, etc.).
Kernel level threading features Features of user-level threads
1. If a thread in the process is blocked, the kernel can schedule other threads in the same process (ready state) to run on the processor.

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. Application threads run in user mode, and thread scheduling and management are implemented in the kernel. When the thread is scheduled, the control changes from one thread to another thread, which requires a mode switch.
1. Thread switching does not require the kernel mode, which saves the mode switching overhead and kernel resources.

2. Allow processes to select different scheduling algorithms to schedule threads according to specific needs. The scheduling algorithm needs to be implemented.

3. Since it does not require kernel support, it can run across oss.

4. Can’t utilize the multi-core processor a bit, OS scheduling process, each process only one ULT can execute

5. If an ULT blocks, the entire process will be blocked.

Jacketing technology addresses user-level threadingULTOne thread blocking causes the entire process to block.

The jacketing goal is to transform a blocking system call into a non-blocking system call. For example, when a thread in a process calls AN IO interrupt, it calls an application-level I/O jacket routine first, rather than calling a system I/O directly. Have the Jacket routine check and determine if the I/O device is busy. If busy, the Jacketing gives control to the process’s thread scheduler, determines that the thread enters the blocked state and passes control to another thread (how can a process switch be performed without a ready thread).

Combination strategy implemented by threads

It can be seen that user-level thread and kernel-level thread have their own advantages and disadvantages, which are mainly expressed in the application:

  • User-level multithreading is very effective for dealing with logical parallelism problems. Not very 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’s libraries, thread creation is created entirely in user space, ready-made scheduling also takes place within the application, and then user-level multithreading is mapped to (or bound to) some kernel-level multithreading.

Programmers can adjust the number of kernel-level threads according to different application characteristics to achieve the best physical parallelism and logical parallelism.

Refer to the article

  • Blog.csdn.net/winterfeng1…
  • Blog.csdn.net/sinat_38104…
  • Blog.csdn.net/winterfeng1…
  • Docs.microsoft.com/zh-cn/windo…
  • www.kanzhun.com/msh/post/16…

Focus on data craftsman, focus on Java big data field offline, real-time technology dry goods regular sharing! Personal Website: www.lllpan.top