Open In App

Memory Mapped Files in OS

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 associated with a file logically. This technique of memory mapping leads to a significant increase in performance.

Basic Mechanism of Memory Mapping

  • The Operating System uses virtual memory for memory mapping a file. It is performed by mapping a disk block to a page present in the physical memory. Initially, the file is accessed through demand paging. If a process references an address that does not exist in the physical memory, then page fault occurs and the Operating System takes charge of bringing the missing page into the physical memory.
  • A page-sized portion of the file is read from the file system into a physical page.
  • Manipulating the files through the use of memory rather than incurring the overhead of using the read() and write() system calls not only simplifies but also speeds up file access and usage.
  • Multiple processes may be allowed to map a single file simultaneously to allow sharing of data.
  • If any of the processes write data in the virtual memory, then the modified data will be visible to all the processes that map the same section of the file.
  • The memory mapping system calls support copy-on-write functionality which allows processes to share a file in read-only mode but the processes can have their own copies of data that they have modified.

The sharing of memory is depicted with the help of a diagram shown below.

Memory Mapped Files

Memory Mapped Files

Types of Memory Mapped Files

Basically, there are two types of memory mapped files:

  • Persisted: Persisted files are connected with a source file on a disk. After completing the final process, the data is saved to the source file on disc. Working with very big source files is appropriate with these type of memory-mapped files.
  • Non-persisted: Non-persisted files are not connected to any disk-based files. The data is lost when the last process with the file completes its required task. The shared memory that these files enable for inter-process communications or IPC.

Advantages of Memory Mapped Files

  • It increases the I/O performance especially when it is used on large files.
  • Accessing memory mapped file is faster than using direct system calls like read() and write().
  • Another advantage is lazy loading where small amount of RAM is used for a very large file.
  • Shared memory is often implemented by memory mapping files. Thus, it supports data sharing.

Disadvantages of Memory Mapped Files

  • In some cases, memory mapped file I/O may be substantially slower as compared to standard file I/O.
  • Only hardware architecture that has MMU (Memory Management Unit) supports memory mapped files.
  • In memory mapped files , expanding the size of a file is not easy.

Conclusion

Memory mapped files can be used as a process loader in several modern operating systems . Also, memory mapped file I/O is one of the most popular way to safely share memory. Thus, with the help of memory mapped file, applications can access files present on the disk much the same way they access dynamic memory with the help of pointers.

Frequently Asked Questions

Q.1: Why accessing memory mapped files are faster than direct system calls?

Answer:

Accessing memory mapped files are faster than direct system calls because memory mapped file allows a program to directly access any file present on the disk without the overhead of any system calls, which in turn can be much faster. On the other hand when a program accesses a file by making a system call, the operating system has to perform context switching which can be slower. Also, memory mapped files make efficient use of memory as it only maps the portion of the file which is required by the program instead of reading the entire file.

Q.2: What is “copy-on-write” functionality?

Answer:

“Copy-on-write” allows a process to share a file in read-only mode but the process can have their own copies of the data that they have modified.

Q.3: What is demand paging?

Answer:

Demand Paging is referred as the process of loading the pages only when they are needed during the execution of a program. Thus, those pages which are never accessed are not loaded into the physical memory.

Q.4: What is page fault?

Answer:

If a process references an address of a page that does not exist in the physical memory ,then page fault occurs and the operating system takes the charge of bringing the missing page into the main memory.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads