Tell about the Memory Layout of a Process in Linux ?
Every running program (process) occupies some memory for its code and data. Linux follows a particular methodology for assigning memory addresses to various parts of a program.
- TEXT
This is the code segment and contains only the executable code of the program. In Linux, this is a read-only segment, implying that it’s contents can never be overwritten by the program. Thus, Linux does not support self-modifying code. - DATA
This segment contains all the data that is required throughout program execution. In C terms, this includes all extern and static variables. This is split into two physical parts:- Initialized Data Segment
This contains all the extern and static variables of the program that been explicitly initialized in the program - Uninitialized Data Segment (Also called BSS: Block Started by Symbol, a historial and outdated term!)
This contains all the extern and static variables of the program that not been explicitly initialized in the program, and hence have to be automatically initialized to 0.
- Initialized Data Segment
- STACK
This segment contains all local variables that are created when control enters a function. Note that these also include function parameters. - HEAP
This segment is internally kept track via a linked list, and is used by dynamic variables (memory allocated using malloc and calloc). Frequent memory allocations and deallocations result in fragmentation here.
The memory layout of a typical C program is shown below:
There is a strong reason for this arrangement. Note these points:
- The TEXT segment is loaded from the executable file
- The DATA segment is also blindly loaded from the executable file
These points mean that the first part of the memory is a copy of data from the executable - The BSS segment is NOT stored in the executable!
This saves space, as the whole block is anyway full of zeros. - The HEAP and the STACK grow towards each other.
This ensures that both have enough space, and you would not get problems like too much stack space and too little heap space or vice-versa - The Command Line Arguments and the Environment are dumped at the far end of the accessible memory block.
Finally, note that all addresses discussed here are logical addresses, and the actual physical address can be mapped anywhere in memory!
RSS Feed
Twitter
Orkut
