20 April 2009

Unix Programming - Understanding a simple "C" Program

Post 3
We saw that a program is a passive entity which is sequence of instructions. The sequence of instructions implements a specific algorithm or logic for doing specific operations. Another important aspect of writing a program is segregating the program logic into logical chucks as functions. These functions make program modular so that the program can be easily understood. The program also consists of data and the primary aim of any program is to process the data and produce useful information. There may be different types of data required throughout the process execution. Some data might be required for the entire life of the process and some data may needed only for certain period of time. Each of the process has an address space and the data is stored in process address space. The data is referred by the code (instructions) using variables. Based on the nature of the data whether it required throughout the program or only shorter span of time in a function the variables can be classified into two main types - local and global. Let us discuss about various types of variables with the help of simple example. This understanding is very much essential before discussing process address space.


int COUNT = 0;

void simplefunction(int c, int d)
printf("%d, %d", c, d);

int main()
int a = 1;
int b = 2;
simplefunction(a, b);
return 0;

In the above example, we have a variable with name "COUNT". There are two functions "main" and "simplefunction" each two variables. The variables "a", "b", "c" and "d" are called as local variable as their access is restricted the respective functions and the variable "COUNT" is called global variable as the access to global variable is available to all function. The local variables are also called as stack variables. The local variables have shorter life span and global variables exists throughout the life of the entire process. When a process is loaded in to the memory, these variables are stored in a specific region in the process address space, some of them are created when the process starts and some of them are created as and when they are executed and freed as soon as they go out of scope (that is whenever they are not needed).

In the next post, we will discuss about how a process is loaded into the memory, various regions in process address space and how a process is executed.

No comments: