Open In App

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.

Basic Mechanism of Memory Mapping

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



Memory Mapped Files

Types of Memory Mapped Files

Basically, there are two types of memory mapped files:

Advantages of Memory Mapped Files

Disadvantages of Memory Mapped Files

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.

Article Tags :