Memory Management Ics 2305 Systems Programming
Memory Management Ics 2305 Systems Programming
Using C programming
Creating a new process using fork()
system call
CREATING multiple processes using fork
() and pipe()
Demonstrate the concept of Orphan
Process.
Using execv and execvp system calls
MEMORY MANAGEMENT
Karanja Mwangi
Lesson objective
Note that only the global memory has a fixed size that is
known when the program starts execution. Both the stack
and the heap store data the size of which cannot be know
until the program is executing
Variable Types and Memory Allocation-6
General format:
type *p;
p = (type *) malloc (byte_size);
Example
int **p;
p = (int **) malloc(3 * sizeof(int *));
p[0]
p int ** int *
p[1] int *
p[2] int *
Dynamic Allocation of 2-d
Arrays
Recall that address of [i][j]-th element is found
by first finding the address of first element of i-
th row, then adding j to it
Now think of a 2-d array of dimension [M][N] as
M 1-d arrays, each with N elements, such that
the starting address of the M arrays are
contiguous (so the starting address of k-th row
can be found by adding 1 to the starting address
of (k-1)-th row)
This is done by allocating an array p of M
pointers, the pointer p[k] to store the starting
address of the k-th row
Contd.
Heap-allocated memory
This is used for persistent data, that must survive beyond the
lifetime of a function call
• global variables
• dynamically allocated memory – C statements can create new
heap data
• Heap memory is allocated in a more complex way than stack
memory
Like stack-allocated memory, the OS determines where to get
more memory – the programmer doesn‟t have to search for
free memory space!
Dynamic Memory Vs. Static Allocation