mlock and munlock Subroutine

Purpose

Locks or unlocks a range of process address space.

Library

Standard C Library (libc.a)

Syntax

#include <sys/mman.h>

int mlock (addr, len)
const void *addr;
size_t len;

int munlock (addr, len)
const void *addr;
size_t len;

Description

The mlock subroutine causes those whole pages containing any part of the address space of the process starting at address addr and continuing for len bytes to be memory-resident until unlocked or until the process exits or executes another process image. If the starting address addr is not a multiple of PAGESIZE, it is rounded down to the lowest page boundary. The len is rounded up to a multiple of PAGESIZE.

The munlock subroutine unlocks those whole pages containing any part of the address space of the process starting at address addr and continuing for len bytes, regardless of how many times mlock has been called by the process for any of the pages in the specified range.

If any of the pages in the range specified in a call to the munlock subroutine are also mapped into the address spaces of other processes, any locks established on those pages by another process are unaffected by the call of this process to the munlock subroutine. If any of the pages in the range specified by a call to the munlock subroutine are also mapped into other portions of the address space of the calling process outside the range specified, any locks established on those pages through other mappings are also unaffected by this call.

Upon successful return from mlock, pages in the specified range are locked and memory-resident. Upon successful return from munlock, pages in the specified range are unlocked with respect to the address space of the process.

The calling process must have the root user authority to use this subroutine.

Parameters

Item Description
addr Specifies the address space of the process to be locked or unlocked.
len Specifies the length in bytes of the address space.

Return Values

Upon successful completion, the mlock and munlock subroutines return zero. Otherwise, no change is made to any locks in the address space of the process, the surbroutines return -1 and set errno to indicate the error.

Error Codes

The mlock and munlock subroutines fail if:
Item Description
ENOMEM Some or all of the address range specified by the addr and len parameters does not correspond to valid mapped pages in the address space of the process.
EINVAL The process has already some plocked memory or the len parameter is negative.
EPERM The calling process does not have the appropriate privilege to perform the requested operation.
The mlock subroutine might fail if:
Item Description
ENOMEM Locking the pages mapped by the specified range would exceed the limit on the amount of memory the process may lock.