From last year I began to systematically learn the operating system principle, read a lot of content, also wrote a blog post, but on the user state and the kernel state this has been some vague, perhaps before writing the article did not write this piece of the relationship, the brain is still some paste reasons. In fact, the feeling of paste is not learned, we should pay attention to this problem, do not be paralyzed by their own

Understanding this question probably won’t help you in an interview, but it’s certainly not what you’re likely to get asked. But figuring this out, for our understanding of the whole computer system, multithreading, is incredibly meaningful, and it will give you a lot of insight into what you’re going to learn. You’re sure to say, “Oh, that’s so amazing!” This is especially useful when learning operating system principles and helping you understand threads and processes

The resources

Thanks to learning website: B station –> let the information is no longer difficult to find

  • 10 minutes to understand user mode and kernel mode
  • User mode, kernel mode, and interrupt
  • Process and thread PCB TCB
  • Mapping between Java threads and Linux kernel threads
  • Boastful user mode and kernel mode

CPU instruction set permissions

Before I start explaining user versus kernel states, I think it’s worth mentioning the CPU instruction set

1. Understand the CPU instruction set

Instruction set is the CPU to achieve software command hardware execution of the medium, to be specific, each assembly statement corresponds to a CPU instruction, CPU instruction is more than one, but by a very many CPU instruction set together, formed a, or even multiple sets of each instruction set called: CPU instruction set. As time went on, CPU instructions became more and more, from dozens of instructions at the beginning to thousands now

Inter, AMD CPU is a complex instruction set CPU, CPU instructions support up to several thousand, each instruction refers to a kind of mathematical operations, such as addition, subtraction, multiplication and division of 4 kinds of mathematical calculations in the CPU is 4 CPU instructions, and so on, we imagine

CPU instruction set development to now there are many, typical Intel CPU support: EM64T, MMX, SSE, SSE2, SSE3, SSE4A, SSE4.1, SSE4.2, AVX, AVX2, AVX-512, VMX and other instruction sets

In these instruction sets, each CPU instruction has a unique and non-repeating instruction number. The control unit in THE CPU hardware can parse and identify the NUMBER of CPU instructions that the program wants to execute, and then direct the relevant hardware to execute the relevant operations, so assembly language is regarded as the lowest level of programming language

It is generally believed that the mathematical calculation formula directly supported by CPU hardware is the most efficient. Because the CPU instruction set contains this mathematical formula, a mathematical calculation can be completed with only one instruction. On the other hand, multiple CPU instructions are required to complete the mathematical calculation in a combined way. Multiple CPU instructions are required to execute, which is N times more complex in space and time complexity. So please understand the source of CPU execution efficiency, of course, the instruction set is only one of the factors that restrict CPU execution efficiency, but this factor is definitely a heavy, decisive factor

2. CPU instruction permission level

CPU instructions are graded with authority –> Just imagine, CPU instructions can operate hardware directly, if the instruction operation is not standardized, caused by the error will affect the entire computer system. For example, you write a program, but because you are not familiar with the hardware operation, there is a problem, so how much influence? Is the entire computer system, operating system kernel, and all the other running programs, will be irreparably wrong because of your operation error, so you have to restart the entire computer

However, the operation of hardware is very complex, with many parameters and a large chance of problems, so it must be carried out with extreme caution. This is a difficult task for individual developers, and individual developers are not trusted in this respect. So the operating system kernel directly shields the individual developer to the hardware operation possibility, I do not let you can encounter these CPU instructions, you can also make a big news out ~

In this respect, the system kernel has encapsulated the hardware operation and provided the standard function library externally, which makes the operation simpler and safer. For example, if we want to open a file, the corresponding C standard function library is fopen(), which encapsulates the kernel system function open().

Because of this requirement, hardware vendors directly provide hardware-level support by setting permissions on CPU instructions. Different levels of permissions are limited in how many CPU instructions can be used. Take Inter CPU as an example, Inter divides the CPU instruction operation permissions into 4 levels:

  • ring 0
  • ring 1
  • ring 2
  • ring 3

Ring 0 has the highest permission and can use all CPU instructions, while Ring 3 has the lowest permission and can only use conventional CPU instructions. This level of permission cannot use instructions to access hardware resources, such as IO read and write, network card access, and memory application

