Forbidden City Turret is a lot of photography enthusiasts often go to the place, sunset Afterglow under the Palace of the Forbidden City is calm and serene.
?
First, look at the basic concepts of processes, the layout and content of processes in memory.
Also, you need to know how the runtime allocates additional memory for dynamic data structures, such as linked lists and two-fork trees.
One process 1 processes and procedures
Process: is an instance of an executable program.
Program: A file that contains a series of information that describes how to create a process at run time. Contains the following information:
- Binary format identification: such as the most common elf format.
- Machine language instruction: encode the program algorithm.
- Program Entry Address: Identifies the starting command location when the program starts executing.
- Data: The program file contains the variable initial value and the literal constant value that the program uses, such as a string.
- Symbol table and Relocation table: Describes the location and name of functions and variables in a program.
- Shared Library and dynamic Link information: Some of the fields included in the program file list the shared libraries that are required for the program to run, and the path names of the dynamic linker that loads the shared library.
- Additional information.
Process redefinition: A process is an abstract entity defined by the kernel and assigns to the entity the system resources used to execute the program.
From the kernel point of view, the process consists of a user memory space and a series of kernel data structures, where the user memory space contains the program code and the variables used by the code, and the kernel data structure is used to maintain process state information.
2 Typical process memory layouts
The memory allocated by each process is made up of many parts, often referred to as "segments (segment)". As shown in the following:
- Text snippet: Contains the program machine language instruction that the process is running. Text segments have read-only properties, so multiple processes can run the same program at the same time, sharing text segments.
- Initialize a data segment: A global variable and a static variable that contains an explicit initialization. When the program loads into memory, the values of these variables are read from the executable file.
- Uninitialized data segment (BSS segment, block started by symbol): Contains global variables and static variables that are not explicitly initialized. Before the program starts, the system initializes all memory in this section to 0. So it's called 0. Initializes the data segment.
- Stack: A segment that dynamically grows and shrinks, consisting of a stack frame. The system assigns a stack frame to each currently called function. The local variables, arguments, and return values of the function are stored in the stack frame.
- Heap: An area that dynamically allocates memory for a variable at run time. Heap Top becomes program interrupt
Storing the initialized global and static variables separately from the uninitialized global and static variables is primarily due to the fact that there is no need to allocate storage space for uninitialized variables when the program is stored on disk. Instead, the executable simply records the location of the uninitialized data segment and the desired size until the runtime allocates the space by the program loader.
It is important to note that the discussion of the memory layout is in virtual memory and is not a layout in physical memory.
Some details of virtual memory are discussed later.
?
Two memory allocation 1 allocating memory on the heap
Heap: A variable length of contiguous virtual memory that starts at the end of the uninitialized data segment of the process and increases or decreases as memory is allocated and released. Call the top boundary of the current memory of the heap "program breaks"
Program break is a very important concept because the actual action of allocating and releasing memory is to change the program break position of the process.
The starting position of program break (the size of the heap is 0) is after the end of the uninitialized data segment.
Details: After allocating new memory, the program's break position is elevated, and programs can access any memory address in the newly allocated area, at which point the physical memory page has not been allocated. Memory automatically allocates new physical memory pages when the process first attempts to access these virtual memory addresses.
function malloc and free
malloc function Declaration
Function: Allocates a parameter size of memory on the heap.
Return value: Successfully returned a pointer to the new allocated memory start address, failed to return null
free function declaration?
#includevoid free (void *ptr);
Function: Releases the block of memory pointed to by the PTR parameter, which should be the address that was previously returned by malloc or one of the other memory allocation functions.
It is important to note that, in general, free does not reduce the location of program break, but instead increases the memory to a list of idle memory for subsequent malloc functions to be used. Because:
- The freed memory block is usually located in the middle of the heap, not the top of the heap, so it is not possible to reduce program break.
- It minimizes the number of kernel calls to tune program break system calls.
- Typically, the program holds allocated memory or is repeatedly released and redistributed, rather than releasing all memory for a period of time.
The glibc implementation of the free function calls SBRK () to reduce the address of the program break only when the heap top idle memory is "large enough", and the "enough" depends on the control parameters (128KB typical) of the malloc function packet behavior. This reduces the number of calls that must be initiated on SBRK ().
Implementation of malloc and free
Implementation of malloc ()
- A list of free memory blocks freed by free () before scanning to find a piece of memory that is greater than or equal to the required size
- If the size of this memory block is exactly the same as the requirement, return it directly to the caller.
- If it is a large chunk of memory, it will be split, leaving the smaller chunk of free memory in the idle list while returning a chunk of memory to the caller.
- If a large enough block of free memory is not found in the list of free memory, malloc calls SBRK () to allocate more memory, and malloc allocates more memory than the required number of bytes, placing the excess part in the list of free memory.
Implementation of free ()
First, learn two points: the structure of memory blocks and blocks in the free list returned by malloc
In order to know the size of each memory block, when malloc allocates memory blocks, it allocates a few extra bytes to hold an integer value that records the size of the memory. The integer is at the beginning of the memory block, and the memory address that is actually returned to the caller is exactly after the length of the record byte. As shown in the following:
To manage the list of idle memory, free () uses the space of the memory block itself to hold the linked list pointer and adds itself to the list. As shown in the following:
Therefore, after allocating and releasing memory frequently, the list of linked tables in the heap may become the same, and the free memory in the free list is mixed with the allocated memory.
?
Three things to note in programming
By knowing more about memory-related knowledge, when programming in peacetime, it should be clearer why we need to follow the rules below.
- After allocating a chunk of memory, do not change anything outside of this memory range.
- It is an error to release the same piece of allocated memory more than once. When the same piece of memory is released two times, the common consequence is unpredictable behavior.
- The free () function must never be used without a pointer returned by a function in the malloc function package.
- If you need to allocate memory over and over again, be sure to release any memory that is already in use, or it will cause a memory leak.
?
Although in our usual work, may not be involved in such a low-level principle, but through the understanding of these basic principles, can let us more clear, we write code exactly what is written:)
?
Resources:
Linux/unix System Programming Manual (previous book), Chapter 6th, Chapter 7th?
UNIX Advanced Environment Programming (15) process and memory allocation < Forbidden City Turret >