Question

Well. I am pretty much confused with the concept of memory mapped file used in virtual address spaces.

In the following link I have googled.

http://en.wikipedia.org/wiki/Virtual_address_space

I found a section

           Then the application's executable file is mapped into the Virtual Address Space. 
           Addresses in the process VAS are mapped to bytes in the exe file. 
           The OS manages the mapping:

                   0                                            4GB
         VAS        |---vvvvvvv------------------------------------|
         mapping        |-----|
         file bytes     app.exe

        The v's are values from bytes in the mapped file.

Now what's this memory mapped file has to do with a particular process in a particular virtual address space (confused in the last statement of the above section) ?

Why there is an explicit need to include the code for creating a memory mapped file corresponding a particular process ?

Was it helpful?

Solution

The first thing you need to understand is the difference between a physical address and a virtual address. When your program writes data to a particular (virtual)memory address, the address of your write gets translated into the actual address on the RAM chip. If a physical address is not programmed in for a particular virtual address, in you will normally get an access violation or segmentation fault.

The way that this works is the Memory Management Unit (MMU) generates an CPU exception which causes execution to jump to a kernel fault handler. In the access violation case, your process is terminated.

The kernel is able to do some other clever stuff. If your process is idle, it might copy some of the physical memory used by your process into a swap file (and remove the entries from the mmu), when you access the said addresses, the CPU exception will not abort your program. Instead the kernel will first suspend your process. It then will copy the data from the swap file back into memory, reprogram the MMU. The kernel will then resume your process and the process will access the memory as if it had always been there. As far as the process was concerned nothing special happened. This process is known as Demand Paging.

When you mmap a file, the kernel is effectively performing demand paging but from your read specified file and not from the swap file. The process is exactly the same. The CPU access exception will cause data from the file to be read into RAM which you access as normal memory. When you close the file, the memory gets written back into the file deallocated. The big advantage is that your get to use the data in the file with pointers.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top