#include <sys/types.h> #include <sys/mman.h>
int memcntl(caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask);
MT-Safe
The function memcntl() allows the calling process to apply a variety of control operations over the address space identified by the mappings established for the address range [addr, addr + len).
addr must be a multiple of the pagesize as returned by sysconf(3C) . The scope of the control operations can be further defined with additional selection criteria (in the form of attributes) according to the bit pattern contained in attr.
The following attributes specify page mapping selection criteria:
- SHARED
- Page is mapped shared.
- PRIVATE
- Page is mapped private.
The following attributes specify page protection selection criteria:
- PROT_READ
- Page can be read.
- PROT_WRITE
- Page can be written.
- PROT_EXEC
- Page can be executed.
The selection criteria are constructed by an OR of the attribute bits and must match exactly.
In addition, the following criteria may be specified:
where PROC_TEXT specifies all privately mapped segments with read and execute permission, and PROC_DATA specifies all privately mapped segments with write permission.
- PROC_TEXT
- Process text.
- PROC_DATA
- Process data.
Selection criteria can be used to describe various abstract memory objects within the address space on which to operate. If an operation shall not be constrained by the selection criteria, attr must have the value 0.
The operation to be performed is identified by the argument cmd. The symbolic names for the operations are defined in <sys/mman.h> as follows:
Lock future mappings
- MC_LOCK
- Lock in memory all pages in the range with attributes attr. A given page may be locked multiple times through different mappings; however, within a given mapping, page locks do not nest. Multiple lock operations on the same address in the same process will all be removed with a single unlock operation. A page locked in one process and mapped in another (or visible through a different mapping in the locking process) is locked in memory as long as the locking process does neither an implicit nor explicit unlock operation. If a locked mapping is removed, or a page is deleted through file removal or truncation, an unlock operation is implicitly performed. If a writable MAP_PRIVATE page in the address range is changed, the lock will be transferred to the private page.
- At present arg is unused, but must be 0
- to ensure compatibility with potential future enhancements.
- MC_LOCKAS
- Lock in memory all pages mapped by the address space with attributes attr. At present addr and len are unused, but must be NULL and 0 respectively, to ensure compatibility with potential future enhancements. arg is a bit pattern built from the flags:
- MCL_CURRENT
- Lock current mappings
- MCL_FUTURE
The mask argument must be zero; it is reserved for future use.
Locks established with the lock operations are not inherited by a child process after fork(). memcntl() fails if it attempts to lock more memory than a system-specific limit.
Due to the potential impact on system resources, all operations, with the exception of MC_SYNC , are restricted to processes with super-user effective user ID . The memcntl() function subsumes the operations of plock and mctl.
Upon successful completion, the function memcntl() returns a value of 0; otherwise, it returns a value of -1 and sets errno to indicate an error.
Under the following conditions, the function memcntl() fails and sets errno to: