Reputation: 12890
I tried to read a lot of resources about CreateMappingFile
but unfortunately I don't have that good english, so I need to ask you about what I concluded if it is true or not:
CreateMappingFile
makes a RAM-like structure for a file in the virtual memory NOT in RAM , so this structure (or whatever is called) is located in the hard disk but in a form where it is ready to be transferred to the RAM. And when we need to transfer this structure to the RAM we use MapViewOfFile
.
Is that right?
Upvotes: 0
Views: 608
Reputation: 43688
This functions allow a userspace program to request to the OS to create a memory mapping.
A memory mapping is an OS structure that contains the information related to a virtual memory region of your process: starting virtual address, length, permissions, backing store (whether to swap out to a file or swap), ...
In *nix there is a single call (mmap()
) that allows you to create that mapping. Windows separates the "preparation" of the mapping (allocation of a handle and giving it a name) from the actual "instantiation" of that mapping.
So, in Windows, to share a mapping between processes (so called shared memory), you would CreteFileMapping()
from each process using the same "name", then before accessing the mapping you would instantiate it using MapViewOfFile()
(in *nix you would pass a MAP_SHARED
flag to mmap
and the vm portion of the kernel would share the memory; I'd guess the difference is mostly caused by different API style preferences in each platform).
Upvotes: 1
Reputation: 182761
CreateFileMapping just prepares a file for mapping and allocates a handle for the file mapping. MapViewOfFile actually gives that mapping an address in the virtual address space of the calling process.
It's up to the operating system to decide how to dedicate physical RAM to that file mapping. Whenever the process reads or writes to or from the mapping, there must be at least one page of physical RAM for the process to read from or write to. The operating system will back the mapping with physical RAM as needed (this is called 'faulting in') and remove physical RAM from the mapping as physical RAM is needed for other purposes.
The management of how much physical RAM is used by the mapping is entirely up to the operating system. Generally, the operating system will back the pages of the mapping with physical RAM as they are accessed (perhaps a few pages ahead) and will only remove the pages when it has some other use for that memory that it judges to be more important.
By the way, on a modern operating system, the exact same thing happens for an executable. When you run an executable, it is mapped into the memory space of the process and the pages of the executable also fault in as needed while the program runs.
Upvotes: 6