Memory Mappings
Memory mapping allows a file or device to be mapped directly into a process's virtual address space. Instead of using repeated read() and write() system calls, a program can access mapped memory using ordinary pointer operations.
Learning Objectives
- Understand memory mapping.
- Learn virtual memory concepts.
- Study
mmap(). - Study
munmap(). - Understand protection flags.
- Understand mapping flags.
1. What is Memory Mapping?
Memory mapping is a mechanism that maps a file or device into a
process's virtual address space.
Once mapped, the application accesses the mapped region as normal
memory without explicitly calling read() or
write() for every access.
2. Advantages of Memory Mapping
| Advantage | Description |
|---|---|
| Efficient File Access | Access file contents directly through memory. |
| Reduced System Calls | Fewer read() and write() operations. |
| Shared Memory | Multiple processes can share mapped regions. |
| Demand Paging | Pages are loaded as needed by the operating system. |
| Large File Support | Efficient handling of large files. |
3. Memory Mapping Workflow
4. Virtual Memory Overview
Every process executes within its own virtual address space. Memory mapping creates a relationship between a region of this virtual address space and a file or device managed by the kernel.
5. mmap()
The mmap() function creates a mapping between a file (or
device) and a region of virtual memory.
6. Function Parameters
| Parameter | Description |
|---|---|
addr |
Requested mapping address (or NULL). |
length |
Number of bytes to map. |
prot |
Memory protection flags. |
flags |
Mapping options. |
fd |
File descriptor. |
offset |
File offset where mapping begins. |
7. Return Value
| Return | Description |
|---|---|
| Valid Address | Memory mapping created successfully. |
MAP_FAILED |
Mapping failed (check errno). |
8. munmap()
The munmap() function removes a previously created
memory mapping.
9. Protection Flags
| Flag | Meaning |
|---|---|
PROT_READ |
Pages may be read. |
PROT_WRITE |
Pages may be written. |
PROT_EXEC |
Pages may contain executable instructions. |
PROT_NONE |
No access permitted. |
10. Interview Questions
- What is memory mapping?
- What problem does
mmap()solve? - Which header file declares
mmap()? - What does
munmap()do? - What does
PROT_READmean? - Why is
MAP_FAILEDused instead ofNULL? - What is virtual memory?
- Can mapped memory represent a file?
- When should
munmap()be called? - Why is memory mapping faster than repeated
read()operations in many workloads?
11. Mapping Flags
Mapping flags determine how the mapped memory behaves and whether changes made through the mapping are visible to other processes or written back to the underlying file.
12. MAP_SHARED
The MAP_SHARED flag creates a shared mapping.
Changes made to the mapped memory may become visible to other
processes that map the same file and can be synchronized back to
the underlying file.
13. MAP_PRIVATE
The MAP_PRIVATE flag creates a private mapping.
Modifications made through the mapping are not visible to other
processes and are not written back to the underlying file.
This behavior is commonly implemented using copy-on-write (COW).
14. MAP_SHARED vs MAP_PRIVATE
| Feature | MAP_SHARED | MAP_PRIVATE |
|---|---|---|
| Shared Between Processes | Yes | No |
| Writes Affect File | Yes (after synchronization) | No |
| Copy-On-Write | No | Yes |
| Typical Use | Inter-process sharing | Private processing |
15. Anonymous Mapping
Anonymous mappings are not backed by a file. Instead, the operating system provides zero-initialized memory pages that exist only for the lifetime of the mapping.
16. File-Backed Mapping
A file-backed mapping associates a region of virtual memory with an
existing file.
Applications access file contents directly through memory instead of
performing repeated read() and write()
operations.
17. Practical Example
18. msync()
The msync() function synchronizes a mapped region with
its underlying storage.
This is primarily useful for shared writable mappings.
19. msync() Flags
| Flag | Description |
|---|---|
MS_SYNC |
Wait until synchronization completes. |
MS_ASYNC |
Schedule synchronization asynchronously. |
MS_INVALIDATE |
Invalidate cached copies where supported. |
20. Interview Questions
- What is the purpose of
MAP_SHARED? - How does
MAP_PRIVATEdiffer fromMAP_SHARED? - What is copy-on-write?
- What is an anonymous mapping?
- When is
msync()required? - Which mapping type is commonly used for shared memory?
- Why is
MAP_FAILEDchecked after callingmmap()? - What does
MS_SYNCdo? - Can anonymous mappings be backed by files?
- Why are memory mappings often more efficient than repeated file I/O?
21. mprotect()
The mprotect() function changes the access protection of
an existing memory mapping.
It can modify whether mapped pages are readable, writable, executable,
or inaccessible.
22. Example Using mprotect()
The mapped pages become read-only after the protection change.
23. madvise() (Supplementary)
The madvise() function allows an application to provide
the kernel with information about the expected access pattern for a
mapped region.
The operating system may use this information to improve memory
management decisions.
Note: This interface is implementation-dependent and is presented as a Linux/POSIX extension rather than a core APUE topic.
24. mlock()
The mlock() function requests that the specified memory
pages remain resident in physical memory.
Locking memory helps prevent paging for latency-sensitive or
security-sensitive applications.
25. munlock()
The munlock() function removes a previous memory lock,
allowing the operating system to page the memory if necessary.
26. Shared Memory with mmap()
Two or more related processes can communicate by mapping the same file
or shared memory object using MAP_SHARED.
Changes made by one process become visible to other processes mapping
the same region.
27. Linux Memory Mapping Commands
| Command | Purpose |
|---|---|
pmap <pid> |
Display the memory map of a running process. |
cat /proc/<pid>/maps |
Display all mapped memory regions. |
cat /proc/<pid>/smaps |
Display detailed memory mapping statistics. |
vmstat |
Display virtual memory statistics. |
28. Best Practices
- Unmap memory using
munmap()when it is no longer required. - Request only the minimum required protection flags.
- Use
MAP_SHAREDonly when data sharing is required. - Always verify the return value of
mmap()againstMAP_FAILED. - Lock memory only when necessary because excessive locking reduces available physical memory.
29. Common Programming Mistakes
- Forgetting to call
munmap(). - Checking for
NULLinstead ofMAP_FAILED. - Requesting unnecessary write permissions.
- Using
MAP_PRIVATEwhen shared updates are required. - Locking excessive amounts of memory with
mlock().
30. Interview Questions
- What is the purpose of
mprotect()? - Why would an application call
mlock()? - What does
munlock()do? - How does
MAP_SHAREDsupport inter-process communication? - Which command displays the memory map of a process?
- What information is available in
/proc/<pid>/maps? - Why should
munmap()always be called? - Why is
MAP_FAILEDchecked instead ofNULL? - What is the purpose of
madvise()? - When is memory locking useful?
31. Memory Mapping API Comparison
| Function | Purpose |
|---|---|
mmap() |
Create a new memory mapping. |
munmap() |
Remove an existing memory mapping. |
mprotect() |
Modify memory protection permissions. |
msync() |
Synchronize mapped memory with storage. |
mlock() |
Lock mapped pages into physical memory. |
munlock() |
Unlock previously locked pages. |
32. Complete Memory Mapping Workflow
33. Linux Memory Mapping Commands
| Command | Purpose |
|---|---|
pmap <pid> |
Display memory mappings of a process. |
cat /proc/<pid>/maps |
Display mapped address regions. |
cat /proc/<pid>/smaps |
Display detailed mapping statistics. |
vmstat |
Display virtual memory statistics. |
free -h |
Display system memory usage. |
cat /proc/meminfo |
Display kernel memory information. |
34. Best Practices Review
- Always check whether
mmap()returnsMAP_FAILED. - Call
munmap()before terminating the program or when the mapping is no longer required. - Request only the minimum required protection flags.
- Use
MAP_SHAREDonly when processes must share updates. - Synchronize writable shared mappings when persistence is required.
- Avoid locking excessive memory using
mlock().
35. Common Programming Mistakes
- Testing for
NULLinstead ofMAP_FAILED. - Accessing memory after calling
munmap(). - Using an invalid file descriptor with
mmap(). - Ignoring page-alignment requirements for mapping offsets.
- Forgetting to synchronize writable shared mappings when needed.
- Requesting executable permission unnecessarily.
36. Real-World Applications
| Application | Usage |
|---|---|
| Database Systems | Memory-mapped database files. |
| Web Browsers | Efficient loading of large cache files. |
| Image Processing | Direct access to large image files. |
| Embedded Systems | Memory-mapped device registers. |
| Inter-Process Communication | Shared memory between related processes. |
37. Advanced Interview Questions
- How does
mmap()differ fromread()? - Why does
mmap()returnMAP_FAILEDinstead ofNULL? - What is the purpose of
MAP_PRIVATE? - Explain copy-on-write.
- When should
msync()be called? - How does
mprotect()improve security? - Why is
mlock()useful for cryptographic applications? - How can two processes communicate using
MAP_SHARED? - Which Linux command displays detailed memory mapping statistics?
- What happens if a program accesses memory after calling
munmap()?
38. Module Summary
This module introduced POSIX memory mapping interfaces for efficient
file access and inter-process communication.
We studied mmap(), munmap(),
mprotect(), msync(),
mlock(), and munlock(), along with
memory protection flags, shared and private mappings, anonymous
mappings, synchronization, Linux diagnostic utilities, recommended
programming practices, and common pitfalls.
Memory mapping is a fundamental technique used in operating systems,
high-performance applications, databases, and embedded software to
reduce I/O overhead and provide efficient access to large data sets.
Concept Mastery Assessment — Module 37
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.