Sections of a Process

Different Sections of a Process is:

Stack Segment:

  • The stack sits at the higher address and grows towards the lower address.
  • It is unique for each process and stores store automatic variables (non-static local variables).
  • A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack.
  • When a function is called, a stack frame (or a procedure activation record) is created and PUSHed onto the top of the stack.
  • This stack frame contains information such as the address from which the function was called and where to jump back to when the function is finished (return address), parameters, local variables, and any other information needed by the invoked function.
  • Data is removed in a last-in-first-out manner from the stack.

Heap Segment:

  • This segment is responsible for holding all the variables are which are dynamically allocated via malloc(), calloc(), realloc() and new for C++ function.
  • This is shared by all the shared libraries and processes.
  • The stack and heap are traditionally located at opposite ends of the process’s virtual address space.
  • It is typical for the heap to grow upward. This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items.
  • It is also typical for the heap to start immediately after the BSS read of the data segment.
  • The end of the heap is marked by a pointer known as the break.
  • A program will terminate if they reference past the break.

Data Segment:

  • This section of memory is responsible for holding global variables, static variables, constant and extern variables.
  • This is further divided into two parts:
    • Initialized data segment: This contains all the global, static, extern, and constant variables which are initialized with non-zero values.
    • char a[] = “hello” is initialized read write area and char *a= “hello” is initialized read only area.
    • Uninitialized data segment: This is also called as bss(block starting symbol) segment and contains all the global, static, extern, and constant variables which are uninitialized.
    • It contains all global variables and static variables that are initialized to 0 or do not have explicit initialization in source code

Text Segment:

  • This is also referred to as a code segment and contains the executable and usually starts from a low address and is a static and read-only part of memory.
  • This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system

Note:

We also have one segment called the command line arguments section which holds the variable passed as a command-line argument. int main( int argc, int *argv[])

Diag-1: Process memory layout

Memory Layout in C - javatpoint

Related Posts:

Reference:
https://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html



Categories: Operating system (OS)

6 replies

Trackbacks

  1. Memory Layout of Thread - Tech Access Info
  2. Characteristics of a Thread - Tech Access
  3. Index of Operating System - Tech Access
  4. Termination of a Process - Tech Access
  5. Fork() System Call - Tech Access
  6. Context Switching - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: