#include <sys/mman.h> int mlock(const void *addr, size_t len); int munlock(const void *addr, size_t len); int mlockall(int flags); int munlockall(void); |
munlock() unlocks pages in the address range starting at addr and continuing for lenbytes. After this call, all pages that contain a part of the specified memory range can be moved to external swap space again by the kernel.
The flags argument is constructed as the bitwise OR of one or more of the following constants:
標籤 | 描述 |
---|---|
MCL_CURRENT | Lock all pages which are currently mapped into the address space of the process. |
MCL_FUTURE | Lock all pages which will become mapped into the address space of the process in the future. These could be for instance new pages required by a growing heap and stack as well as new memory mapped files or shared memory regions. |
munlockall() unlocks all pages mapped into the address space of the calling process.
Real-time processes that are using mlockall() to prevent delays on page faults should reserve enough locked stack pages before entering the time-critical section, so that no page fault can be caused by function calls. This can be achieved by calling a function that allocates a sufficiently large automatic variable (an array) and writes to the memory occupied by this array in order to touch these stack pages. This way, enough pages will be mapped for the stack and can be locked into RAM. The dummy writes ensure that not even copy-on-write page faults can occur in the critical section.
Memory locks are not inherited by a child created via fork(2) and are automatically removed (unlocked) during an execve(2) or when the process terminates.
The memory lock on an address range is automatically removed if the address range is unmapped via munmap(2).
Memory locks do not stack, i.e., pages which have been locked several times by calls tomlock() or mlockall() will be unlocked by a single call to munlock() for the corresponding range or by munlockall(). Pages which are mapped to several locations or by several processes stay locked into RAM as long as they are locked at least at one location or by at least one process.
Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process can lock and the RLIMIT_MEMLOCK soft resource limit instead defines a limit on how much memory an unprivileged process may lock.
標籤 | 描述 |
---|---|
ENOMEM | (Linux 2.6.9 and later) the caller had a non-zeroRLIMIT_MEMLOCK soft resource limit, but tried to lock more memory than the limit permitted. This limit is not enforced if the process is privileged (CAP_IPC_LOCK). |
ENOMEM | (Linux 2.4 and earlier) the calling process tried to lock more than half of RAM. |
EPERM | (Linux 2.6.9 and later) the caller was not privileged (CAP_IPC_LOCK) and its RLIMIT_MEMLOCK soft resource limit was 0. |
EPERM | (Linux 2.6.8 and earlier) The calling process has insufficient privilege to call munlockall(). Under Linux the CAP_IPC_LOCKcapability is required. |
For mlock() and munlock(): | |
EINVAL | len was negative. |
EINVAL | (Not on Linux) addr was not a multiple of the page size. |
ENOMEM | Some of the specified address range does not correspond to mapped pages in the address space of the process. |
For mlockall(): | |
EINVAL | Unknown flags were specified. |
For munlockall(): | |
EPERM | (Linux 2.6.8 and earlier) The caller was not privileged (CAP_IPC_LOCK). |
Since kernel 2.6.9, if a privileged process calls mlockall(MCL_FUTURE) and later drops privileges (loses the CAP_IPC_LOCK capability by, for example, setting its effective UID to a non-zero value), then subsequent memory allocations (e.g., mmap(2), brk(2)) will fail if the RLIMIT_MEMLOCK resource limit is encountered.
On POSIX systems on which mlockall() and munlockall() are available,_POSIX_MEMLOCK is defined in <unistd.h> to a value greater than 0. (See alsosysconf(3).)