mlock(2) System Calls Manual mlock(2) mlock, mlock2, munlock, mlockall, munlockall - C (libc, -lc) #include int mlock(const void addr[.len], size_t len); int mlock2(const void addr[.len], size_t len, unsigned int flags); int munlock(const void addr[.len], size_t len); int mlockall(int flags); int munlockall(void); mlock(), mlock2() mlockall() , . munlock() and munlockall() perform the converse operation, unlocking part or all of the calling process's virtual address space, so that pages in the specified virtual address range can be swapped out again if required by the kernel memory manager. . mlock(), mlock2() munlock() mlock() , addr len . , , , , , ; . mlock2() , addr len . flags. flags 0 : MLOCK_ONFAULT , , , - (fault). flags 0, mlock2() mlock(). munlock() , addr len . , , , , . mlockall() munlockall() mlockall() , . , , , , , . , ; . flags : MCL_CURRENT , . MCL_FUTURE , . , , , , . MCL_ONFAULT ( Linux 4.4) MCL_CURRENT, MCL_FUTURE . ( MCL_CURRENT) ( MCL_FUTURE) , (faulted in). MCL_CURRENT , mlockall() . MCL_FUTURE , - . MCL_ONFAULT MCL_CURRENT MCL_FUTURE . MCL_FUTURE, (, mmap(2), sbrk(2), malloc(3)), , (. ). : SIGSEGV. munlockall() , . On success, these system calls return 0. On error, -1 is returned, errno is set to indicate the error, and no changes are made to any locks in the address space of the process. EAGAIN (mlock(), mlock2() munlock()) . EINVAL (mlock(), mlock2() munlock()) addr+len addr (, ). EINVAL (mlock2()) flags. EINVAL (mlockall()) flags MCL_ONFAULT MCL_FUTURE MCL_CURRENT. EINVAL ( Linux) addr . ENOMEM (mlock(), mlock2() munlock()) . ENOMEM (mlock(), mlock2() munlock()) ( ). , : . ENOMEM (Linux 2.6.9 ) RLIMIT_MEMLOCK, , . (CAP_IPC_LOCK). ENOMEM (Linux 2.4 ) . EPERM (CAP_IPC_LOCK) . EPERM (munlockall()) (Linux 2.6.8 ) (CAP_IPC_LOCK). Linux Linux, mlock(), mlock2() munlock() addr . , POSIX.1 , mlock() munlock() , addr , . The VmLck field of the Linux-specific /proc/pid/status file shows how many kilobytes of memory the process with ID PID has locked using mlock(), mlock2(), mlockall(), and mmap(2) MAP_LOCKED. mlock() munlock() mlockall() munlockall() POSIX.1-2008. mlock2() Linux. POSIX-, mlock() munlock(), _POSIX_MEMLOCK_RANGE , PAGESIZE ( ) sysconf(_SC_PAGESIZE). POSIX-, mlockall() munlockall(), _POSIX_MEMLOCK, , (. sysconf(3)). mlock() munlock() mlockall() munlockall() POSIX.1-2001, POSIX.1-2008, SVr4. mlock2() Linux 4.4, glibc 2.27. , , : . , ( ) . sched_setscheduler(2). , , , . , , , , . (, (suspend) , ). , mlockall() (page fault), , , . , () , . , . , . , fork(2), () execve(2) . MCL_FUTURE MCL_FUTURE | MCL_ONFAULT mlockall() , fork(2), execve(2). Note that fork(2) will prepare the address space for a copy-on-write operation. The consequence is that any write access that follows will cause a page fault that in turn may cause high latencies for a real-time process. Therefore, it is crucial not to invoke fork(2) after an mlockall() or mlock() operation--not even from a thread which runs at a low priority within a process which also has a thread running at elevated priority. , munmap(2). , , mlock(), mlock2() mlockall() , munlock() munlockall(). , , , . mlockall() MCL_FUTURE , , , MCL_FUTURE . MLOCK_ONFAULT mlock2() MCL_ONFAULT mlockall() , , () . - . Linux 2.6.8 (CAP_IPC_LOCK), RLIMIT_MEMLOCK . Linux 2.6.9, , RLIMIT_MEMLOCK . Linux 4.8 (. ., CAP_IPC_LOCK) , , , addr len , . - << >>, , RLIMIT_MEMLOCK, mlock() mlock2() , . Linux 4.9. In Linux 2.4 series of kernels up to and including Linux 2.4.17, a bug caused the mlockall() MCL_FUTURE flag to be inherited across a fork(2). This was rectified in Linux 2.4.18. Since Linux 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 nonzero value), then subsequent memory allocations (e.g., mmap(2), brk(2)) will fail if the RLIMIT_MEMLOCK resource limit is encountered. mincore(2), mmap(2), setrlimit(2), shmctl(2), sysconf(3), proc(5), capabilities(7) () aereiae , Alexey , Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , ITriskTI , Max Is , Yuri Kozlov , ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . mlock(2)