Process

Program A set of instructions executed in a specific order
Process The execution of the program The Operating System creates the process that runs the program
Memory organization for the process


Stack
It is fast and static (fixed size defined by the Operating System)
If the size is too small:
there's a high risk of stack overflow
If the size is too large:
memory is wasted (because much of it often remains unused)
overall process management slows down (since all processes occupy a lot of memory)
Activation record
The stack memory is made up of Activation Records (ARs), which contain information needed to manage function calls.

Example:
#include <stdio.h>
void g();
void f();
void g()
{
//Something
}
void f()
{
g();
}
int main(void)
{
f();
return 0;
}
The
main
is the first Activation Record (AR) createdThe AR of function
f
is created when it's called frommain
The AR of function
g
is created when it's called fromf

Structure of AR

Ref Address it indicates the address where the return value of the function is stored.
Dynamic Link
Local Parameters Contains the parameters passed to the function during invocation.
Local Variables Contains the variables defined within the function.
Retval Obsolete part that contained the return value of the function.
DATA
It contains static variables (keyword static
)
They are initialized and created when the process is created (before the execution of the first instruction in
main
).They are initialized only once.
If declared without initialization, all the memory they occupy is initialized bit by bit to 0.
Example:
void g(int j);
static int i=0;
i++;
The variable i
is initialized at the beginning and then is not re-initialized in subsequent calls to the function.
HEAP
Memory area that can grow or shrink. All dynamic memory allocations are managed by the HEAP.
Array Declaration
It is necessary to specify the size of an array (in its standard declaration) because this allows the system to evaluate the future memory usage of the array.
If it fits within the free portion of the STACK, it is placed there.
Otherwise, it is dynamically allocated in the HEAP.
Multithreading Systems
The HEAP is the shared access point between different threads and allows communication between them.
AR Examples
Example 1
f (int a, int b)
{
g(&a,&b);
}
The address of variables
a
andb
is passed tog
.This WORKS because
g
is removed from the STACK beforef
.

Example 2
f ()
{
int *p
p=malloc();
g(p)
}
The function g()
will be able to:
Access the allocated area and modify its contents
In
f()
, the reference to that modified area will remainp

Example 3
int *f()
{
int *p;
p=g();
}
int *g()
{
int i=5;
return &i;
}

Last updated