12. Windows API Memory Management (2)

Source: Internet
Author: User

4. process memory space

1)Implementation of Virtual process address space

In Windows, each process has a private virtual address space. The system needs to map the virtual address of each process to the physical memory address. To enable each process in the system to have a private virtual address space, the system creates a directory and a group of page tables for each process. The page tables of each process are independent, while the page tables of the kernel space are shared by all processes.

On the X86 platform, the CR 3 register identifies the physical address of the page Directory. The first process on Windows has an independent page Directory. Therefore, when the system is running, the address of the page Directory saved in the audit 3 register changes, the value is the page Directory address of the currently running process. When the system needs to switch the process, it sets the page Directory address saved in the Audit audit register to the page Directory address of the process to be switched over and started to run. Every process on Windows has a kprocess data block, which includes the address of the Process page Directory.

2)Data sharing and protection

Some processes share data and the system is executable.Code(System DLL) and so on, are consistent among various processes, so there is no need to save multiple copies of the data in the physical memory, the Virtual Memory paging of different processes can be mapped to the same physical memory paging, which can save the use of physical memory.

At the same time, to ensure that the memory pages mapped to the same physical memory page are still private in the process, the system also provides some protection mechanisms. If a process loads a system DLL into the memory space of the process and writes the data in the DLL, The system monitors the operation and before writing the data, the Virtual Memory Page of the process to be written is mapped to another physical page, and the system DLL content in the original page is copied to this page. The new physical memory page is not shared among processes, the Process completes the write operation and writes the data to the new page. This write operation on system DLL data in the memory does not affect other processes, so data independence in each process is protected. This mechanism is called copy-on-write. If the system does need to share data between processes, you can also do not use copy-on-write for a specific page.

3) Notes

Developers only need to understand the following content.

(1) data in the virtual address space is managed by page.

(2) ApplicationsProgramYou do not need to consider the memory usage of other applications in the system, such as the memory occupied and memory occupied.

(3) the virtual address is not the address in the physical address space, not the address where data exists in the memory. The operating system maps the virtual address of the process to the address in the real physical memory.

(4) The process does not need to consider the actual physical memory size, but only needs to know that 2 GB memory can be used (generally), and the operating system is responsible for conversion.

(5) if the system does not have enough physical memory for use, the operating system will "Schedule" unused memory to the hard disk for storage. Page scheduling does not change the address of memory data in the virtual address space, so the process does not need to know how the memory paging is scheduled, you do not need to know whether the data stored in the memory is in the memory or on the hard disk. You only need to know its virtual address.

5. Virtual Memory layout and memory Division

The virtual memory space of a process is divided into two parts. The low 2 GB (or 3 GB) is used by the application, and the high 2 GB (OR 1 GB) is used by the system kernel program.

The memory space of the system kernel includes the driver, the executable program of the system kernel, and the data structure (such as the page Directory and page table) used for memory management ), data Structures Used for process management, thread scheduling, various interrupt handling programs, and system cache.

The low address space that an application can use includes the code, data (global variables, etc.) of the application, the code of the system and user DLL, the stack and heap of each thread, and so on.

Stack and stack are two important internal management forms.

Each thread of a process has its own stack, which is related to function call, execution, return, and local variable storage. Generally,The stack stores the function parameters, return addresses, and local variables. The call function adds the parameters to the stack, and the call command adds the return address of the function to the stack. The called function stores the stack pointer of the called function, allocate space for local variablesHeap is a kind of memory that can be dynamically allocated and released. It is managed by the heap manager. All the memory allocation functions used in the program are allocated through heap.

6,Memory protection attributes and Access Permissions

The system provides protection attributes and access permissions for each memory page. The minimum unit of the memory protection attributes and access permissions is paging. The memory in the same page has the same protection attributes and access permissions. The memory paging in the user address space has multiple protection attributes and access permissions.Such as page_execute..

Http://msdn.microsoft.com/en-us/library/aa366786%28VS.85%29.aspx

7. Related APIs

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.