C Programming learning in linux -- how to lock the memory and lock the linuxc Programming

Source: Internet
Author: User

C Programming learning in linux -- how to lock the memory and lock the linuxc Programming

In Linux, request page scheduling is implemented. Page scheduling means that pages are exchanged from the hard disk on demand and are exchanged when they are no longer needed. This allows the virtual address space of each process in the system to have no direct connection with the total physical memory, because the swap space on the hard disk can give the process an illusion that the physical memory is almost infinite.

The switch is transparent to processes, and applications generally do not need to worry (or even do not need to know) about Kernel page scheduling. However, in the following two cases, the application may affect the page scheduling of the system:

Determinism)

Behaviors that must be determined by applications with strict time constraints. If some memory operations cause page errors, resulting in expensive disk operations, the speed of the application cannot meet the requirements, and the scheduled operations cannot be performed on time. If you can ensure that the required pages are always in the memory and never exchanged into the disk, the application can ensure that memory operations do not cause page errors and provide consistent and deterministic program behavior, thus, performance is provided.

Security)

If the memory contains private secrets, the secrets may be stored on the hard disk in non-encrypted mode by PAGE scheduling. For example, if a user's private key is normally stored on a disk in an encrypted way, a backup of the encrypted key in memory is finally stored in the swap file. This may be unacceptable in a highly secure environment. Such an application can request to keep the key in the physical memory. Of course, changing the kernel behavior will negatively affect the overall system performance. When the page is locked in the memory, the security of one application may be improved, but this can make the page of another application swap out. If the kernel is designed to be trustable, it always best exchanges pages (pages that seem to be least usable in the future ).

If you do not want to replace a block with a disk when it is not used for the time being, you can lock the memory. The related functions are as follows:

#include <sys/types.h>  int mlock(const void *addr,size_t length)  int munlock(void *addr,size_t length)  int mlockall(int flag)  int munlockall(void)

Function:

Mlock locks a memory area. addr indicates the memory address and length indicates the length to be locked.

Munlock contacts the locked memory.

Mlockall locks Multiple Memory pages at a time. The flag value has two pages for MCL_CURRENT to lock the memory used, and MCL_FUTURE locks the Address Space Memory Page allocated to the process. Munlockall is used to unlock the memory.

Note: Only Super Users can lock and unlock the 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.