The Linux kernel uses the ring 0 and ring 3 permissions

  • ring 0Is calledKernel modeAnd completely inOperating system kernelRun by dedicatedA kernel threadPerforms its tasks in the CPU
  • ring 3Is calledUser modeIn theThe applicationRun fromUser threadsPerforms its tasks in the CPU

In Linux, all operations on hardware resources must be performed in the kernel state, such as reading and writing IO, and network operations

User mode, kernel mode

You can also take a look at this article: Big Talk user mode and kernel mode

1. What are user mode and kernel mode

Everyone in learning operating system principle, multithreading must have heard of this: system interruption will cause the CPU from the user -> kernel state switch, although there will be some explanations, but I think there should be a lot of people do not understand the user state, kernel state is what east east

What are user-mode and kernel-mode? It’s not really that complicated. If you want to read and write I/O in the application, then you must use the CPU instruction of ring 0 level, while the application CPU instruction permission is only Ring 3. This line of code must be executed in the system kernel that has ring0 privileges, which inevitably causes the CPU to switch from user mode to kernel mode. This takes the form of code switching from the user thread where the application is running to the kernel thread in the kernel, and that’s it

The user thread and kernel thread will be explained below, and you will understand it thoroughly when you read the explanation of user thread and kernel thread. If you are still confused, please continue to read later

2. Differences between user mode and kernel mode in resources

The level of CPU instruction permission will be reflected in the operation of resources, the difference is that the high CPU instruction permission can operate more, more core resources, typical is memory and hardware resources

Although conceptually speaking: user mode, kernel mode is only the difference between CPU instruction permissions, but in the program, system kernel design, there must be a corresponding operation mechanism to support, which is reflected in two significant differences:

  1. User-mode code must be executed by the user thread, kernel-mode code must be executed by the kernel thread
  2. User mode, kernel mode or user thread, kernel thread can use different resources, especially in memory resources. The Linux kernel allocates 4 gb of virtual memory space to each process
    • User mode:–> Only 0-3g memory address can be operated
    • Kernel mode:–> all memory addresses from 0 to 4G can be operated, especially the high level addresses from 3 to 4G must be operated by the kernel mode, because all processes use the same block of high level addresses, reserved for the use of the system kernel 1G physical memory
  3. All access to hardware resources and system kernel data must be performed in kernel mode

Add:

The 4GB address space of Linux process, 3G-4G part is shared by everyone (refers to the kernel-mode logical address of all processes is the same physical memory address), is the kernel-mode address space, where the entire kernel code and all kernel modules, as well as the data maintained by the kernel

3. System call

I won’t go into the details of system calls here, but if you don’t know, read my article: Android Base – Operating system

Hardware resources, system kernel data access must be executed by the kernel state, which involves a switch from the user state to the kernel state, the specific need to understand the user thread, the kernel thread after the clear understanding

There are three ways to switch from user state to kernel state: system call, interrupt, and exception. Among them, system call is also realized by using interrupt. Here we mainly talk about system call, and the following operations need to be executed in kernel state:

  • Perform operations on and obtain process information
  • File operations
  • Hardware Device Operations
  • Query system kernel information and hardware information
  • Communication, process communication, malloc memory requisition, pipe open pipe

The man Syscalls directive can see all the Linux system kernel call methods

User threads and kernel threads

1. What is process

The interview will ask what is the process, what is the thread, generally we answer to the process is: the system scheduling resource minimum or basic unit, how to understand, I think many people are not particularly clear

A task_struct is a data structure or an object that stores information about a particular resource

The information it stores is this:

For structs do not understand the C language syntax, very simple, is the Java object, but the structure has only properties and no methods

The task_struct structure has a special name: PCB –> PROCESS control block, also called PROCESS control block, PCB data stored in the PROCESS of 4G memory virtual address in the kernel state, that is, 3-4G memory in the section, obviously is not accessible in user mode, want to access must be switched from user mode to kernel state. It is also dangerous to expose such core data directly to user programs

A process is a task_struct structure called a PCB in the kernel, and a PCB TABLE is used to store and schedule processes

2. What are threads

In the kernel, a thread is a task_struct structure, like a process. When a thread is new, it copies the PCB of the process to itself, and then adds the PC program counter, SP stack, State, and register information that the PCB does not have. The task_struct structure of a thread is called TCB –> Thread control block. A thread is essentially a list of tasks, a stack frame

