ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3142Memory Mapped Files
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3142

    Operating Systems

    Progress0 / 34 done
    Topics34
    1Operating Systems Basics2System Calls3Process Concept and Scheduling4Interprocess Communication5Multithreaded Programming6Multithreading Models7Threading Issues8Process Scheduling Algorithms9Thread Scheduling10Multiple-Processor Scheduling11Synchronization12Critical Section13Synchronization Hardware14Synchronization Problems15Deadlocks16Detecting and Recovering from Deadlocks17Memory Management18Swapping19Contiguous Memory Allocation20Segmentation and Paging21Virtual Memory Management22Demand Paging23Thrashing24Memory-Mapped Files25File Systems26File Concept27Directory and Disk Structure28Directory Implementation29Free Space Management30Disk Structure and Scheduling31Swap Space Management32System Protection33Virtual Machines34Operating System Security
    8 min read
    Topic 24 of 34
    Topic Study Note Illustration
    24

    Memory-Mapped Files

    A memory-mapped file is a feature in operating systems that allows a program to access the contents of a file directly in memory. Instead of reading or...

    Memory-Mapped Files in Operating Systems

    A memory-mapped file is a feature in operating systems that allows a program to access the contents of a file directly in memory. Instead of reading or writing data to a file using traditional system calls (such as read() and write()), a file is mapped into the address space of a process. The operating system manages this mapping, allowing the application to manipulate the file as if it were part of the program's memory.

    This technique is often used for efficient file I/O and shared memory communication between processes.


    How Memory-Mapped Files Work

    1. Mapping a File into Memory:

      • The operating system uses a system call (e.g., mmap() in Unix/Linux, CreateFileMapping() and MapViewOfFile() in Windows) to map a file or a portion of a file into the virtual memory of a process.
      • This mapping creates a direct correspondence between the file and a range of memory addresses in the process's address space.
    2. Accessing the File via Memory:

      • After mapping, the process can access the file's contents through memory accesses (e.g., reading or writing to the memory locations).
      • The operating system handles the I/O operations in the background, reading from or writing to the actual file on disk when necessary. This eliminates the need for explicit read and write operations for every file access.
    3. Automatic Synchronization:

      • When changes are made to the memory-mapped area, the operating system ensures that those changes are written back to the file (when the mapping is flushed or the file is unmapped).
      • Conversely, when another process maps the same file into memory, the OS ensures that changes made by one process are reflected in the memory of the other process.

    Advantages of Memory-Mapped Files

    1. Performance:

      • Faster File I/O: Memory-mapped files can provide faster file access compared to traditional I/O methods (like read() and write()) because the file data is accessed directly in memory, avoiding extra system calls.
      • Efficient Large File Access: Accessing large files becomes efficient since the system can load only the necessary parts of the file into memory (this is called demand paging), rather than loading the entire file at once.
    2. Simplified Code:

      • Instead of using explicit read/write calls, the programmer can manipulate the file data using regular pointers and memory operations, making the code easier to write and understand.
      • Memory-mapped files allow file manipulation through simple memory access (like arrays or structures), which can reduce the complexity of handling file I/O.
    3. Shared Memory:

      • Memory-mapped files can be used for inter-process communication (IPC). Multiple processes can map the same file into their address space, allowing them to share data efficiently.
      • This is often faster and easier than using other IPC mechanisms, such as pipes, message queues, or shared memory segments, because the processes access the shared data directly in memory.
    4. Automatic File Synchronization:

      • With memory-mapped files, changes made in memory can be automatically synchronized with the file. This can be controlled by setting specific flags (e.g., whether changes are written immediately or at certain intervals).

    Disadvantages of Memory-Mapped Files

    1. Limited Address Space:

      • The size of the memory-mapped file is constrained by the process's virtual address space. On 32-bit systems, this is typically limited to 4 GB of addressable space. This might be a problem if you are trying to map very large files.
    2. Complex Error Handling:

      • If a memory-mapped file is accessed and an error occurs (e.g., the file is deleted or there is an I/O error), handling these errors can be more complex compared to traditional file I/O operations.
    3. System Resource Management:

      • Memory-mapped files consume address space, and having too many large mappings might exhaust the available virtual memory for a process. This could lead to memory pressure and potential performance issues.
    4. Synchronization Overhead:

      • While the OS handles synchronization between the memory and the file, certain operations (such as writing to the file from multiple processes) might introduce synchronization issues that need to be carefully managed.
    5. File Size:

      • Memory-mapped files are typically best suited for files that are not excessively large. Mapping an extremely large file into memory can lead to excessive use of system resources and could slow down or even crash the system if there isn’t enough available virtual memory.

    Types of Memory-Mapped Files

    There are two common types of memory-mapped files:

    1. Private Mapping:

      • In a private mapping, the changes made to the memory region are not reflected in the original file. This means that the memory is only mapped for the process’s use, and when the mapping is removed, any changes made to it are discarded.
      • This type of mapping is often used for accessing files temporarily or for file-like objects that don’t need to be persisted.
    2. Shared Mapping:

      • A shared mapping allows multiple processes to map the same file into their address spaces. Changes made by one process can be visible to other processes that have mapped the same file.
      • This is useful for inter-process communication and shared memory applications, where processes need to share large amounts of data efficiently.

    Use Cases of Memory-Mapped Files

    1. Large File Handling:

      • Memory-mapped files are ideal for applications that need to read or write large files (e.g., databases, log files, media files) without having to load the entire file into memory. The operating system manages the paging in and out of memory as needed.
    2. Inter-Process Communication (IPC):

      • Memory-mapped files allow different processes to communicate efficiently by sharing data stored in the same mapped file. Changes made by one process are immediately available to all processes that map the file.
    3. Database Systems:

      • Many database management systems (DBMS) use memory-mapped files to optimize performance. They map large database files into memory, allowing for faster access and manipulation of data without loading the entire database into RAM.
    4. Virtual Memory Management:

      • Memory-mapped files can be used as a method to implement virtual memory. The OS can map sections of a file to virtual memory, providing processes with access to a larger memory space than is physically available.
    5. Shared Data Between Applications:

      • Memory-mapped files allow applications to share data directly, without needing to rely on slower inter-process communication mechanisms such as pipes or message queues.

    Example: Memory-Mapped Files in Linux (Using mmap)

    In Linux, the mmap() system call is used to map a file into memory. Below is a simple example of using mmap() to create a memory-mapped file:

    #include <fcntl.h>
    #include <sys/mman.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int fd = open("example.txt", O_RDWR, 0);
        if (fd == -1) {
            perror("Error opening file");
            exit(1);
        }
    
        // Get the size of the file
        off_t file_size = lseek(fd, 0, SEEK_END);
        lseek(fd, 0, SEEK_SET);
    
        // Memory-map the file
        char *mapped = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (mapped == MAP_FAILED) {
            perror("Error mapping file");
            exit(1);
        }
    
        // Modify the contents of the file through memory
        mapped[0] = 'H';  // Change the first character in the file
    
        // Unmap the file and close the file descriptor
        if (munmap(mapped, file_size) == -1) {
            perror("Error unmapping file");
        }
    
        close(fd);
        return 0;
    }
    

    In this example:

    • The mmap() call maps the file example.txt into memory.
    • The file can be read and written to directly through the mapped pointer.
    • Changes made to the memory-mapped region are reflected in the file because MAP_SHARED is used.

    Conclusion

    Memory-mapped files are a powerful and efficient technique for file I/O in modern operating systems. By allowing a file to be directly mapped into a process's address space, memory-mapped files enable faster access to file data and simplify code. They are particularly useful in situations where large files need to be handled or shared between multiple processes. However, developers must carefully manage memory resources and handle synchronization issues to avoid performance degradation or errors.

    ← Previous topic 23
    Thrashing
    Next topic 25 →
    File Systems

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Operating Systems