WWW Geeksforgeeks Org Memory Mapping Amp
WWW Geeksforgeeks Org Memory Mapping Amp
WWW Geeksforgeeks Org Memory Mapping Amp
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.
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.
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.
return 0;
}
Output
Answer:
Memory mapping or mmap() is a function call thats helps to directly map a file to the currently executing
process’s own memory address space.
Answer:
1. File Mapping: It will map the File to the process virtual memory.
-Read-Write Mappings
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.
Q.3: What are the functions used for memory mapping and un-mapping?
Answer:
Engineering Maths
UPSC Study Preparation Competitive More Tutorials Free Online Write & Earn
Material Corner Exams Software Tools Write an Article
Polity Notes Company-Wise JEE Advanced Development Typing Test Improve an Article
Geography Notes Recruitment UGC NET Software Testing Image Editor Pick Topics to Write
Process
History Notes SSC CGL Product Code Formatters Share your
Resume Templates Management Experiences
Science and SBI PO Code Converters
Technology Notes Aptitude Project
SBI Clerk Currency Converter Internships
Preparation Management
Economy Notes
IBPS PO Random Number
Puzzles Linux
Ethics Notes Generator
IBPS Clerk
Company-Wise Excel
Previous Year Random Password
Preparation
Papers All Cheat Sheets Generator
Companies
Colleges
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved Open In App