0% found this document useful (0 votes)
2 views5 pages

Memory Mapping

Memory mapping is the process by which an operating system translates virtual memory addresses to physical addresses, allowing programs to run by accessing data stored in virtual memory. The document explains various types of memory mapping, including file mapping, anonymous mapping, and device mapping, along with an algorithm for implementing memory mapping in C. It also provides a sample code demonstrating the memory mapping process, including file creation, memory allocation, and data manipulation.

Uploaded by

olgaphiri44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Memory Mapping

Memory mapping is the process by which an operating system translates virtual memory addresses to physical addresses, allowing programs to run by accessing data stored in virtual memory. The document explains various types of memory mapping, including file mapping, anonymous mapping, and device mapping, along with an algorithm for implementing memory mapping in C. It also provides a sample code demonstrating the memory mapping process, including file creation, memory allocation, and data manipulation.

Uploaded by

olgaphiri44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Memory Mapping

Last Updated : 24 Apr, 2025



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.
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.
Types of Mapping
1. File Mapping: It will map the File to the process virtual memory
 Read-Only Mappings: The File is mapped into memory only for the
purpose of reading. If we try to write it will end up in a segmentation fault.
 Read-Write Mappings: The file is mapped into memory for both reading
and writing. ie, it helps with file manipulation.
2. Anonymous mapping: It is a memory-mapped region where there is no
connection with any file/device. Or we can say it is used for dynamic allocation
of memory within a program.
 Private Anonymous Mapping: This is an area of memory that does not
have a file associated with it. It is commonly used for dynamic allocation.
Custom memory allocators are examples of this type of mapping.
 Shared Anonymous mapping: Inter-process communication is a type of
shared mapping in which multiple processes can have access to and
change the data. An example of this is implementing Inter-process
communication (IPC).
3. Device Mapping: In this type, Registers, hardware devices like graphic
cards, and network adapters can be mapped into the process’s address
space.
Implementation of Memory-Mapping Algorithm
M 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
emory 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.top
#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
More info

Similar Reads

Computer Memory
Computer memory is just like the human brain. It is used to store data/information and
instructions. It is a data storage unit or a data storage device where data is to be
processed and instructions required for processing are stored. It can store both the
input and output can be stored here.It's fa
8 min read

Cache Memory
Cache memory is a special type of high-speed memory located close to the CPU in a
computer. It stores frequently used data and instructions, So that the CPU can access
them quickly, improving the overall speed and efficiency of the computer. It is a faster
and smaller segment of memory whose access
7 min read


Cache Memory Design
Prerequisite - Cache Memory A detailed discussion of the cache style is given in this
article. The key elements are concisely summarized here. we are going to see that
similar style problems should be self-addressed in addressing storage and cache style.
They represent the subsequent categories: Cac
5 min read


Deleting Memory in C
In C programming, we might allocate memory dynamically for various tasks but what
happens when those pieces of memory are no longer needed? If not managed properly,
they can lead to memory leaks, wasting valuable resources, and slowing down our
program. Therefore, we need to manage memory in C by pr
5 min read


What is a Memory Dump ?
A memory dump copies all information from RAM to a storage disk as a memory dump
file (*.DMP format). A memory dump generally identifies a problem or error with the
operating system or any installed applications on the system. Typically, a memory
dump contains information on the final state of progr
4 min read


What is a Memory Heap?
What is Heap memory? Heaps are memory areas allocated to each program. Memory
allocated to heaps can be dynamically allocated, unlike memory allocated to stacks. As
a result, the heap segment can be requested and released whenever the program
needs it. This memory is also global, which means that it
5 min read


Windows Memory Management
The memory management in the operating system is to control or maintain the main
memory and transfer processes from the primary memory to disk during execution.
Memory management keeps track of all memory locations, whether the process uses
them or not. Determines how much memory should be allocated
5 min read


Memory Mapped Files in OS
We can use standard system calls like read(), seek(), open(), and so on to perform a
sequential read of a file present on the disk. Thus, to access a file from the disk we need
system calls and disk access. Memory mapping is a technique that allows a part of the
virtual address space to be associate
4 min read


Memory Usage in Android
In order to make our app get liked by users and make the app famous Memory plays a
very important role. Less the memory requirement for the application, the faster the
app will run on a specific device. We need to be careful if the app is being run on an
entry-level device and we know that most andr
7 min read


What is Memory Decoding?
The digital electronic circuit is a kind of circuit that only processes signal with two
states: either zero or one. Transistors in a circuit are used to conduct various Boolean
logic. In digital electronics, the memory decoding process took place, when there is a
need to access the memory in digital
9 min read

You might also like