Understanding Process for a Program
For Complete YouTube Video: Click Here
In this class, we will have Understanding Process for a Program.
We have already discussed functions in our previous classes.
Process for a Program
The concepts discussed in this class are very important to understand recursive functions.
What is a process?
A program in execution is called a process.
The above definition states that for every program to execute, a program will get created.
The process contains different data structures for the storage of variables, execution of functions, dynamic memory allocations for the program execution.
The image below is the program to understand the process.
For every program in execution, a process will get created.
The image below is the process for the above program.
In a process, we have separate memory space for static and global variables.
All the static and global variables will get stored in that specific space.
Similarly, for ‘dynamic memory allocation,’ heap data structure is used, as shown in the above image.
In the same way, the stack at the top of the process is used for function calls.
The stack will grow downward.
Whenever a function call is made, a new stack record will get created called an activation record.
We will try to understand how the process will help in the above program execution.
Firstly in the program’s execution, when the main function is called, a new activation record will get created, as shown in the image below.
The activation record above contains all the variables belonging to that function and also includes an instruction pointer.
The instruction pointer is the value of the line number from where the program execution has been jumped to another function call.
In the above main function, the instruction pointer value is two because that is the line number from where the sum function call is made.
At line number two of the above program sum function is called.
Whenever a function call is made, a new activation record is created for sum function call, as shown in the image below.
After completing the function execution, the activation will get deleted from the stack.
Now the program execution will go back to the main function.
After returning to the function, using the instruction pointer, the next line will get executed.
With that line of execution, the activation record for the main will get deleted.