A program in itself is just a text i.e. a passive entity.
When you run a program that is open an executable( .exe or a.out ), O.S creates an active entity known as process. A process is created for the program execution.
💡A process is a program in execution
Confused, right? Let's take an example to make this distinction super obvious.
Let's say you wrote a C program,
You compiled it into
gcc example.C command.
It is important to note here is both of the files,
a.out are programs only - a set of instructions.
Both files reside in the secondary memory i.e your hard disk.
Now when you type
a.out in the terminal to execute the compiled file, your operating system creates a data structure( process ) for the program execution in the main/primary memory. It looks like the following,
A good analogy is to think of processes like our body and program as our soul. The body cannot exist without a soul and the soul needs the body to take action!
The operating system's job is to give the set of instructions in
a.out to CPU for execution one by one.
While executing a program,
- Stack space is used for function calling - to store activation records ( information about called function )
- Heap space is used for dynamic memory allocation - memory allocated using
malloc, callocfunction is present in this section of the process.
It is important to note that, the stack grows downwards and the heap grows upwards since before program execution operating system cannot predict the exact size of both.
❗ Don't confuse this heap portion with heap data structure, they both are completely different things.