Using mlock() and munlock()

The mlock() interface causes the pages in the specified address range to be locked in a physical memory. References to locked pages in a process do not result in page faults that require an I/O operation. Because an I/O operation interferes with normal operation of virtual memory, and slowsother processes, the use of mlock() is limited to the superuser. The limit of number of pages that can be locked in a memory is dependent on the system configuration. The call to mlock() fails if this limit is exceeded. For more information, see the mlock(3C) man page.

The munlock() interface releases the locks on physical pages. If multiple mlock() calls are made on an address range of a single mapping, a single munlock() call releases the locks. However, if different mappings to the same pages are locked by mlock(), the pages are not unlocked until the locks on all the mappings are released.

Removing a mapping also releases locks, either through being replaced with the mmap() operation or removed with the munmap() operation.

The copy-on-write event that is associated with a MAP_PRIVATE mapping transfers a lock on the source page to the destination page. Thus locks on an address range that includes MAP_PRIVATE mappings are retained transparently along with the copy-on-write redirection. For more information about creating and using mappings, see Creating and Using Mappings.

For more information, see the mlock(3C) and munlock(3C) man pages.