Thread can share heap memory resource, how to do it, it is really very simple, the process resource information PCB copy to their own TCB, so here we experience

3. Change your perspective on processes and threads

  • process–> is the minimum and basic unit for applying resources to the system kernel
  • thread–> is the smallest, basic unit of competing CPU resources

I find it easier to understand that processes are competing for memory resources and threads are competing for CPU resources

4. Kernel threads

Thread subdivision: kernel thread, user thread, kernel thread’s TCB and process’s PCB are stored in one location, all in 3-4G kernel address memory. Kernel threads are scheduled in the system kernel, not user threads

The kernel thread also has its own stack frame so all the ring0 code is iosing to the kernel thread to execute

5. User threads

With the kernel thread, why also have to design the user thread, do not think is in order to and ring0,ring3 instruction permissions corresponding, is the permission responsibility is clear. System kernel operation, hardware operation, high security permission code must be ioo to execute in the kernel, the user can not touch, the user’s own low permission code to execute in the user’s own thread is good, so the design code layer, expansion performance is good, do not affect each other

Linux uses the 1:1 threading model by default, which means that if you have a user thread, you have to start a corresponding kernel thread in the kernel and bind the user thread to the kernel thread. Of course, there are other corresponding ways, which rely on third party implementation of the scheme. The Dart library is a 1:1 thread model. The Dart library is a 1:1 thread model. The Dart library is a 1:1 thread model

It should be noted that the Java JVM uses the default Linux library, known as the PThread, 1:1 threading model. When Java creates a new Thread, it creates a user Thread and a kernel Thread, and then binds the user Thread to the inner Thread. The code of ring3 is executed in the user Thread, and the code of ring0 is switched to the kernel Thread for execution, and then receives the scheduling of the system kernel using the kernel Thread. After the kernel thread grabs the CPU time slice, the user thread activates the execution code

The TCB of the user thread is stored in the heap memory of the 0-3g virtual address space of the process and is invisible to the system kernel, so the scheduling of the thread is undertaken and processed by the kernel thread

6. Switch between user thread and kernel thread

As you can see, the difference between a user thread and a kernel thread is the location of the TCB, the level of code permission to execute, and then the kernel thread receives scheduling instructions from the operating system kernel scheduler

What about switching between user threads and kernel threads? The user thread and the kernel thread have their own stack frames, register values, cpu-loaded cache and other data belonging to their own threads. The switch from the user thread to the kernel thread will also involve the switch of these threads’ context, and the performance loss is here

Generally speaking, ioo operations are ioo to execute in the kernel thread, so the IO code will involve the switch from the user state to the kernel state, where the performance loss is the switch of the thread context. The key is that IO operation will cause frequent switch from the user state to the kernel state and then to the user state. The focus of optimization is to reduce switching, so MMAP is important. Mmap implements data reading and writing directly to the hard disk without using system interrupts or executing system functions in kernel mode, eliminating the need to switch from user mode to kernel mode, so the performance is good and the efficiency is high

Ok so much, so understanding on the line, do not think too complicated, that is what is going on. User mode = user thread, kernel mode = kernel thread, the difference between the two is the difference between the scope of memory operation, the difference between the operation permissions of instructions

7. The JVM thread

What about threads created by the JVM

Kernel threads must be created and scheduled by the system kernel, but user threads must also be used in user programs, which is a must for security and kernel isolation. The binding relationship between kernel thread and user thread is discussed in the design of general operating system. There are three schemes:

  • 1:N
  • N:N
  • 1:1

The Linux kernel uses the 1:1 model by default, but that is not to say that it cannot be changed. Linux kernel for external interface unified design of the Posix specification, the interface is fixed, specific how to implement the design of higher level, language level coroutines is designed on this basis

I found a fairly straightforward explanation for user-thread design in the JVM:

NPTL (Native POSIX Thread Library) support has been used in Linux kernel 2.6 since then, but threads are by nature lightweight processes. The JVM provides user programs with create, synchronize, schedule, and manage thread functions to control user threads without requiring user/core mindset switching

Note that the JVM provides a mechanism that does not rely on, does not involve, does not trigger the kernel thread scheduling mechanism, the user level of the user thread scheduling mechanism, so that it is connected to the JVM thread scheduling mechanism. So that it’s hard to understand at the system level

See the mapping between Java threads and Linux kernel threads