1MLOCK(2) Linux Programmer's Manual MLOCK(2)
2
3
4
6 mlock, mlock2, munlock, mlockall, munlockall - lock and unlock memory
7
9 #include <sys/mman.h>
10
11 int mlock(const void *addr, size_t len);
12 int mlock2(const void *addr, size_t len, unsigned int flags);
13 int munlock(const void *addr, size_t len);
14
15 int mlockall(int flags);
16 int munlockall(void);
17
19 mlock(), mlock2(), and mlockall() lock part or all of the calling
20 process's virtual address space into RAM, preventing that memory from
21 being paged to the swap area.
22
23 munlock() and munlockall() perform the converse operation, unlocking
24 part or all of the calling process's virtual address space, so that
25 pages in the specified virtual address range may once more to be
26 swapped out if required by the kernel memory manager.
27
28 Memory locking and unlocking are performed in units of whole pages.
29
30 mlock(), mlock2(), and munlock()
31 mlock() locks pages in the address range starting at addr and continu‐
32 ing for len bytes. All pages that contain a part of the specified ad‐
33 dress range are guaranteed to be resident in RAM when the call returns
34 successfully; the pages are guaranteed to stay in RAM until later un‐
35 locked.
36
37 mlock2() also locks pages in the specified range starting at addr and
38 continuing for len bytes. However, the state of the pages contained in
39 that range after the call returns successfully will depend on the value
40 in the flags argument.
41
42 The flags argument can be either 0 or the following constant:
43
44 MLOCK_ONFAULT
45 Lock pages that are currently resident and mark the entire range
46 so that the remaining nonresident pages are locked when they are
47 populated by a page fault.
48
49 If flags is 0, mlock2() behaves exactly the same as mlock().
50
51 munlock() unlocks pages in the address range starting at addr and con‐
52 tinuing for len bytes. After this call, all pages that contain a part
53 of the specified memory range can be moved to external swap space again
54 by the kernel.
55
56 mlockall() and munlockall()
57 mlockall() locks all pages mapped into the address space of the calling
58 process. This includes the pages of the code, data, and stack segment,
59 as well as shared libraries, user space kernel data, shared memory, and
60 memory-mapped files. All mapped pages are guaranteed to be resident in
61 RAM when the call returns successfully; the pages are guaranteed to
62 stay in RAM until later unlocked.
63
64 The flags argument is constructed as the bitwise OR of one or more of
65 the following constants:
66
67 MCL_CURRENT
68 Lock all pages which are currently mapped into the address space
69 of the process.
70
71 MCL_FUTURE
72 Lock all pages which will become mapped into the address space
73 of the process in the future. These could be, for instance, new
74 pages required by a growing heap and stack as well as new mem‐
75 ory-mapped files or shared memory regions.
76
77 MCL_ONFAULT (since Linux 4.4)
78 Used together with MCL_CURRENT, MCL_FUTURE,