Memory ing files in windows

Source: Internet
Author: User

On Windows, memory ing files make reading and writing of large files not consume too much memory, but also reduce frequent cpu commands. In addition, data is shared between processes on windows through memory ing files.
In this case, we need to load tens of megabytes of small files on the server. Each file contains about 1 million pieces of data. So let's take a look at this content.
To use a memory ing file, follow these steps:
1. Open a file object, that is, the file handle.
2. Create a file ing object.
3. Get the pointer of the file ing object in the process address space.
When you complete the memory ing file operation, perform the following steps to clear the memory:
1. Tell the system to cancel the image mapped to the kernel object from the address space of your process.
2. close the file ing object.
3. close the file handle.
Memory ing can efficiently read and write large files. As you can understand, the storage space of this file on the physical disk is used as virtual memory. You can read and write this memory area, it is equivalent to reading and writing files. Efficiency is much higher than I/O file systems. This method is used to transmit SendMessage and PostMessage between processes on win32.
In fact, if you just use it, you don't have to go into the principle, otherwise you need to go to "Windows core programming".
The Code is the final principle:
Assume that there is a test file test.txt on the e-drive. The content is as follows:
# Include "windows. h"
# Include "stdio. h"
 
Int main ()
{
// File object
Tchar str [] = "hello world! ";
HANDLE hFile = CreateFile ("E: \ test.txt ",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
// Ing object
HANDLE hFileMapping = CreateFileMapping (hFile,
NULL,
PAGE_READWRITE,
0,
0,
NULL );
// Two file ing pointers are assigned here. The address starts from the file header.
TCHAR * pbFile1 = (TCHAR *) MapViewOfFile (hFileMapping, FILE_MAP_WRITE, 0, 0, 0 );
TCHAR * pbFile2 = (TCHAR *) MapViewOfFile (hFileMapping, FILE_MAP_WRITE, 0, 0, 0 );
/*
Output the content of the string. We can see that the entire file is output as a string.
Of course, if the file contains '\ 0' characters, you cannot see the output of all the file content.
*/
// The two outputs are the same
Printf ("% s \ n", pbFile1 );
Printf ("% s \ n", pbFile2 );
/*
The following operations write custom content to the address pointed to by pbFile1,
Then compare whether the value of pbFile2 pointing to the address has changed accordingly.
*/
Memcpy (pbFile1, STR, strlen (STR ));
Printf ("% s \ n", pbFile1 );
Printf ("% s \ n", pbFile2 );
// Clear pointer and memory
UnmapViewOfFile (pbFile1 );
UnmapViewOfFile (pbFile2 );
CloseHandle (hFileMapping );
CloseHandle (hFile );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.