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