World Library  
Flag as Inappropriate
Email this Article


Article Id: WHEBN0000608709
Reproduction Date:

Title: Mmap  
Author: World Heritage Encyclopedia
Language: English
Subject: Inter-process communication, C dynamic memory allocation, Memory-disk synchronization, Out of memory, Sbrk
Collection: C Posix Library, Inter-Process Communication
Publisher: World Heritage Encyclopedia


In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It naturally implements demand paging, because file contents are not read from disk initially and do not use physical RAM at all. The actual reads from disk are performed in a "lazy" manner, after a specific location is accessed. After the memory is no longer needed it is important to munmap(2) the pointers to it. Protection information can be managed using mprotect(2) and special treatment can be enforced using madvise(2).

In Linux, Mac OS X and the BSDs, mmap can create several types of mappings.


  • History 1
  • File-backed and anonymous 2
  • Memory visibility 3
  • Example of usage under the C programming language 4
  • See also 5
  • References and further reading 6


mmap and associated systems calls were designed as part of the Berkeley Software Distribution (BSD) version of Unix. Their API was already described in the 4.2BSD System Manual, even though it was neither implemented in that release, nor in 4.3BSD.[1] Sun Microsystems had implemented this very API, though, in their SunOS operating system. The BSD developers at U.C. Berkeley requested Sun to donate its implementation, but these talks never led to any transfer of code; 4.3BSD-Reno was shipped instead with an implementation based on the virtual memory system of Mach.[2]

File-backed and anonymous

File-backed mapping maps an area of the process's virtual memory to files; i.e. reading those areas of memory causes the file to be read. It is the default mapping type.

Anonymous mapping maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero.[3] In this respect an anonymous mapping is similar to malloc, and is used in some malloc(3) implementations for certain allocations. However, anonymous mappings are not part of the POSIX standard, though implemented by almost all operating systems, by the MAP_ANONYMOUS flag.

Memory visibility

If the mapping is shared (the MAP_SHARED flag is set), it is kept visible across a fork(2) system call. This means that writes to that area in one process will affect other processes (that still have the same area mapped) and the underlying file (though it might not be msync(2)'ed and written to the underlying medium).

If the mapping is private (the MAP_PRIVATE flag is set), the changes will neither be seen by other processes nor written to the file.

A process reading from or writing to the underlying file will not always see the same data as a process that has mapped the file, since the segment of the file is copied into RAM and periodically flushed to disk. Synchronization can be forced with the msync system call.

mmap(2)ing files can significantly reduce memory overhead for applications accessing the same file; they can share the memory area the file encompasses, instead of loading the file for each application that wants access to it. This means that mmap(2) is sometimes used for Interprocess Communication (IPC). On modern operating systems mmap(2) is typically preferred to the System V IPC Shared Memory facility.

The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that SystemV shared memory is persistent: unless explicitly removed by a process, it is kept in memory and remains available until the system is shut down. mmap'd memory is not persistent between application executions (unless it is backed by a file).

Example of usage under the C programming language


/* Does not work on OS X, as you can't mmap over /dev/zero */
int main(void)
        const char str1[] = "string 1";
        const char str2[] = "string 2";
        int parpid = getpid(), childpid;
        int fd = -1;
        char *anon, *zero;

        if ((fd = open("/dev/zero", O_RDWR, 0)) == -1)
                err(1, "open");

        anon = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
        zero = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);

        if (anon == MAP_FAILED || zero == MAP_FAILED)
                errx(1, "either mmap");

        strcpy(anon, str1);
        strcpy(zero, str1);

        printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
        switch ((childpid = fork())) {
        case -1:
                err(1, "fork");
                /* NOTREACHED */
        case 0:
                childpid = getpid();
                printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);

                printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
                munmap(anon, 4096);
                munmap(zero, 4096);
                return EXIT_SUCCESS;

        strcpy(anon, str2);
        strcpy(zero, str2);

        printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
        munmap(anon, 4096);
        munmap(zero, 4096);
        return EXIT_SUCCESS;

sample output:

PID 22475:      anonymous string 1, zero-backed string 1
PID 22476:      anonymous string 1, zero-backed string 1
PID 22475:      anonymous string 2, zero-backed string 2
PID 22476:      anonymous string 2, zero-backed string 2

See also

  • Virtual memory for when there is more address space than physical memory
  • Paging for the implementation of virtual memory
  • Page cache for a disk caching mechanism utilized by mmap
  • Demand paging for a scheme implemented by mmap

References and further reading

  1. ^  
  2. ^  
  3. ^ "mmap(2) - Linux manual page". 
  • Description from POSIX standard
  • Differences:
    • FreeBSD
    • Linux
    • Mac OS X
    • Solaris
    • HP-UX
    • QNX
  • Windows
    • MapViewOfFile win32 function is somewhat equivalent to mmap.
  • More example source code:
    • SharedHashFile, An open source, shared memory hash table implemented using mmap().
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.