Linux memory protection mechanism and inter-process communication principle

Source: Internet
Author: User

Linux virtual memory enables each process to have a unified and consistent 4G address space. The main reason for supporting this function is that when the page is not in the physical memory, due to page missing exceptions, the exception handler will swap data into the physical memory from the swap space, then re-execute the command that causes the exception, and the process can be accessed normally. The process itself does not need to be concerned about the process.

3G lower process address space is the user space, namely the user State, and the required CPU permission is 3; 1g higher is the kernel state, and the required CPU permission is 1. Linux uses these two permission values to isolate user and kernel states. However, the two communication methods can only communicate through the system call method confirmed by the system, that is, to access the kernel-state data, it must be accessed through the system call, the main process of system calling is to modify the status mark of a register-the CPU privilege value, so that you can access the data in the kernel state.

In addition to two different States, Linux memory protection also isolates each process, that is, the space of the other side is invisible between different processes. Each process has its own address space, and there is only one process address space at any time, which is why only one process is running at any time (single CPU ). When the process is switched, the base address register of the page table is modified accordingly. By modifying this register, the address space is switched. This is also why the process cannot access other process address spaces-the base address register of the page table used when it is running must be its, the mapped address is definitely its own, so that it can prevent other processes from being damaged. The essence of inter-process communication is to use some space in the kernel state for data transmission (multiple copies, copy the data in the kernel-state buffer to the user-state buffer copy_to_user ). Generally, MMAP and shared memory are implemented in two different ways; in this example, Shared Memory creates a new segment (shared memory area) in the address space of a process, and then mounts the region to another process (the process to communicate ), at this time, the virtual addresses of the two are not necessarily the same, but their physical addresses are the same, so they only save one copy in the memory, their access is directly accessed through the virtual address after mounting, and the kernel space copy is no longer needed. This is why shared memory is the fastest communication among processes. While MMAP uses files as a transfer station to achieve data communication, which is essentially similar to shared memory.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.