Wednesday, November 19, 2008

Process and Memory Segments

Process:

* An instance of program in execution.
* Can be created by fork() system call
* number of total processes created by "n" calls to fork are (2 pwr n)-1

Different memory segments in a process

1. Code/text segment
* Contain code instructions. This segment can be shared across multiple running instances of the same program

2. Data Segment
* Contains global and static variable

2.a Zero Initialized Data segment ( bss segment )
* Global and statically allocated data that are initialized to zero by default are kept in what is colloquially called the BSS area of the process.1 Each process running the same program has its own BSS area. When running, the BSS data are placed in the data segment. In the executable file, they are stored in the BSS section.

* The format of a Linux/Unix executable is such that only variables that are initialized to a nonzero value occupy space in the executable's disk file. Thus, a large array declared 'static char somebuf[2048];', which is automatically zero-filled, does not take up 2 KB worth of disk space. (Some compilers have options that let you place zero-initialized data into the data segment.)

2.b Initialized Data segment
* Statically allocated and global data that are initialized with nonzero values live in the data segment. Each process running the same program has its own data segment. The portion of the executable file containing the data segment is the data section.


3. Heap segment
* All dynamically allocated memory

4. Stack segment –
* All local / automatic variables are stored in stack
* Function parameters and function return address are also stored in the stack

5. Env Segment
* All environment variables

6. Shared memory segment

7. mmaped memory segment



No comments: