Home arrow BrainDump arrow Page 2 - The MMAP System Call in Linux

The page size - BrainDump

In this third part of a seven-part series on Linux I/O file system calls, you'll learn how to use the mmap() system call, which will give you some flexibility when handling files. This article is excerpted from chapter four of the book Linux System Programming: Talking Directly to the Kernel and C Library, written by Robert Love (O'Reilly, 2007; ISBN: 0596009585). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. The MMAP System Call in Linux
  2. The page size
  3. Return values and error codes
  4. Associated signals
By: O'Reilly Media
Rating: starstarstarstarstar / 5
December 11, 2008

print this article



The page is the smallest unit of memory that can have distinct permissions and behavior. Consequently, the page is the building block of memory mappings, which in turn are the building blocks of the process address space.

Themmap()system call operates on pages. Both theaddrandoffsetparameters must be aligned on a page-sized boundary. That is, they must be integer multiples of the page size.

Mappings are, therefore, integer multiples of pages. If thelenparameter provided by the caller is not aligned on a page boundary—perhaps because the underlying file’s size is not a multiple of the page size—the mapping is rounded up to the next full page. The bytes inside this added memory, between the last valid byte and the end of the mapping, are zero-filled. Any read from that region will return zeros. Any writes to that memory will not affect the backing file, even if it is mapped asMAP_SHARED. Only the originallenbytes are ever written back to the file.

sysconf(). The standard POSIX method of obtaining the page size is withsysconf(), which can retrieve a variety of system-specific information:

  #include <unistd.h>

  long sysconf (int name);

A call to sysconf() returns the value of the configuration item name, or-1ifnameis invalid. On error, the call setserrno toEINVAL. Because-1may be a valid value for some items (e.g., limits, where-1means no limit), it may be wise to clearerrnobefore invocation, and check its value after.

POSIX defines_SC_PAGESIZE(and a synonym,_SC_PAGE_SIZE) to be the size of a page, in bytes. Therefore, getting the page size is simple:

  long page_size = sysconf (_SC_PAGESIZE);

getpagesize(). Linux also provides thegetpagesize()function:

  #include <unistd.h>

  int getpagesize (void);

A call to getpagesize() will likewise return the size of a page, in bytes. Usage is even simpler than sysconf():


  int page_size = getpagesize ();

Not all Unix systems support this function; it’s been dropped from the 1003.1-2001 revision of the POSIX standard. It is included here for completeness.

PAGE_SIZE. The page size is also stored statically in the macroPAGE_SIZE, which is defined in<asm/page.h>. Thus, a third possible way to retrieve the page size is:

  int page_size = PAGE_SIZE;

Unlike the first two options, however, this approach retrieves the system page size at compile-time, and not runtime. Some architectures support multiple machine types with different page sizes, and some machine types even support multiple page sizes themselves! A single binary should be able to run on all machine types in a given architecture—that is, you should be able to build it once and run it everywhere. Hard-coding the page size would nullify that possibility. Consequently, you should determine the page size at runtime. Becauseaddrandoffsetare usually0, this requirement is not overly difficult to meet.

Moreover, future kernel versions will likely not export this macro to user space. We cover it in this chapter due to its frequent presence in Unix code, but you should not use it in your own programs. Thesysconf()approach is your best bet.

>>> More BrainDump Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates


Dev Shed Tutorial Topics: