Process Image



Now that we have seen how to get the basic information of process and its parent process, it is time to look into the details of process/program information.

What exactly is process image? Process image is an executable file required while executing the program. This image usually contains the following sections −

  • Code segment or text segment
  • Data segment
  • Stack segment
  • Heap segment

Following is the pictorial representation of the process image.

Process Images

Code segment is a portion of object file or program’s virtual address space that consists of executable instructions. This is usually read-only data segment and has a fixed size.

Data segment is of two types.

  • Initialized
  • Un-initialized

Initialized data segment is a portion of the object file or program’s virtual address space that consists of initialized static and global variables.

Un-initialized data segment is a portion of the object file or program’s virtual address space that consists of uninitialized static and global variables. Un-initialized data segment is also called BSS (Block Started by Symbol) segment.

Data segment is read-write, since the values of variables could be changed during run time. This segment also has a fixed size.

Stack segment is an area of memory allotted for automatic variables and function parameters. It also stores a return address while executing function calls. Stack uses LIFO (Last-In-First-Out) mechanism for storing local or automatic variables, function parameters and storing next address or return address. The return address refers to the address to return after completion of function execution. This segment size is variable as per local variables, function parameters, and function calls. This segment grows from a higher address to a lower address.

Heap segment is area of memory allotted for dynamic memory storage such as for malloc() and calloc() calls. This segment size is also variable as per user allocation. This segment grows from a lower address to a higher address.

Let us now check how the segments (data and bss segments) size vary with a few sample programs. Segment size is known by executing the command “size”.

Initial program

File: segment_size1.c

#include<stdio.h>

int main() {
   printf("Hello World\n");
   return 0;
}

In the following program, an uninitialized static variable is added. This means uninitialized segment (BSS) size would increase by 4 Bytes. Note − In Linux operating system, the size of int is 4 bytes. Size of the integer data type depends on the compiler and operating system support.

File: segment_size2.c

#include<stdio.h>

int main() {
   static int mystaticint1;
   printf("Hello World\n");
   return 0;
}

In the following program, an initialized static variable is added. This means initialized segment (DATA) size would increase by 4 Bytes.

File: segment_size3.c

#include<stdio.h>

int main() {
   static int mystaticint1;
   static int mystaticint2 = 100;
   printf("Hello World\n");
   return 0;
}

In the following program, an initialized global variable is added. This means initialized segment (DATA) size would increase by 4 Bytes.

File: segment_size4.c

#include<stdio.h>

int myglobalint1 = 500;
int main() {
   static int mystaticint1;
   static int mystaticint2 = 100;
   printf("Hello World\n");
   return 0;
}

In the following program, an uninitialized global variable is added. This means uninitialized segment (BSS) size would increase by 4 Bytes.

File: segment_size5.c

#include<stdio.h>

int myglobalint1 = 500;
int myglobalint2;
int main() {
   static int mystaticint1;
   static int mystaticint2 = 100;
   printf("Hello World\n");
   return 0;
}

Execution Steps

Compilation

babukrishnam $ gcc segment_size1.c -o segment_size1
babukrishnam $ gcc segment_size2.c -o segment_size2
babukrishnam $ gcc segment_size3.c -o segment_size3
babukrishnam $ gcc segment_size4.c -o segment_size4
babukrishnam $ gcc segment_size5.c -o segment_size5

Execution/Output

babukrishnam size segment_size1 segment_size2 segment_size3 segment_size4 segment_size5
   text  data  bss  dec  hex  filename
   878   252    8   1138 472  segment_size1 
   878   252   12   1142 476  segment_size2 
   878   256   12   1146 47a  segment_size3 
   878   260   12   1150 47e  segment_size4 
   878   260   16   1154 482  segment_size5
babukrishnam
Advertisements