LINUX TIPS AND TRICKS --- May 19, 2000

Published by ITworld.com, the IT problem-solving network 
http://www.itworld.com/newsletters 

*********************************************************************

Understanding Virtual Memory 
By Danny Kalev 

Modern operating systems have a sophisticated virtual memory system. In 
this column, I will discuss the main concepts of virtual memory and its 
advantages. 

In Linux, each process gets a separate address space -- a memory 
address is valid only within its process bounds. A process address 
space is independent of other processes address spaces. Theres no way 
that an address in one process can access data of another process (Ill 
ignore shared memory for now), although all processes share the same 
address ranges. 

For example, if address 0x4000aef6 in process A points to a user- 
defined variable, the same address in process B may point to some other 
data variable or it may not exist in B. To use an analogy, think of two 
countries that have cities with identical names. A combination of a 
country + a city name uniquely identifies the city. Likewise, a process 
id + a memory address is a unique combination. 

In virtual memory systems, a memory address isnt the same thing as the 
physical RAM address where the data is actually stored. Rather, a 
memory address is an alias. The virtual memory manager has a dynamic 
lookup table that maps a combination of a pid + an address to its 
physical location. Thus, address 0x4000aef6 can be mapped to the 
physical address 0x102788ff in the systems RAM, or to cluster 1740 on 
the systems hard disk. The physical location is hidden from the 
process and the programmer. Because of this, the system can swap memory 
into a disk file or read swapped memory back to the RAM in a totally 
transparent manner. 

For an end user, virtual memory offers two advantages: swapping and 
system stability. Memory swapping frees the system from the limits of 
physical memory. Linux can run several applications simultaneously, 
each using dozens of megabytes, on a machine that has only 8MB of RAM. 
Virtual memory ensures that memory-related bugs in one process (e.g., 
writing to an out-of-bounds array index) cant harm other running 
processes. For a programmer, virtual memory provides an additional 
layer of abstraction that hides hardware details. Anyone who has 
programmed for DOS surely appreciates this; in DOS, which doesnt have 
a virtual memory system, one must deal with memory segments and 
offsets, far and near pointers, and other horrors. 

This is all good and well. But doesnt the mapping of addresses to 
their physical locations and vice versa add runtime overhead? No, it 
doesnt. The mapping and lookup processes are performed by dedicated 
hardware components, not the CPU or the kernel. 

About the author 
---------------- 
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. 
  
*********************************************************************

CUSTOMER SERVICE 

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html?

********************************************************************* 

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com 
