DEV Community

Cover image for C/C++  Process Map
Ketan Patil
Ketan Patil

Posted on

C/C++ Process Map

Most of us start learning of programming language with C/C++. C consider as grandfather of modern programming languages and some of them are running on c behind the scene.
Remember your excitement of writing first ‘hello world!’ program in c, definitely this had been a great experience. but what exactly happens when we compiled and run our c program?

COMPILATION:
On compiling c program, compiler compiles the source code file and outputs the binary code this binary code is contained in the file Hello.o or Hello.obj

Alt Text

After this linker links binary code of source code file and binary code contained in library. After successful linking, linker outputs program which has the name Hello.exe. program is then saved to Disc.

Alt Text

RUN:
Now when we run this program. OS loader loads program onto RAM for execution. Now this program has become process, process is a program under execution.

WHAT IS PROCESS MAP ?
Process has five memory segments DATA, BSS, CODE or TEXT, HEAP and STACK this is collectively called as PROCESS MAP.

Alt Text

Consider following code to understand use of individual memory segment

Alt Text

DATA segment : All static and global variables initialized to a NON- ZERO value go here. From above code data segment will contain

int g_b = 5 and static int z = 2
Enter fullscreen mode Exit fullscreen mode

BSS (Block Started By Symbol) : All static and global variables initialized to a ZERO go here. From above code, BSS segment will contain,

int g_a and static int d=0
Enter fullscreen mode Exit fullscreen mode

here we didn’t assign value of g_a as 0 then how g_a is come in BSS?, comment your answer below.

CODE / TEXT : Binary code of all the functions we used in our code go here.
It will contain binary code of functions add(), main() and printf().

HEAP : Heap is use for dynamic memory allocations. in above code we didn’t made any dynamic memory allocation hence in our case heap memory remains empty.

STACK : It contains stackframe of the functions currently under execution. stackframe is a structure that holds all needed information during functions execution. stackframe has four parts as follow,

Alt Text

Stackframe is created when function is called and then stackframe of respective function is pushed onto the stack.as we have three functions in above code hence three stackframes are created while program execution.

In this way C program is compiled and run and after its execution process map looks like below:

Alt Text

Top comments (2)

Collapse
 
ac000 profile image
Andrew Clayton

here we didn’t assign value of g_a as 0 then how g_a is come in BSS?

Global variables that aren't given an initial value are initialised to 0/NULL.

Collapse
 
ketan_patil profile image
Ketan Patil

True