LINUX TIPS AND TRICKS --- May 18, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

Memory Locking
By Danny Kalev

A process's memory regions may be paged, or swapped, to a disk 
file when RAM has become scarce. The virtual memory management 
system is responsible for this swapping. In most cases, a 
process doesn't care about the exact, physical location of a 
certain memory page. However, the latency caused by the 
kernel's paging of memory back into RAM may be unacceptable to 
time-sensitive applications.

To avoid this latency, a process can lock specific memory 
regions in RAM and thus ensure that they aren't paged to a 
disk file. Only processes with root permissions are allowed 
to lock memory. The total amount of memory that a process can 
lock shall not exceed the RLIMIT_MEMLOCK value.

The memory locking interfaces are declared in <sys/mman.h>. 
This header file declares the following functions:

    int mlock(caddr_t addr, size_t length);

mlock() locks length bytes starting at address addr. Since 
Linux can only lock entire pages, the beginning and the end of 
the locked region are "rounded" to fit into page boundaries. 
Consequently, the locked block may be equal or larger than 
length. When mlock() returns, all the pages in the specified 
range have been locked in RAM.

    int mlockall(int flags);

To lock the entire address space of a process, you call the 
mlockall() function. The flags argument may be one of the 
following constants or both of them combined with the bitwise 
OR operator:

    * MCL_CURRENT - all the pages in the process's address
      space are locked into RAM. 
    * MCL_FUTURE - all future pages added to the process's
      address space would be locked into RAM.

To unlock memory, you call munlock():

    int munlock(caddr_t addr, size_t length);

The munlock() function takes the same arguments as mlock() 
and unlocks all the pages in the indicated region.

To lock all the locked regions at once, call munlockall(). This 
function has the following prototype:

    int munlockall(void);

Locking the same page multiple times has the same effect as 
locking it once. Regardless of the number of lock operations, 
a single call to munlock() will unlock the specified region.
________________________________________________________________

CORRECTION

In last week's newsletter, I made a typo in the final section. 
The call to the goodday() function through a pointer should 
read pgoodday(), not goodday(). Likewise, the second technical 
note should read as follows:

    pgoodday(); /* #2 call library's function through the
           pointer obtained previously*/

    2. When you call a function through a pointer, you use the
       pointer as if it were the function's name. However, the
       following form is also legal, albeit antiquated and
       perhaps less readable: (*pgoodday)();.

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). Contact him 
at linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

How to defrag

http://www.itworld.com/jump/lintps_nl/www.itworld.com/
App/4117/NWW01041100636262/

How does swap space work?

http://www.itworld.com/jump/lintps_nl/www.itworld.com/
Comp/2377/UIR960401perf/

How much RAM is enough? 
Determining the right amount of RAM for your Unix computer 
requires a little detective work

http://www.itworld.com/jump/lintps_nl/www.itworld.com/
Comp/1936/UIR960501perf/

Help! I've lost my memory! 
Before you scream "Memory leak!" take a look at how SunOS 
and Solaris handle your precious RAM.

http://www.itworld.com/jump/lintps_nl/www.itworld.com/
Comp/2377/UIR951001perf/
________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and 
put out the occasional call for help in this discussion for 
programmers of all levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/345!skip=281

Ray Hebert
Ray Hebert is IBM's senior technical marketing manager for 
IBM's Unix-based operating systems. Ask him your questions on 
IBM's future Unix directions. Runs May 15 to 17.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee712d8/0!skip=
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://reg.itworld.com/cgi-bin/subcontent12.cgi
- Enter your email address under "Current subscriber" to log in
- Uncheck the box next to the newsletter you want to 
  unsubscribe from
- Or check the box next to the newsletter you want to
  subscribe to
- Submit

If you have questions, please send email to customer 
service at: mailto:support@itworld.com
________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate
  Editor, Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account
  Executive at: dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz,
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com
  or Paul Duthie, Western Regional Sales Manager at:
  paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
