Article Title: Describes Linux processes and stack spaces. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1) Overview
The stack is the memory area of the user space, and the process uses the stack as the temporary storage.
. The stack stores local variables in the function. In the life cycle of the function, the variables can be pushed into the stack. the compiler must ensure that the stack pointer is restored to the initial position before the function exits, that is, memory is automatically allocated and released.
. C/C ++ uses the local variables stored in the stack as the automatic storage and uses the auto keyword. This is the default storage method of local variables, so no one uses the auto keyword.
Static storage, that is, local variables defined by static storage, uses data segments instead of stacks for storage.
. The base address of the stack is located near the highest virtual address of the user space and extended from there.
At the beginning of a process, the maximum value of the stack cannot be changed. If the occupied space exceeds the size of the stack, it will cause stack overflow.
2) Memory Organization of processes
A process is divided into three areas: Text, data, and stack.
1) text area:
The text area is also called a code segment, which is determined by the program. It includes code (instructions) and read-only data. This area is usually marked as read-only, any write operation on it will cause a segment error.
2) Data region:
A Data region is also called a data segment. It includes initialized and uninitialized data. Static variables are stored in this region and their size can be changed by calling brk (2) by the system.
3) stack region:
A stack region is also called a stack segment. It is used to dynamically allocate space to local variables. Similarly, parameters and return values of function passing also need to be used in the stack.
Stack can also grow down (to the memory address) or up, depending on the specific implementation, usually downward growth, while SP (Stack pointer) it is also the last address pointing to the stack.
4) memory allocation area:
As described above, the stack is located near the highest virtual address, while the data segment is located after the stack segment, and finally the code segment.
3) stack coloring
When two threads or processes use the same stack virtual address, they compete for the same cache row, leading to competition and degradation.
Stack coloring technology makes each process have different base addresses. Multiple processes use different cache lines to avoid stack coloring by randomly allocating stack base addresses.
4) stack restrictions
The maximum value of the stack space is determined by the setrlimit system call. You can also set and view it through the bash built-in ulimit command.
For example:
View the maximum currently available stacks (in KB)
The following is a code snippet:
Ulimit-s
8192
Set the maximum stack size to 15 kb.
The following is a code snippet:
Ulimit-s 15
When ls is executed, a segment error is returned.
The following is a code snippet:
Ls-l/etc/
Total 1040
Segmentation fault
By tracking the ls command with strace, the following system calls will be found:
The following is a code snippet:
Getrlimit (RLIMIT_STACK, {rlim_cur = 15*1024, rlim_max = 15*1024}) = 0
The available stack space is insufficient to run the strace command.
5) resident memory and locked memory
Resident memory refers to the memory in RAM, excluding the memory of swap areas and unstored processes.
The locked memory is a subset of the resident memory. It refers to the virtual memory that is explicitly locked to RAM by the process. It cannot be used for switching and has been resident in RAM.