Handle comprehension and handle Comprehension
Handle comprehension: (read the following article carefully! Knowledge about MMU)
Summary:
1. The handle is similar to the file stream in file operations. The handle can be used to operate the database;
2. When the program runs, each object will reside in the memory. If we get the first address of the memory, we may not be able to access its value correctly;
Why?
Because linux/windows are based on virtual memory and the address of each object remains unchanged, we cannot access the object Value correctly through the original address;
With the help of a handle (similar to a second-level pointer), the address of the handle remains unchanged. The first-level pointer stores the address of the object. This first-level pointer unit is unstable and uses a second-level pointer (handle) the Level 1 pointer unit to record this change; therefore, no matter how the Level 1 pointer unit changes, the object can be accessed through the handle;
This is a concept in the operating system. A handle is a unique integer used by Windows to identify the objects created or used by applications. Windows uses various handles to identify application instances and Windows, controls, bitmaps, and GDI objects. A Windows handle is a bit like a file handle in C Language (that is, a file stream ).
From the above definition, we can see that a handle is an identifier used to identify an object or project. It is like our name, and each person has a name, different people have different names, but there may be people with the same name as you. In terms of data type, it is only a 16-bit unsigned integer. Applications almost always obtain a handle by calling a WINDOWS function. Then other WINDOWS functions can use this handle to reference the corresponding objects.
To better understand the handle, I can tell you that the handle is a pointer to the pointer. We know that a pointer is a memory address. After the application starts, the objects that make up the program stay in the memory. If we simply understand it, it seems that we only need to know the first address of the memory, then we can use this address to access the object at any time. However, if you really think so, you are very wrong. We know that Windows is an operating system based on virtual memory. In this system environment, Windows Memory Manager often moves objects back and forth in memory to meet the memory needs of various applications. The object is moved, which means its address has changed. If the address is always changing, where should we find this object?
To solve this problem, the Windows operating system frees up some internal storage addresses for each application to specifically register the address changes in the memory of each application object, and this address (the location of the storage unit) it remains unchanged. After the Windows Memory Manager moves the object's location in the memory, it notifies the new address of the object to save the address. In this way, we only need to remember this handle address to indirectly know the location of the object in the memory. This address is assigned by the system during object loading. When the system is detached, it is released to the system.
Handle address (stable) → record the address of the object in the memory ---- → address of the object in the memory (unstable) → actual object
Nature: WINDOWS programs do not use physical addresses to identify a memory block, file, task, or dynamic loading module. On the contrary, windows api assigns a definite handle to these projects, return the handle to the application, and then use the handle to perform the operation.
However, it must be noted that each time the program is restarted, the system cannot ensure that the handle assigned to the program is the original one, and the vast majority of cases are indeed different. If we regard going to a cinema to watch a movie as the startup and running of an application, the handles assigned by the system to the application are always different, this is the same as the fact that tickets sold to us at a cinema are always different.