This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

Quote: Recently, when I looked at the implementation of Java built-in lock, I saw that the performance cost of heavyweight lock is relatively high, mainly because the use of heavyweight lock needs to use a pthread_mutex_lock system call, resulting in Java programs need to switch between user state and kernel state, because I do not know what user state and kernel state are, so today I will study.

What is?

What is kernel mode and user mode in Linux?

In The Linux operating system, processes are divided into two types. One is the kernel process of the operating system itself, also known as the operating system process. Another non-operating system process is a user-defined program that runs on top of the capabilities provided by the operating system. We call this a user-like process.

As shown in the figure above, the job of an operating system is to manage cpus, memory, hard disks, network devices, input and output devices, and so on.

The code running in kernel mode can schedule CPU, allocate memory to reclaim memory, accept interrupt signal of mouse and key, etc.

The user mode is the mode in which the user program runs. The code running in this mode is restricted from performing certain operations, such as writing to the storage space of other processes. It cannot schedule the CPU and has to wait for the CPU to schedule.

Why is that?

Why do kernel-state and user-state exist?

Here’s an example:

In fact, creating a new process is a core function of any operating system, Linux or not, because it does a lot of low-level detailed work, consuming physical resources of the system, such as allocating physical memory, copying information from the parent process, copying setup page directories and page tables, and so on.

This obviously cannot be done by any program, which naturally leads to the concept of privilege levels, where the most critical rights must be performed by a highly privileged program. The benefits of this are:

  1. Not only can ensure the centralized management of resources, reduce the conflict of resource use;
  2. It can also reduce the development threshold of other programs, reduce the probability of error, and reduce the development and maintenance costs of programs.

User processes work in user mode, which is limited. Many operations involving hardware cannot be performed, but they can only ask the operating system working in kernel mode to help complete these operations and hand the results to the user process.

How does it work?

How do I switch from user mode to kernel mode? There are three ways:

A, the interrupt

To return to the operating system whenever needed, this alters the normal flow of CPU execution, leading to the introduction of the familiar term Interrupt. By interrupting, you can guarantee a return to the operating system, thereby giving control of the CPU to the operating system.

Interrupt literally means to interrupt the normal flow of execution, but note that it means to interrupt the flow rather than to terminate the flow, which is a different concept. Interrupts are a very important mechanism in operating systems, as described above: Interrupts are used to ensure that CPU control is given to the operating system so that it can perform certain operations.

For example, after a disk read/write operation is complete, the system switches to the disk read/write interrupt handler for subsequent operations.

System call

System call is an interface provided by the operating system for the user process to request the operating system to do some privileged operations, that is, a window to provide services for the user process. In Linux, you can run the man syscalls command to view all system calls provided by Linux.

The user process does not have permission to open the file. Therefore, the user process can only send an open() system call to the operating system to open the file. The operating system opens the file and hands the resulting file descriptor to the user process, which can then operate on the file. Further, if the user process wants to read a line of data from an open file, the user process does not have permission to read the file. The user process can only send a read() system call to the operating system to read the line of data, which the operating system can then hand to the user process.

As you can see, the system calls open() and read() are like functions. In fact, they are all functions, but they are specially provided by the operating system, usually written in assembly language or mixed with some assembly code, because they interact with the hardware.

The main process after the system call is initiated:

1. Initiate a system call requesting the operating system to perform certain operations, which generates a soft interrupt. 2. The soft interrupt causes the kernel to be trapped, and the CPU control is handed to the operating system. The operating system processes the interrupt, that is, performs the requested operation. 3. If all is normal, the operating system returns to the breakpoint and continues to execute the operation. 4. The user process obtains the operating result of the OPERATING system and continues to perform operations.

The heavyweight lock in the Java language is implemented using the pthread_mutex_lock system call.

Three, abnormal

When the CPU is executing a program running in user mode, some unexpected exception occurs, which triggers a switch from the current running process to the kernel-related program handling the exception, and then goes to the kernel state, such as a page missing exception.

Summary:

These three ways are the most important ways for the system to transition from user mode to kernel mode at runtime, in which the system call can be considered to be actively initiated by the user process, while exceptions and peripheral interrupts are passive.