Linux C Lock Pages

Source: Internet
Author: User

Virtual memory is broken down by page, and we can tell the system explicitly that a virtual memory page needs to be associated with the actual memory frame. In this way, the memory page is swapped in and will not be swapped out by the system. This behavior is called the Lock page (locking a page).

In general, the page swap out is transparent, the general program is not exposed to this layer. However, the lock page can provide us with the following benefits:

1, Speed: If your program on the speed of strict requirements, page errors cause the page to swap out will waste a certain event. Of course, in order to improve the program speed, you may also need to increase the priority of the program.

2, privacy security: Yes Lock page can make your program more secure. If your password happens to be on a page and the page is swapped out to the swap area of your hard drive, someone else will have more chances to get your password.

However you cannot lock too many pages (so that you rob other programs of resources)

Specific details:

Memory lock only for virtual pages, real memory we don't care. An actual memory page can correspond to multiple virtual memory pages, so long as a virtual memory page is locked, the actual physical memory page will not be swapped out.

You can not add a lock to a virtual memory page, there can be at most one lock.

Memory lock in two cases will be unlocked: 1, process self-unlock, 2, process exit, a process corresponding to a virtual memory, when its corresponding virtual memory is invalid, you can understand the virtual memory page unlocked (the door is not yet to lock dry? )。

Memory locks cannot be inherited by a quilt process, (note: Now in UNIX systems, once the fork operation has taken place, the child process and the parent process's virtual memory correspond to this same actual memory frame page. Although there is no inheritance lock, but actually enjoy the benefits brought by the

Because of the impact on 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 two virtual memory pages do not share memory, the kernel may still associate two virtual memory pages to the same memory frame. (For efficiency, two virtual memory pages have exactly the same data, why use two different memory frames to correspond to) if there is a virtual memory page where data modification occurs, the kernel will perform a write-time copy (Copy-on-write).

Related functions:

Mlock

int mlock (constvoid *addr, size_t len)// with addr as the starting address, Len locks up the page where the memory block is in length. (if the page is called, the page will be paged in, then locked up)// return value see the man Manual

Munlock:

int munlock (constvoid *addr, size_t len)// perform the opposite operation with Mlock, unlock

There are other functions: Mlockall and Unmallocall. Not a lot of introductions.

Linux C Lock Pages

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.