Linux C lock pages, linuxlockpages
Virtual Memory is divided by page. We can clearly tell the system that a virtual memory page needs to be associated with the actual memory frame. In this way, the memory page is swapped in and won't be swapped out by the system. This line is called the locking a page ).
In general, the switch-in and switch-out of pages are transparent, and generally the program cannot access this layer. However, the lock page can bring us the following benefits:
1. Speed: If your program has strict requirements on speed, page errors may result in page feed-in and out, which will waste some events. Of course, in order to increase the program speed, you may also need to increase the program priority.
2. Privacy Security: Yes, locking pages can make your programs more secure. If your password is stored on a page, and this page is replaced with the hard disk swap area, others will have more opportunities to get your password.
However, you cannot lock too many pages (in this way, the resources of other programs are snatched)
Details:
The memory lock only targets virtual pages. We don't care about the actual memory. A real memory page can correspond to multiple virtual memory pages. As long as one virtual memory page is locked, the actual physical memory page will not be swapped out.
You cannot stack a lock on a virtual memory page. You can only have one lock at most.
The memory lock will be unlocked in two cases: 1. The process is unlocked by itself. 2. The process exits. A process corresponds to a virtual memory. In this case, the corresponding virtual memory is invalid, I can understand that the virtual memory page has been unlocked (why are there no locks required ?).
The memory lock cannot be inherited by the quilt process. (Note: In the current UNIX system, once the fork operation produces a sub-process, the virtual memory of the sub-process and the parent process corresponds to the same actual memory frame page. Although there is no inheritance lock, but actually enjoy the benefits)
As it may affect other processes, only super users can lock pages. However, any process can unlock the page.
The system limits how much memory a process can lock.
Even if the two virtual memory pages do not share the memory, the kernel may still associate the two virtual memory pages with the same memory frame. (For efficiency, the two virtual memory pages have identical data. Why do we need two different memory frames?) If there is a virtual memory page with data modification, the Kernel performs copy-on-write ).
Related functions:
Mlock:
Int mlock (const void * addr, size_t len) // lock the page of the memory block with the length of len as the starting address of addr. (If the page is called, the page will be transferred and locked.) // for the returned values, see the man manual.
Munlock:
Int munlock (const void * addr, size_t len) // execute operations opposite to mlock to unlock
There are other functions: mlockall and unmallocall. I will not discuss it much.