Lab VII: Memory Management
Date: 3 October 2019
In this lab, you will be extending the process simulator and
memory manager written in the last lab. Many processes will be
loaded into memory concurrently. However, we still assume
that the total memory required for all the processes is less
than the available physical memory. The process PIDs will
start at 1 and sequentially go up to the number of processes
(NP). NP is
a global variable.
- Physical Memory (Data Structure):
Create a global data
structure phys_mem to represent physical
memory. Its size must be equal to the number of frames. Each
entry should keep track of the page number loaded into that
frame along with the process PID, if a page is loaded; or a
value indicating free frame. This data structure should be
initialised to all free frames at the beginning.
- Process Ready Queue (Data Structure):
Create a global data
structure proc_queue to represent the ready
queue. Its size is NP. Initialise it
randomly with process PIDs.
Write the following programs:
- Process
Simulator: int sim_process
(int M, int sigma, FILE *outfile)
The process simulator remains the same as the one described
in the previous lab. Return value is the number of address
strings written into outfile.
Here is Random Number Generator code.
- Memory Mapper:
int mem_mapper (FILE *infile,
FILE *outfile, int pid)
Modify the memory manager function from the previous lab and
rename it as above. It now does the following:
- Generate a random number A
uniformly between 512 and 4096.
- Read A address references from
infile.
- For each address reference, find the physical address
using the same logic as that from previous lab. Extend
the logic to make sure that the page number and
the pid both match.
- Write the triplet (logical address,
physical address, is_pagefault)
into outfile.
- The function returns a negative
value if there is no free frame in physical memory
to load a page; 0 ("terminated"
status) if the entire file of address references is
processed; and, a positive value if
there are more addresses to be processed.
- Memory Manager: This is the main
program for the present! It works as follows:
- For every PID from 1
... NP, run process
simulator with outfile =
AR_pp.dat where pp
stands for the PID of the process. If the PID is 1, it
should be written into AR_01.dat
file.
- Open the AR_*.dat files for
reading.
- For every PID, open a
file AR_pp_out.dat for
writing the logical, physical addresses and page faults
triplets.
- Initialise proc_queue
and phys_mem.
- Get the first PID cur_pid
from proc_queue.
- Call the mem_mapper() function
with the correct input and output files
and cur_pid.
- If return value is negative, exit with an error message
Memory not available
If return value is 0 (process terminated normally), print
an informative message Process pid
of the process completed; removing from queue;
if the return value is positive, insert the process PID at
the end of proc_queue.
- Repeat the last three steps (5 - 7)
until proc_queue is empty.
- Print phys_mem data structure
to see the current page to frame mapping of each
process,
If you need help in design, here is a header
file. You don't have to use this but in case you are
short of ideas, you can use it.