TOC PREV NEXT INDEX

POSIX 1.b Migration Guide


Memory Locking

The memory locking interface has changed from Draft 9 to POSIX.1b. Some Draft 9 facilities have been discontinued (see below), while a new feature has been introduced to restrict memory locks to the current address space.

For additional information, refer to "Changes from Draft 9 to POSIX.1b" later in this chapter.

Locking the Specific Address Space

The ability to restrict memory locking for data, text, or stack segments of a process under Draft 9 (DATALOCK, TXTLOCK, STKLOCK flags) no longer exists. The only behavior supported is the ability to lock a specified address range (REGLOCK flag in Draft 9) and the entire process address space (PROLOCK flag in Draft 9).

Two new functions have been introduced to lock and unlock the entire address space, instead of using the PROLOCK flag. The following example illustrates their use and comparison to Draft 9 code.

Draft 9 Code

#include <sys/memlk.h>

#define SIZE 1024

main()
{
void *addr;
:
addr = malloc(SIZE);
memlk(REGLOCK, addr, SIZE);
:
memunlk(REGLOCK, addr, SIZE);
:
}

Equivalent POSIX.1b Code

#include <sys/mman.h>

#define SIZE 1024

main()
{
void *addr;
:
addr = malloc(SIZE);
mlock(addr, SIZE);
:
munlock(addr, SIZE);
:
}

Locking Future Growth

When locking the entire address space, Draft 9 guaranteed that subsequent growth would also be locked. POSIX.1b provides two flags, MCL_CURRENT and MCL_FUTURE. These flags request locking current pages or future pages, respectively. Under LynxOS, MCL_CURRENT locks current as well as future pages.

The MCL_FUTURE flag by itself locks only future pages, not current ones. It would be unusual for an application writer to request this flag by itself.

Memory Locking Flags
Flag
LynxOS Semantics
MCL_CURRENT
Lock current as well as future pages
MCL_CURRENT | MCL_FUTURE
Same as MCL_CURRENT
MCL_FUTURE
Locks only future pages, not current

Draft 9 Code

#include <sys/memlk.h>

main()
{
:
memlk(PROLOCK, NULL, 0);
:
memunlk(PROLOCK, NULL, 0);
:
}

Equivalent POSIX.1b Code

#include <sys/mman.h>

main()
{
:
mlockall(MCL_CURRENT | MCL_FUTURE);
:
munlockall();
:
}

Changes from Draft 9 to POSIX.1b

Memory-Locking Interface  
Draft 9
POSIX.1b
<sys/memlk.h>
<sys/mman.h>
MEMLK_BOUNDSIZE
PAGESIZE
memlk(REGLOCK,addr, size)
mlock(addr,size)
memunlk(REGLOCK,addr, size)
munlock(addr,size)
memlk(TXTLOCK|DATALOCK| STKLOCK,..)
No Equivalent
memunlk (TXTLOCK|DATALOCK| STKLOCK,..)
No Equivalent
memlk(PROLOCK,NULL,0)
mlockall(MCL_CURRENT|MCL_FUTURE} or
mlockall(MCL_CURRENT)
memunlk(PROLOCK,NULL,0)
munlockall()
No Equivalent
mlockall(MCL_FUTURE)

Locking Flags

Draft 9 allows processes to lock their data, text, or stack segments. There is no support for such functionality in POSIX.1b. The flags used are:

Multiple Locks

Multiple memory locks can be set for a given region under Draft 9. There are no semantics for multiple locking in POSIX.1b. Memory locking functions can be invoked multiple times for a given address range, but still act as a single lock and are removed by a single unlock.

Locking/Unlocking the Entire Process

In Draft 9, the PROLOCK flag was used with memlk() and memunlk() to specify the entire process address space. In POSIX.1b, there are two new functions, mlockall() and munlockall(), for this purpose.

Current/Future Locking

In Draft 9, future memory growth was automatically locked with the PROLOCK flag. POSIX.1b provides a flag to request whether current or future pages be locked, with the MCL_CURRENT and MCL_FUTURE values, respectively. Under the LynxOS implementation, the MCL_CURRENT flag locks current as well as future pages. The following table shows the meaning of these flags.

Memory Locking Flags  
Flag
LynxOS Semantics
MCL_CURRENT
Lock current as well as future pages
MCL_CURRENT | MCL_FUTURE
Same as MCL_CURRENT
MCL_FUTURE
Lock only future pages, not current

Interoperability

The memory locking facilities for both Draft 9 and POSIX.1b are based on the same code in LynxOS.



LynuxWorks, Inc.
855 Branham Lane East
San Jose, CA 95138
http://www.lynuxworks.com
1.800.255.5969
TOC PREV NEXT INDEX