Understanding the operating system through Linux (iv): Memory Management (I)

Source: Internet
Author: User

The most intuitive understanding of memory can be imagined as a grid. Each grid is marked by an address and stores one byte of data. For 32-bit machines, there can be 2 ^ 32 addresses, that is, theoretically 4 GB of data (the actual machine is not necessarily 4 GB of physical memory ). Indeed, for programmers, this understanding is enough to meet the requirements of programming, and the actual physical model of memory is also like this. However, for the system, this simple model is not enough, because under normal circumstances, the system will run multiple programs, if these programs can directly operate on any memory address, one program may directly modify the data stored in the memory by another program. What will happen in this case, it's hard to say, but it's a tragedy. Therefore, the operating system must implement some mechanisms to ensure that each process can use the limited memory in a harmonious and friendly manner, and at the same time ensure the memory usage efficiency, these are the main contents of this article.

1. Basic Concepts

To solve the preceding problems, the operating system abstracts the physical memory and obtains an important concept:Address SpaceThe address set that can be used to access the memory, that is, 0X00000000 to 0 xFFFFFFFF. The size is 4 GB. Each process has its own address space. In the opinion of each process, this 4G is equivalent to physical memory. It can access them using any address without worrying about affecting other processes. Because the address here is not a physical address but a virtual address, it needs to be converted to a physical address by the system before accessing the memory, the system ensures that the same virtual address in different processes will be mapped to different physical addresses, and will not operate on the same block of memory (unless that block of memory is shared ). Because a program usually does not use 4G memory, the 4G physical memory can store data of multiple programs at the same time without overlap, even if the 4G memory is full, you can also save some useless data to the disk to free up space for other data. For more information about how to perform this operation, see here, our program accesses the memory through a virtual address, and the system ensures that each process accesses its own data through the address space.

With the concept of address space, when discussing how a program uses memory, we can put the concept of physical memory aside, next, let's take a look at how processes in Linux use the address space:

In Linux, although each process has 4G address space, only 3G belongs to it, which is also calledUser spaceThe remaining 1 GB is shared by all processes, that isKernel spaceThis 1 GB kernel space stores important kernel data, such as the page table used for paging query and the process descriptor mentioned earlier, these contents will be stored in the memory during system operation, andUser ModeThe process under is invisible, only when the process switchesKernel ModeThe kernel space is shared by all processes, therefore, using the kernel space for inter-process communication is a matter of course. All IPC objects, such as message queues, shared memory, and semaphores, are stored in the kernel space.

The user space is divided into three segments based on logical functions: Text, Data, and Stack, as shown in.

The content of the Text segment is read-only and the size of the entire segment does not change. It stores the execution instructions of the program, which are derived from executable files, we know that after the program is compiled, we will get an executable file, which stores the machine commands executed by the program, copy these commands to the Text segment, and then the CPU reads and executes the commands from here.

The data segment, as its name implies, stores data in the program, including various types of variables, arrays, and strings. It consists of two parts, one being an initialized data zone, stores data with initial values in the program. One is a non-initialized data zone (usually called BSS) that stores data without initial values in the program, data in the BSS area is automatically initialized to 0 when the program is loaded. Note that the data here does not include local variables in the function, because it is in the stack segment. For example, anyone familiar with C/C ++ knows that if the global variables in our program are not set with an initial value, the system will automatically initialize them to 0, while the local variables do not set the initial value, then their values are uncertain. The reason is that when the global volume does not have an initial value, it will be saved in the BSS area. The value is automatically 0 here. If there is an initial value, in the initialization zone, local variables are not initialized in the Stack segment. Different from the Text Segment, data in the data segment can be modified, and the size of the data segment may also be changed during the program running. For example, when you call malloc, the address of the data segment is extended, and the dynamically allocated memory is calledHeap.

The stack segment is located at the top of the user space and can grow down. It is used to store the stack for function calling. When a program is executed, the main function stack is first created, along with the imported environment variables and execution parameters, and pushed into the system stack (the stack segment ), when calling another function A in the main function, the system first presses the parameter and return address of function A in the main stack, create A new stack for A and press it into the system stack. When A returns, the stack of A is popped up, in this way, the currently executed functions are always at the top of the system stack (the top here is at the bottom, because the stack segment is increasing down). This is a rough process of function calling.

2. Address Space Applications

As mentioned above, the concept of address space allows a process to read and write data using addresses in the address space, regardless of where the actual data is stored, next, let's look at what the system can do with this.

(1) shared text segment: we already know that the text segment stores machine commands for running programs. When multiple processes run the same program, their text segments must be the same. At this time, in order to save physical memory, the system will not put the text segments of each process into the physical memory, instead, only one copy is saved, and the text of the address space of each process is mapped to this area. This will not affect the running of each process, at the same time, it saves valuable physical memory. In fact, the system also ensures that only one copy of the same command exists in the memory. An actual example is the use of the dynamic Connection Library.

(2) memory ing file: because the process uses the address space address to read and write data without having to worry about where the actual data is, it means that the data may not even be in the memory, the memory ing file is used to reserve an area of the process address space and map this area to a file on the disk, the process can access the file like the memory (that is, the pointer and offset can be used like an array), instead of the file I/O operation, of course, the operating system must provide a mechanism to convert the Logical Address to the actual file storage location, but this is not what we are concerned about. Another advantage of using a memory ing file is that multiple processes can map to the same file at the same time, because the file is in the memory of the process, that is to say, it can be regarded as a shared memory, which means that the modifications made by each process to this region are visible to other processes in real time, of course, the efficiency here is lower than when actually storing data in the memory, but it brings another advantage that the disk space is infinite compared with the memory, so it can achieve data sharing of large data volumes.

Well, here we have a more detailed understanding of the memory, and the address space is a concept worth noting, in the next article, let's take a look at the mechanism through which our program can easily access the memory.

 

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.