web.archive.org

Mmap

  • ️Mon Mar 05 2012

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 initially file contents are not entirely read from disk and do not use physical RAM at all. The actual reads from disk are performed in "lazy" manner, after a specific location is accessed. After the memory is not to be used, 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.

Anonymous mapping maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero.[1] 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 systems, by the MAP_ANONYMOUS flag.

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.

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.[citation needed]

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

#include <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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 = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
        zero = 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);
                sleep(3);
                printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
                munmap(zero, 4096);
                close(fd);
                return (EXIT_SUCCESS);
        }
        sleep(2);
        strcpy(anon, str2);
        strcpy(zero, str2);
        printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
        munmap(zero, 4096);
        close(fd);
        return (EXIT_SUCCESS);
}

See also

  • Virtual memory for a general context of possessing more addresses than physical memory
    • Swapping or Paging for implementation of virtual memory used in contemporary systems

References and further reading

  • Windows
Methods
Selected protocols
and standards
Libraries
and frameworks

This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)