paging

In order to save memory and improve usage efficiency, the operating system will split memory into small pieces for use. In Linux, each piece is called a page and has a size of 4K

What is a virtual address space

In a multitasking operating system, each process runs in its own Virtual memory, called the Virtual Address Space.

Why have a virtual address space

To isolate processes from each other.

If you allow processes to operate directly on physical memory, it is likely that different processes will operate on the same physical memory address, resulting in mutual influence. So the virtual address space is abstracted as a middle layer, so that a virtual address space is mapped to a physical address space

As shown in the figure above, two pieces of virtual memory map themselves to physical memory via the page table. The process can only see the virtual memory (of course, it does not know that the memory is virtual). The process can only “run” in the virtual address space and only operate on its own virtual memory, so processes do not affect each other

Size and allocation of the virtual address space

The operating system needs to allocate its own virtual memory for each process. How much virtual memory should it allocate?

Before there was no virtual address space, physical memory was allocated according to the needs of the process. However, with the virtual address space, the allocation strategy can be changed. First, allocate the virtual address space to a larger size, but do not immediately establish a mapping with the physical memory, but when used, use as much, establish as much

In this way, the physical memory size is the same, but the memory allocation flexibility is greatly improved, and the process does not have to worry about the address will conflict with other processes, even though

In 32-bit operating systems, the operating system allocates a maximum of 4 gigabytes (2 ^ 32) of virtual address space per process

User space versus kernel space

Although the operating system allocates virtual address space for each process, not all areas of the virtual address space can be used by the process

The operating system divides the virtual address space into user space and kernel space. For a 32-bit operating system, the ratio of user space to kernel space is 3:1 in the Linux virtual address space and 2:2 in the Window

Why is there kernel space

For system security, modern operating systems generally force user processes not to operate directly on the kernel, and all system calls must be made to the kernel.

But the kernel also runs in memory. To prevent user processes from interfering, the operating system allocates a separate memory area for the kernel. This area is called kernel space.

Mapping between kernel space and user space

In Linux, when the system starts, the kernel needs to be loaded into the kernel space of physical memory to run.

For a process, physical memory is invisible to it, but it needs the kernel to make various system calls, and the kernel is actually on physical memory. How to resolve this contradiction?

Linux has figured out a way to “move” the kernel to virtual memory by mapping the kernel space in the virtual address space of a process to the kernel space in physical memory. And when the process sees it, it has the kernel in its own memory, and it can make various system calls through the kernel

In Linux, kernel space is persistent, and the kernel space in the virtual address space of all processes maps to the same kernel space in physical memory

As shown in the figure above, the kernel space of process A and process B is mapped to the same physical memory region, while the user space addresses are mapped to different physical memory regions

User mode and kernel mode

When a process executes a system call and gets stuck in kernel code, it is said to be in kernel state (or kernel-state for short).

Let’s say there’s a Python program that writes files

With the open ('/test. TXT ', 'a +') as fw: # open file fw. Write (' content ')Copy the code

When it runs, it is a process and has its own virtual address space, including user space and kernel space. At first the process runs in user space, but when it goes to fw.write(‘ content ‘), it finds itself writing a file to disk, which only the kernel can do. The kernel provides a write() system-level function. The fw.write(‘ content ‘) code ultimately executes the write() system call to write the file

The initial Python code is loaded into memory in user space and runs when the write() system call is executed. Write () is run in kernel space, but they are all part of the same process and only a state switch occurs when the system call is executed.

When the program is running in user space, the process is in user mode, and when the program is running in the kernel, the process is in kernel mode. The switch between the two states is called the upstream switch

The above text switch is resource-intensive, so avoid context switches as much as possible

There are three ways to switch from user mode to kernel mode

In addition to the system call mentioned above, there are two modes that cause the user mode to switch to kernel mode

  • System call: an active user mode process requires switching to kernel mode of a way to use the operating system user mode process through the system call application services provided by the program to finish the work, and the mechanism of the system call its core or use the operating system for the user to open an interrupt to implement in particular, for example, the Linux int 80 h.
  • Exception: When the CPU is executing a program that is running in user mode, some unknown exception occurs. At this time, the current running process will be triggered to switch to the kernel-related program that handles this exception, and then the kernel mode will be switched, such as the page missing exception.
  • Peripheral interruption: After the peripheral equipment to complete the operation of the user request, will send a corresponding to the CPU interrupt signal, the CPU will be suspended for the next article is going to execute commands to perform and interrupt signals corresponding handler, if previously executed instructions are under the user mode application, then the transformation process also occurs naturally from user mode to kernel mode switch.