Implementation of Memory-Mapping Algorithm

Memory mapping involves a series of steps. An algorithmic explanation of how memory mapping can be implemented in C is provided here:

  1. Start
  2. Initialize the size of the file and mapping.
  3. If the file not exists, create file; Open the file.
  4. If exists, truncate the file to the desired size.
  5. Create a memory-mapped region.
  6. Initialize memory-mapped region with some data.
  7. Read and print the contents of the memory mapped region.
  8. Un-map the memory and close the file.
  9. Stop

C




#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
 
int main()
{
    const char* file_path = "GFG-M-Mapping.txt";
    const size_t file_size
        = 4096; // Initializing file size to $kb.
 
    // We open the file for read/write operation
    int fd = open(
        file_path, O_RDWR | O_CREAT,
        0400 | 0200); // Use octal values for permissions
    if (fd == -1) {
        perror("open");
        return 1;
    }
 
    // if already exist,we trucate it to desired size
    if (ftruncate(fd, file_size) == -1) {
        perror("ftruncate");
        close(fd);
        return 1;
    }
 
    // Create a memory-mapped region for the file.
    void* file_memory
        = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
               MAP_SHARED, fd, 0);
    if (file_memory == MAP_FAILED) {
        perror("mmap");
        close(fd);
        return 1;
    }
 
    // Initialize the memory-mapped region with some data.
    const char* message
        = "Hello Geeks,Memory mapping is done !";
    strncpy(file_memory, message, strlen(message));
 
    // Read and print the contents of the memory-mapped
    // region.
    printf("Contents of the memory-mapped region: %s\n",
           (char*)file_memory);
 
    // Unmap the memory and close the file.
    munmap(file_memory, file_size);
    close(fd);
 
    return 0;
}


Output

Contents of the memory-mapped region: Hello Geeks,Memory mapping is done !

Memory Mapping

Modern computers have a virtual memory that is not physically present. We can achieve it by setting up a Hard disk, this way extended memory is called virtual memory. So any program that is inside the computer will have a virtual memory address to store data. This data cannot be used unless it is converted to a physical address. So, In short, Memory Mapping is the process done by the Operating System to translate Virtual memory addresses to physical addresses. So, the program can run anytime when the OS loads it as it is required.

Similar Reads

What is Memory Mapping?

Memory mapping or mmap() is a function call in an Operating system like Unix. It is a low-level language, and it helps to directly map a file to the currently executing process’s own memory address space. Which could optimize File I/O operations, inter-process communication, etc....

Implementation of Memory-Mapping Algorithm

Memory mapping involves a series of steps. An algorithmic explanation of how memory mapping can be implemented in C is provided here:...

FAQs on Memory Mapping in OS

...

Contact Us