Analysis of memory mapping files (2)

zhaozj2021-02-11  175

Memory map file

As mentioned earlier: The memory map is used as the system's memory used as the system, then it mainly

What is the use? Mainly the following two points:

1. Directly use the memory map file to access the data file on the disk without the need for files

I / 0 operation.

2. Use to share data between multiple processes. There are many ways to share data between processes, such as

Send a message WM_CopyData, anonymous pipe, etc., but their low level has no exception

Use mapping file. However, because WM_COPYDATA must be used

Synchronous function SendMessage, so it is not very good in real-time.

(As for synchronous and asynchronous differences, you can refer to another article of the author:

http://www.9cbs.net/develop/read_article.asp?id=14204)

As mentioned earlier, the location of the memory map file is in 3G-4G space, this part is Win32

All processes are viewed and shared, naturally can be used to transfer data, and each process is

Shared DLLs are also mapped in this spatial range.

The use of memory map files can be divided into the following three steps:

1.createfilemapping creates a file mapping kernel object

2. MapViewOffile will map the file data into the process address space

3.unmapViewoffile releases this mapping from the process address space

Below the two main functions of Mapping File, two simple examples are given:

A Access files directly with a memory map.

First create a mapping.txt under the C. Enter 1234567

Handle Hfile = CREATEFILE ("c: //mapping.txt",

Generic_read | generic_write,

File_share_read | file_share_write,

NULL,

Open_EXISTING,

FILE_ATTRIBUTE_NORMAL,

NULL);

Handle HfileMap = CreateFilemapping (HFile ..);

NULL,

Page_readwrite,

0,

100, // only open up 100

NULL);

LPVOID PVOID = MapViewOffile (HFilemap, File_Map_all_access, 0,0,0);

Char * buf = (char *) PVOID;

BUF [0] = "t"

CloseHandle (HFILE);

CloseHandle (HFILEMAP);

UnmapViewoffile (PVOID);

(Note: No abnormal situation is considered)

This way, when we open mapping.txt file, you will find the first byte "1"

It has been changed to 't'.

Maybe some readers will ask questions: Do you have trouble? Directly use Fopen or CreateFile

Isn't it OK? Yes, the small file is, but if this file has hundredspag? Mapping

FILE provides us with a convenient way of direct mapping access.

Here is a small place to note that there is a protection attribute when you create a mapping object.

FDWPROTECT can choose Page_Writecopy, as the name refers to write a copy,

After receiving this parameter, the system will submit the physical memory from the page file.

(As mentioned earlier, the mapping object does not use the page file). When reading a read operation occurs, the system

Still using the mapping file, when writing, the system allocates the page from the page file,

Copy from the mapping file to this page for access, which makes the original write operation being discarded.

The reader can try the example on the top of CreateFilemapping and MapViewOffile

The two corresponding bytes inside are changed to Page_WriteCopy and File_Map_copy,

Thus the original document will not be changed even if there is a write operation.

B Shared data between different processes

To make a sharing If you have to create a file on your hard disk every time you have trouble,

Windows provides such a mechanism: if the mapping object is created, if hfile

Fill in (HANDLE) 0xfffffffff, the system automatically creates file objects from the page file.

In addition, the book mentioned that the sharing method is made in the way in P2P or the architecture of C / S, I think it is different, there is no other difference, (a use createfilemapping

Open whether it is already existing, another open with openfilemapping)

Let's see an example;

# Define WM_Datacoming WM_USER 100

Process A:

Handle HfileMap = CreateFilemapping (Handle) 0xfffffffff,

NULL,

Page_readwrite,

0,

100,

"Shared");

LPVOID PVOID = MapViewOffile (HFilemap, File_Map_all_access, 0,0,0);

MEMSET (PVOID, 0, 100);

STRCPY ((char *) PVOID, "this is a mapping file test");

Handle HDES = FindWindow (NULL, "MApping"); // Name of the object window

SendMessage (HDES, WM_Datacoming, 0, 0);

CloseHandle (HFILEMAP);

UnmapViewoffile (PVOID);

Process B (has a window name mapping)

// wm_datacoming message capture function

