User status:
When a process is executing the user's own code, it is said that it is in the user state. At this time, the CPU access resources are limited, and programs running in the user state cannot directly access the operating system kernelData structureand program.
Kernel state
When a task (process) executes a system call and is trapped in kernel code execution, we say that the process is in kernel state, and then the CPU can access any resources on the computer.
Kernel functions:
(1) Process (thread) management(Process or thread scheduling)
(2) Low-level memory management(The transformation of the physical address of the user program logical space to the memory space)
(3) Interrupt and trapped management(Interrupt and trapped)
Due to the existence of the microkernel structure, the program runs in two different places.Kernel state and user state, kernel state and user state are the two operating levels of the operating system, and there is no necessary connection with Intel CPU.
The user program runs in the user state, and the operating system runs in the kernel state. (The operating system kernel runs in the kernel state, while the server runs in the user state). The user state cannot interfere with the kernel state. Therefore, there are two types of CPU instructions, privileged instructions and non-privileged instructions. Different states correspond to different instructions. Privileged instructions: instructions that can only be used partially by the operating system kernel and are not allowed to be used directly by users.like,I/OInstructions, terminal blocking instructions, clearing memory, building storage protection, setting clock instructions (these are remembered well, belong to the kernel state).Non-privileged directives: All programs can be used directly.
User state becomes kernel state:
- System call: This is a way for the user-state process to actively ask to switch to the kernel state. The user-state process applies to use the service program provided by the operating system to complete the work through the system call. For example, in the previous example, fork() actually executes a system call to create a new process. The core of the system call mechanism is to use an interrupt specially opened by the operating system to implement it, such as Linux's int 80h interrupt.
- Exception: When the CPU executes a program running in the user state, some pre-unknown exception occurs. At this time, it will trigger the switch from the current running process to the kernel-related program that handles this exception, and then it will go to the kernel state, such as a page-missing exception.
- Interruption of peripheral devices: When the peripheral device completes the user requested operation, it will send a corresponding interrupt signal to the CPU. At this time, the CPU will pause the execution of the next instruction to be executed and instead execute the processing program corresponding to the interrupt signal. If the previously executed instruction is a program in the user state, then the conversion process will naturally undergo a switching from the user state to the kernel state. For example, if the hard disk read and write operation is completed, the system will switch to the interrupt handler for the hard disk read and write operation to perform subsequent operations, etc.