In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is user state and kernel state". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn "what is user state and kernel state"!
Content outline
Short story
Zhang San is a junior Java development engineer (low authority) in a technology company. At present, he is in the office code on the 15th floor. The resources provided by the company are only a set of computers (user mode). Thinking about the housing price on this front line, Zhang San feels a lot of pressure, so he sets a goal for himself. He must be a technical director, take root in the front line, struggle B Zhang San, and finally become a technical director (high authority) after 5 years of struggle. After that, Zhang San moved to the 30th floor and can apply to the Resources Department (system call) at any time to apply for all kinds of resources and obtain the company's confidential information (kernel state), which is to reach the peak of life.
Through this story, we find that the scope of resources with low permissions is smaller, while the scope of resources with high permissions is larger, and the so-called "user mode and kernel mode are only the scope of resources with different permissions".
C P U instruction set permissions
Before talking about user mode and kernel state, it is necessary to talk about C P U instruction set. Instruction set is the medium for C P U software to direct hardware execution. Specifically, each assembly statement corresponds to a C P U instruction, and a lot of C P U instructions together can form one or even multiple sets. The set of instructions is called C P U instruction set.
At the same time, the C P U instruction set has permission levels. Just imagine, the C P U instruction set can directly operate the hardware. If the instruction operation is not standard, the errors caused will affect the entire computer system. For example, if you write a program, because you are not familiar with the hardware operation, the operating system kernel, and all other running programs, may suffer irreparable errors as a result of operational errors, and finally have to restart the computer.
The operation of the hardware is very complex, there are many parameters, and the probability of problems is quite high, so it must be operated cautiously, which is an arduous task for developers and will increase the burden. At the same time, developers are not trusted in this respect, so the operating system kernel directly shields developers from the possibility of hardware operations and does not let you encounter these C P U instruction sets.
High emotional quotient
Ring 0 is called kernel state and runs entirely in the operating system kernel.
Ring 3 is called user mode and runs in the application
Low EQ
Execute code in kernel space with ring 0 protection level, have all operation permissions on hardware, execute all C P U instruction sets, access memory at any address, any exception in kernel mode is catastrophic and will cause the whole machine to shut down
In user mode, it has ring 3 protection level, and the code does not have direct control over the hardware, nor can it directly access the memory of the address. The program accesses the hardware and memory by calling the system interface (System Call APIs). In this protected mode, even if the program crashes, it can recover. On the computer, most programs run in user mode.
User state and kernel state
Clearance of C P U instruction set permissions, now the user state and kernel state is very simple, the concept of user state and kernel state is the difference between C P U instruction set permissions, in the process to read and write I O, must use the ring 0 level C P U instruction set, but at this time the C P U instruction set operation authority is only ring 3, in order to operate the ring 0 level C P U instruction set The operation permission level of C P U switching instruction set is ring 0 CPU and then executes the corresponding ring 0 level C P U instruction set (kernel code). The kernel code executed will use the kernel stack of the current process.
PS: each process has two stacks, the user stack and the kernel stack, and the use of the application state and kernel state
The space between user state and kernel state
In the use of memory resources, the operating system has also imposed restrictions on user mode and kernel state, and each process creation will be assigned a "virtual space address" (if you don't understand, please refer to my other article "15 minutes!" To help Xiaobai understand the memory of the operating system, take the Linux32-bit operating system as an example, its addressing space is 4G (2 to the 32th power), while the operating system divides the virtual control address into two parts, one is kernel space, the other is user space, the high-order 1G (from virtual address 0xC0000000 to 0xFFFFFFFF) is used by the kernel, and the low-order 3G (from virtual address 0x00000000 to 0xBFFFFFFF) is used by each process.
From the figure above, we can see that the whole Linux system is divided into user mode and kernel state through system calls. In order to enable applications to access kernel resources, such as CPU, memory, and Imax O, the kernel must provide a set of general access interfaces, which are called system calls.
Library function is to shield these complex underlying implementation details, reduce the burden of programmers, and thus pay more attention to the upper logic implementation. It encapsulates system calls and provides simple basic interfaces to programmers.
Shell, as its name implies, means shell, which is like a shell wrapped around the kernel. It is a special application, commonly known as the command line. Shell is also programmable, it has a standard Shell syntax, in line with its syntax of the text called Shell script, many people will use Shell script to achieve some common functions, can improve work efficiency.
Finally, what will cause the user mode to switch to the kernel state?
System call: the way in which the user-mode process actively switches to the kernel state. The user-mode process requests resources from the operating system to complete the work through the system call. For example, fork () is a system call to create a new process. The core mechanism of the system call uses an interrupt that the operating system is particularly open to the user, such as Linux's int 80h interrupt, which can also be called a soft interrupt.
Exception: when an unexpected exception occurs when C P U executes a process in user mode, the current running process will switch to the kernel-related process that handles the exception, that is, to the kernel state, such as a page-missing exception
Interrupt: when C P U is executing a process in user mode, after the peripheral device completes the operation requested by the user, it will send a corresponding interrupt signal to C P U. At this time, C P U will suspend the execution of the next instruction to be executed and transfer it to the handler corresponding to the interrupt signal, that is, it will switch to the kernel state. If the read and write operation of the hard disk is completed, the system will switch to the interrupt handler of the read and write of the hard disk to perform the following operations.
Thank you for your reading. the above is the content of "what is user state and kernel state". After the study of this article, I believe you have a deeper understanding of what is user state and kernel state. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.