Technology Encyclopedia Home >How to use mmap function for memory mapping in Linux?

How to use mmap function for memory mapping in Linux?

The mmap function in Linux is used for creating a new mapping in the virtual address space of the calling process. It maps files or devices into memory, allowing data to be accessed as if it were in memory, which can be more efficient than traditional I/O operations for large files or sequential access patterns.

Function Prototype

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

Parameters

  • addr: Suggests the starting address for the mapping. If NULL, the kernel chooses the address.
  • length: The length of the memory region to map.
  • prot: Specifies the desired memory protection (e.g., PROT_READ, PROT_WRITE).
  • flags: Determines the type of mapping (e.g., MAP_SHARED, MAP_PRIVATE).
  • fd: File descriptor of the file to map.
  • offset: Offset within the file where the mapping starts.

Return Value

On success, mmap returns a pointer to the mapped region. On failure, it returns MAP_FAILED (usually (void *) -1).

Example Usage

Here's a simple example of using mmap to map a file into memory:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd = open("example.txt", O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    struct stat sb;
    if (fstat(fd, &sb) == -1) {
        perror("fstat");
        exit(EXIT_FAILURE);
    }

    void *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (addr == MAP_FAILED) {
        perror("mmap");
        exit(EXIT_FAILURE);
    }

    // Now you can access the file content via the memory address 'addr'
    printf("File content: %s\n", (char *)addr);

    if (munmap(addr, sb.st_size) == -1) {
        perror("munmap");
        exit(EXIT_FAILURE);
    }

    close(fd);
    return 0;
}

Explanation

  1. Open the file: The file example.txt is opened with read-only permissions.
  2. Get file size: The fstat function retrieves the file size.
  3. Map the file: The mmap function maps the file into memory.
  4. Access the data: The file content can be accessed directly from the memory address returned by mmap.
  5. Unmap the file: The munmap function unmaps the file from memory.
  6. Close the file: The file descriptor is closed.

Cloud Computing Context

In the context of cloud computing, memory mapping can be particularly useful for handling large datasets efficiently. For instance, when dealing with big data analytics or high-performance computing tasks on cloud platforms like Tencent Cloud, using mmap can help optimize I/O operations and reduce latency.

Tencent Cloud offers services like Tencent Cloud Block Storage and Tencent Cloud File Storage that can be integrated with applications using mmap for efficient data handling and processing. These services provide scalable and reliable storage solutions that can be leveraged in conjunction with memory mapping techniques to enhance application performance.