Handle HfileMap = OpenFilemapping (Null, NULL, "Shared");

LPVOID PVOID = MapViewOffile (HFilemap, File_Map_all_access, 0,0,0);

Label1-> CAPTION = (char *) pvoid;

It can be seen that the data has been transferred correctly.

Some readers may have noticed that in this case, you need to take a name to the mapping object (example

Medium shared), yes, need to give it a name under this use, and in the first application

This place can be ignored. It may cause the place where the fight is this name,

If multiple processes have created multiple mapping objects, isn't it easier to conflict according to the name?

Yes, this is a problem, the author suggests the name of the form (mapping) or other

The only ID is made to make it not confused.

Note this function: MapViewOffile, notice that there is a word: viewà

This function is to resolve the created mapping object to the address space, which is produced.

One dependency. How to map the unified data files in Windows, such as

All files are mapped to a view, then map his first 10K separately into one depend.

So is the system really distinguish these more attention? The answer is to see what system,

If it is Win9x, the system does not have an additional map of a new address to it, but just

Return the original base address to the address of the new view, in other words

It is only one copy, and Winnt is a real new address space.

Returned.

Take a look at this small example:

Handle HfileMap = CreateFilemapping (Handle) 0xfffffffff,

NULL,

Page_readwrite,

0,

100,

"Shared");

/ / Submit the entire address to the space

LPVOID PVOID1 = MapViewOffile (HFileMap, File_Map_all_access, 0,0,0);

// Generate a new look from offset 40

LPVOID PVOID2 = MapViewoffile (HFileMap, File_Map_all_access, 0,40,0);

IF (pvoid1 40 == pvoid2)

MessageBox ("Run on Win95");

Else

MessageBox ("Run on NT");

It can be noted that the returned value is 0x8 ... This is in line with the location of the mappingfile in the address list. Ignored

The address of the mapping object opened in "Server" is consistent with the address returned by MapViewOffile (9X). This is also because this part of the address space is shared.

So since it is the same, can you use this value directly? For example, the above process is shared

Example: If the sending statement of the process A is changed to:

/ / Put the pointer value as a parameter

SendMessage (HDES, WM_Datacoming, (WPARAM) PVOID, 0);

The acceptance message part of the process B is changed to:

LPVOID PVOID = (LPVOID) msg.wparam;

Label1-> CAPTION = (char *) pvoid;

It can be seen that it can be displayed correctly, because the place refers to the pointer does have such a data,

So do you mean that we can use it? The answer is negative, first of all this value, etc.

Just in the Win9X environment, it is unequal in the NT environment, and the additional NT is accessed.

When the space is required, you must first use the MapViewOffile function. This is the first reason, more

What is important is that the memory map object belongs to the kernal object, the maximum difference of this object.

It is a data structure maintained by the system, and the user can only be indirect via the corresponding interface function.

Access. Each access is an increase in reference count, when the counter changes 0

Time, the system automatically releases this kernel object. In the above example, although the Server side and Client

The value is the same, but if the server ends unmapVIEWOFFILE release kernel objects,

This part of the data will be released by the system because its reference count is just 1, only we are in the client.

When you use MapViewOffile to increase the count of this object, it will not be released by the system.

heap

Win32's stack is located in the process private space, which belongs to the free distribution area. For example, everyone is often in C .

The NEW operator used is allocated in this place, with HeapCreate for the operation of the heap

And Heapalloc, etc., no longer discussed here.

postscript

Mapping file has always been a problem that is more difficult to discuss, and many netizens have been seen in 9CBS.

The discussion is relatively blurred, and finally can't. The author has always wanted to understand this problem.

Richter's masterpiece , because the part of memory is connected together

A few chapters, theory is more abstract and complicated, and it is very headache. Writing this article is also hope.

Help you better understand this part, have a further understanding of memory to better develop programs.

Interested further researchers can contact QQ: 33854303

Xrbeck is written in 2002/7/3

Reference:

1. Windows Advanced Programming Guide JEFFERY RICHTER

2. Windows programming Charles Petzold

3. Win32 multi-threaded program design Houjie

转载请注明原文地址:https://www.9cbs.com/read-5360.html

New Post(0)