LINUX TIPS AND TRICKS --- April 27, 2001

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

Memory mapping
By Danny Kalev

A process may map files to its address space, thereby creating 
a 1-to-1 equivalence between the files' data and its 
corresponding memory-mapped region. Memory mapping has 
several uses:

    * Dynamic loading -- A program can load executable code 
      sections dynamically by mapping executable files and 
      shared libraries into its address space.

    * Fast-file I/O -- Ordinary file I/O operations, such as 
      read() and write(), require the kernel's intermediation; 
      data from the file is copied to a kernel's buffer rather 
      than directly to the process. Memory mapping eliminates 
      this intermediary buffering, thus improving the overall 
      I/O performance.

    * Just-in-time compilation -- Memory allocated through 
      memory maps can be made executable and filled with 
      machine instructions that are then executed. Utilities 
      that generate code on the fly and just-in-time compilers 
      use this feature.

    * Streamlining file access -- Once you map a file to a 
      memory region, you access it via pointers, just as you 
      would access the program's variables and arrays, thus 
      eliminating the need for fread(), fseek(), fwrite() 
      function calls.

    * Memory persistence -- Memory mapping enables processes 
      to share memory sections that persist independently of a 
      certain process's lifetime.

Mapping and unmapping
You map memory by using the mmap() system call that is 
declared in <sys/mman.h> as follows:

    caddr_t mmap(caddress_t map_address, size_t length, 
              int protection, int flags, int fd, off_t offset);

The map_address parameter indicates the address to which the 
memory should be mapped. A NULL value allows the kernel to pick 
any address. The second parameter indicates the number of bytes 
to map from the file. The third parameter indicates the types 
of access allowed to the mapped region:

    PROT_READ                  the mapped region may be read
    PROT_WRITE                 the mapped region may be written
    PROT_EXEC                  the mapped region may be executed

The fourth parameter indicates various mapping attributes. The 
MAP_LOCKED attribute, for instance, guarantees that the mapped 
region is never swapped. The fd parameter is a descriptor of 
the file being mapped into memory. Finally, offset specifies 
the position in the file from which mapping should begin. 
Offset 0 indicates mapping from the file's beginning.

In the following example, the program maps the first 4 KB of a 
file passed whose name is passed as a command line argument:

    #include <errno.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <sys/types.h>

     int main(int argc, char *argv[])
     {
     int fd;
     void * pregion;
     if (fd= open(argv[1], O_RDONLY) <0)
     {
      perror("failed on open");
      return -1;
     }
     /*map first 4 kilobytes of fd*/
     pregion=mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
     if (pregion==(caddr_t)-1)
     {
      perror("mmap failed")
      return -1;
     }
     close(fd); 
     /*..access mapped memory*/
     }

The munmap() function unmaps a mapped region.

    int munmap(caddr_t addr, int length);

addr is the regions address and length specifies how much of 
the memory should be unmapped (you may unmap a portion of a 
mapped region).

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

What happens when you throw the switch? 
Understanding how a computer boots up

http://www.itworld.com/jump/lintps_nl/www.itworld.com/Comp/
3380/swol-0223-unix101/

The ins and outs of standard input/output 
Learn the basics of standard input, standard output, 
and standard error

http://www.javaworld.com/jjw/lintps_nl/jw-03-2001/
jw-0302-java101.html

Help me program asynchronous I/O

http://www.itworld.com/jump/lintps_nl/www.itworld.com/Comp/
3380/UIR010329cockcroftletters/#aio

Why doesn't my virtual memory monitoring program add up?

http://www.itworld.com/jump/lintps_nl/www.itworld.com/Comp/
3380/UIR010329cockcroftletters/#vm
________________________________________________________________

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/325!skip=260

Ask the Geek
Got a question about Linux setup, tuning, security, or 
maintenance? Hackers, newbies, and all the geeks in between 
knock heads and devise solutions in this discussion.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6c981/503!skip=464
________________________________________________________________

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
