What is the proper use of mlock(2)/munlock(2)?

Daniel Rudy dr2867 at pacbell.net
Wed Mar 29 06:27:39 UTC 2006


Hello FreeBSD Hackers,

I've been reading the man page on mlock(2) and a number of questions
have arisen about it's use.  I have looked at malloc and mmap, and I
have not been able to figure this one out.  There doesn't seem to be any
compiler or library options dealing with this either.

1) How do you make sure that an allocated address range has been aligned
on a multiple of the page size given FreeBSD's virtual address map?

2) If 1 cannot be done, then is there a way to force a memory allocation
on a page alignment?

3) Is there another set of functions that do this besides mmap/malloc?

munlock(2) is not the problem here, it's mlock(2) because according to
the man page, it states the following:

The mlock() system call locks into memory the physical pages associated
with the virtual address range starting at addr for len bytes.  The
munlock() system call unlocks pages previously locked by one or more
mlock() calls.  For both, the addr argument should be aligned to a multi-
ple of the page size.  If the len argument is not a multiple of the page
size, it will be rounded up to be so.  The entire range must be allo-
cated.


The thing that is tripping me up is this sentence here:  For both, the
addr argument should be aligned to a multiple of the page size.

And further on in the errors section, we have this:

[EINVAL] The address given is not page aligned or the length is
negative.


Any ideas on how to properly use this?


-- 
Daniel Rudy


More information about the freebsd-hackers mailing list