Chapter 2 memory ing files
Or advantages and disadvantages. Then use. How to use it.
17.1 Principle and usage of memory files
17.2 use memory ing files
17.3 share data between processes using memory ing files
17.1 Principle and usage of memory files
Similar to the virtual memory, the memory ing file allows developers to reserve an address space area and allocate physical memory to the area. The difference is that the physical memory of the memory ing file comes from the existing files on the disk, rather than the Page Swap files from the system. Once the file is mapped to the address space, we can access it, just as the entire file has been loaded into the memory.
Memory ing files are mainly used in the following three scenarios:
1)
The system uses a memory ing file to load and run the. exe and DLL files. Saves space for Page Swap files and the actual startup of applications. (The system automatically does not pay attention to it)
2)
Developers can use memory ing files to access data files on disks. This prevents direct I/O operations on files and file content caching.
3)
Data is shared between different processes on the same machine.
17.2 use memory ing files
1) Create or open a file kernel object. Createfile
2) create a file ing kernel object to tell the System File Size and how we plan to access the file.
3) Tell the system to map the file ing object to the address space of the process.
After the memory ing file is used up, clear it in two steps.
4)
Tells the system to cancel the ing of the file ing kernel object from the process address space.
5)
Disable file ing objects and file objects.
//Open the file for reading and writing
HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (INVALID_HANDLE_VALUE == hFile){AfxMessageBox(_T("File could not be opened."));return FALSE;}//Create the file-mapping object.DWORD dwFileSize = GetFileSize(hFile, NULL);HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize + sizeof(TCHAR), NULL);if (NULL == hFileMap){AfxMessageBox(_T("File map could not be opened."));CloseHandle(hFile);return FALSE;}//Get the address where the first byte of the file is mapped into memory.PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);if (NULL == pvFile){AfxMessageBox(_T("Could not map view of file"));CloseHandle(hFileMap);CloseHandle(hFile);return FALSE;}//Clean up everything before exiting.UnmapViewOfFile(pvFile);CloseHandle(hFileMap);SetFilePointer(hFile, dwFileSize, NULL, FILE_BEGIN);SetEndOfFile(hFile);CloseHandle(hFile);return TRUE;
17.3 share data between processes using memory ing files
Data sharing between processes (or inter-process communication): Windows messages, memory ing files, clipboard, mail slots, pipelines, semaphores, sockets, etc.
Usage: Same as above. Note:
Ø
After process a completes the preceding steps, process B can directly access (or perform the same steps ).
Ø when data is shared using memory ing files, multiple processes must use the same memory ing file name.
Ø if you only want to share data, you do not need to create a file. You only need to pass invalid_handle_value to the hfile of createfilemapping. A more professional statement: the physical storage of the file ing object created in this way is not the files on the disk, but the system wants to transfer the physical storage from the page swap file.