Thursday, October 29, 2009

How does a C Program Work/Execute


How does a C program works :

Note : Following article is based on my understanding of the chapter "Basic Memory Management" , from book Operating Systems , Third Edition by Gary Nutt .

This article describes the various Stages your C code or Program goes through before producing the desired output .

Stage 1 :
Writing of code or program and saving it with “.c” extension .

Stage 2 :
Compile Time : Now you need to compile your program .
In simple terms its job is to check whether your code follows all the rule of the C language (any language for that matter) i.e errors like , related to syntax and declariations etc are not present and convert the code into a language which is understood by your computer .

Let us now explore the compilation process in a bit more detail :

Aim of compile time is to produce a Relocatable Object Module , commonly known as an object file, with extension “.o” .
For a C program relocatable object module has has three logical blocks of addresses :



  1. A Text Segement (also known a Code Segment) , which contains a block of machine instructions .


  2. Data Segment , which is a block of Static Variables .


  3. Stack Segment , which represents the stack which is used while program is in execution .

Now the relocatable object module contains the relative addresses of the various code segments .
For example if you have used a user defined function “foo()” , which is also present in the same file , then it will contain its relative entry address.

However , for funtions like “printf()” or any other function, whose definition is not present in the in the code that you have written , compilier is not able to get their relative addrsses at the Compile time, so what is does it annotate each such reference to an external address , so that linkeditor can place correct addesse at the link time.

Stage 3 :
Link time : Now at link time code and data segments of each relocatable object module are combined to form the absolute module or the load module .
Link editor combines all the datasegment into single data segment and all code segment into a single code segment.
When datasegments are combined the relative addresses of individual static variables are changed. The linkage editor then relocates the addresses in the instructions so that they reference the uptodate addresses in the aggregated data segment .All the undefined addresses references are found by linkage editor as it combines the relocatable module: the resulting composition includes all the program text and data, so that every reference to the data or program entry point is resolved .

The absolute program is stored in a file(in the secondary memory) until a process is ready to use it.
image : courtesy operating system, third edition, Gary Nutt

Stage 4 :
Load time : Now when a process is allocated to use the absolute module , memory manager allocates a block of primary memory to the process. Then the loader copies the absolute program and data into the newly allocated memory.

Now the interseting part is , till now all the adderesses in the absolute program file were relative , starting from 0 , but now program is at some particular Physical address in the primary memory , so Loader Translates all internal logical primary memory addresse , with zero being replaced by the Physical memory address , and othe addresses changed according to it .

At this stage the program is called Executable program , the one expected by the hardware control unit , just before it is added to the primary memory at the proper location. The PC (program counter) is set to the primary memory address of the first ececutable insrtuction i.e. Main enry point for the program .

Now the program is ready to Run , the Static and Dynamic binding concepts which also play an important role , but beyond the scope of this article . May be in some other article :)

References : Operating Systems ,Third Edition by Gary Nutt.





4 comments:

  1. Thanks!
    You cleared all my queries regarding a standard program execution.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Where can I get the ebook Gary Nutt:Operating System.If you have it kindly forward me in an email hermandeep5@gmail.com

      Delete

About Me

My photo
Hi Friends, I am Samarth currently pursuing my Masters degree in Information Technology from International Institute of Information Technology ,Bangalore .