When learning C language, we often use the "&" symbol, and the following table, so does the retrieved address correspond to the real physical address?Let's write a step-by-step verification.
From the above figure, it is not difficult to see that from the body **, to the command line parameter environment variable, the address is from low to high, let's write a paragraph ** to verify it.
From here, it is not difficult to find that the addresses do appear in order from high to low.
So what about the command line arguments and environment variables, let's write a few more sets of **.
From the above results, it is not difficult to find that the addresses of the stack and the heap are relative, and the address of the command line argument is indeed at the top of the address space.
The parent process and child process are created at the same time, and then the result of the global variable is modified by the child process.
We found that the value of g val had not changed before five seconds, and that the g val address was the same in both parent and child processes, which was nothing to be confused about.
Five seconds later, we changed the value of g val, but this time, the value of g val is different, but the address is the same. So are we wrong, or is the computer wrong?Obviously, the computer certainly can't be wrong. Is this address a physical address that actually exists?Definitely not, this is the virtual address that the computer gives us a linear address.
Process address space: So the address space of the program we usually talk about is not right, it should be called the process address space, so how to understand it?What is an address space: Each process will have a process address space with a size of [0,4GB]. So why is this happening?
The parent process behaves like a shallow copy when creating a child process, so the child process inherits a large number of parent properties, including a page table, which is a relationship table that maps virtual and physical addresses. Each process will have its own page table.
When a subprocess wants to modify data, it triggers a write-time copy. The operating system will step in and prepare a special space for the sub-process to store the modified data, protecting the independence of the process. However, the virtual address corresponding to the subprocess page table has not been modified, but the physical address corresponding to the virtual address on the subprocess page table has been modified.
There is not only a mapping of virtual and physical addresses, but also permission bits. When the sub-process tries to modify the data (** is not modified by default), it will trigger copy-on-write, which will cause a page loss interruption, and the operating system will intervene to determine whether the write is legitimate, and when the behavior is legitimate, the operating system will open up physical space for the sub-process. The child process then writes and modifies its own data.
Regardless of the C C++ language, "&& prints the virtual address of the process, so the address we observed above has not changed. Each process will have a process address space, and the operating system will organize the management of these process address spaces as described. In simple terms, a process address space is a specific data structure object.
So what are the properties in the process address space?
According to the source ** published by Linux, there is such a structure as mm struct in the task struct, which is also in the process control block, we can see above that there are some characters such as "strart" and "end", it is not difficult to guess that this is a regional division of the process address space, and the memory resources in its own area can be used by the process to avoid the problem of crossing the boundary.
Our address space does not have the ability to save our ** and data, whether it is ** or data are stored in physical memory. The process provided me with a table, which shows the relationship between the virtual address and the physical address. This then converts the process address space (virtual linear) address to physical memory!
2023 Let's turn the page Why do we need process address spaces and page tables?
a.Maps the physical memory from an unordered state to an ordered state on the table.
b.With page tables, process management and memory management are separated, and the operating system decides when to free up memory and then write physical addresses to the page table. This decouples process management and memory management.
c.The address space plus the page table is an important means to protect memory security, and the process will not be allowed to access the memory casually (illegal access can be intercepted by the page table).
Note: There is a CR3 register on the CPU, which stores the physical address of the page table.
Note: When we request memory, we do so in the virtual space of the process, and the operating system does not open up physical space for us in physical memory (if the user has not yet attempted to write). It is only when the user actually tries to write spatially that the operating system opens up physical space and establishes a mapping on the page table. This behavior of separating the creation of virtual addresses from the creation of physical addresses greatly improves the efficiency of the operating system, because users do not necessarily use the space immediately, avoiding memory idling and waste of resources.