21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

All modern operating systems have virtual memory managers. Among other things,<br />

virtual memory enables the operating system to make more memory available to running<br />

programs by swapping the contents of physical memory to disk. When a program<br />

must store sensitive data in memory, it risks having the information written to<br />

disk when the operating system runs low on physical memory.<br />

On Windows systems, the VirtualLock( ) API function allows an application to<br />

“lock” virtual memory into physical memory. The function guarantees that successfully<br />

locked memory will never be swapped to disk. However, preventing memory<br />

from swapping can have a significant negative performance impact on the system as<br />

a whole. Therefore, the amount of memory that can be locked is severely limited.<br />

On Unix systems, the POSIX 1003.1b standard for real-time extensions introduces<br />

an optional system call, mlock( ), which is intended to guarantee that locked memory<br />

is always resident in physical memory. However, contrary to popular belief, it<br />

does not guarantee that locked memory will never be swapped to disk. On the other<br />

hand, most current implementations are implemented in such a way that locked<br />

memory will not be swapped to disk. The Linux implementation in particular does<br />

make the guarantee, but this is nonstandard (and thus nonportable) behavior!<br />

Because the mlock( ) system call is an optional part of the POSIX standard, a feature<br />

test macro named _POSIX_MEMLOCK_RANGE should be defined in the unistd.h header file<br />

if the system call is available. Unfortunately, there is no sure way to know whether<br />

the system call will actually prevent the memory it locks from being swapped to disk.<br />

On all modern hardware architectures, memory is broken up and managed by the<br />

hardware in fixed-size chunks called pages. On Intel x86 systems, the page size is<br />

4,096 bytes. Most architectures use a similar page size, but never assume that the<br />

page size is a specific size. Because the hardware manages memory with page-sized<br />

granularity, operating system virtual memory managers must do the same. Therefore,<br />

memory can only be locked in a multiple of the hardware’s page size, whether<br />

you’re using VirtualLock( ) on Windows or mlock( ) on Unix.<br />

VirtualLock( ) does not require that the address at which to begin locking is pagealigned,<br />

and most implementations of mlock( ) don’t either. In both cases, the starting<br />

address is rounded down to the nearest page boundary. However, the POSIX<br />

standard does not require this behavior, so for maximum portability, you should<br />

always ensure that the address passed to mlock( ) is page-aligned.<br />

Both Windows and Unix memory locking limit the maximum number of pages that<br />

may be locked by a single process at any one time. In both cases, the limit can be<br />

adjusted, but if you need to lock more memory than the default maximum limits,<br />

you probably need to seriously reconsider what you are doing. Locking large<br />

amounts of memory can—and, most probably, will—have a negative impact on<br />

overall system performance, affecting all running programs.<br />

708 | Chapter 13: Other Topics<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!