For example, you might use FILE_MAP_COPY to avoid the OS writing stuff for you, but you would have to handle writes yourself now, and that is decidedly not trivial. When you use memory mapped files for writes, you don’t know for sure in what order that is going to happen. Usually, when writing to files using file I/O routines, you have very predictable write pattern. However, what would happen during a crash? You might have partially written data in that scenario. So when you ask for it the next time, it can load it up again and the application won’t notice. When that happens, it will write that page to disk for you. The OS can decide, at any time, to take one of those pages away from you because it needs that memory for other things. For example, let us say that you made changes to two different memory locations, which reside on two different pages. It is hard to make sure that the writes you do are coherently stored in the file system. The disadvantages you need to be aware that things like the base addresses would change whenever you re-open the file, and that data structures that are good in memory might not result in good performance if they are stored on disk.Īnother problem relates to how the changes actually are saved to the file. It also make it drastically easier to do parallel I/O safely, since you can read/write from “memory” concurrently without having to deal with complex API. And the OS will do things like give you a page buffer, caching, preloading, etc. Because the I/O is actually being managed by the operation system, you gain a lot of experience in optimizing things. That can drastically reduce the amount of work you have to do, and it can also give you a pretty significant perf boost. Probably the most important is that you don’t have to do manual file I/O. What are the most important advantages and disadvantages?.And you don’t have to worry about all that pesky file I/O stuff. The reason you want to do this sort of thing is that it gives you the ability to work with files as if it was memory. You can read about this more in this article. Once that is done, you can access the part of the memory you have mapped and the OS will load the relevant parts of the file to memory. So you can map any file into your memory space. The next step is to make use of that for more than just the page file. So the OS already knows how to evict pages from memory and store them on the file system, because it needs to do that for the page file. The next stuff is where it gets interesting. So far, this is pretty basic OS 101 stuff. ![]() This is the point were the page file gets involved. It is obvious from the image that there is a problem here, what happens if two processes uses 4GB each? There isn’t enough physical memory to handle that. Well, what actually happens is that the pointer that looks like a pointer to physical memory is actually a virtual pointer, which the CPU and the OS work together to map to physical memory. What happen when both of those processes obtain a pointer to 0x0232194? Each of them get their own 4 GB address space (actually, only 2 GB is available to the process in 32 bits, but that is good enough). ![]() Now, you have two processes that are running. ![]() ![]() Here is your physical memory, using 32 bits for ease of use: In order to understand that, we have to understand virtual memory. Basically, you can tell the OS that some file is the backing store for a certain portion of the process memory. It require coordination between the memory manager and the I/O subsystem. And it is quite an interesting topic.Ī memory mapped file is a feature for all modern operating system. Tobi had a few questions about memory mapped files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |