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.

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

Similar Reads

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....

Types of Memory Mapped Files

Basically, there are two types of memory mapped files:...

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?...

Contact Us