OpSys Spring 2006 - HW4

HW4 - Virtual Memory Simulator

Due Date: 4/4 11:59PM

Submit to WebCT drop box labeled HW4

Late Policy

- VM Simulator

The objectives of this assignment are:

  1. Understanding of the basic operations involved with a virtual memory system.
  2. Understand how various page replacement algorithms work.
  3. Analysis of page replacement algorithms.

You are to write a simulator for a virtual memory system. The simulator will support a number of different page-replacement algorithms (listed below) and will be able to report some statistics after each run. A run will be defined by a sequence of lines of text which your simulator will read from STDIN - each line in the input file corresponds to either a memory access by a process, the beginning of a (simulated) process or the end of a (simulated) process.

Simulation Input

The input contains a series of lines, each line conforms to one of the patterns shown below. You can easily generate and store sample input sequences in a file, then run your simulator with input redirected from this file. Note that as part of the testing of your program, we will generate some input files that are not available to you during development (although we will provide some sample input files). The format of the input files is simple enough that you should consider writing a program that can generate a file according to some model of a pattern of virtual memory accesses by some number of processes.

OperationSyntaxDescription
Start process start pid Corresponds to the creation of a new process. The pid is a number that is used to refer to this specific processs. It is possible that pids will be re-used after termination, (you may come across two process 7's - they are different processes!). At any time there can be up to 10 active processes (pids 0 -9).
Terminate process terminate pid Corresponds to the termination of an existing process. The pid indicates which process is to be completely removed from memory (this simulates a process exit). All pages used by the process should now be available.
Memory Read read pid address Corresponds to a memory read operation by the process corresponding to the given pid. The address specified (in decimal) is a byte address, so you will need to convert this to a page address (as far as your simulation is concerned, there is no difference between accesses that are in same virtual page).

Simulator Command Line

Your simulator must support the command line options described below (using the syntax described below). You probably should use getopt to process the command line, there is a sample program that uses getopt available: getoptexample

./hw4 [-v] [-p pagesize] [-n numpages] [-a algorithm] [-t timestep]

Command line example: The following command line would run the simulator with the page size set to 32 bytes, a total of 8 pages of physical memory, simulating a clock interrupt every 100 memory operations, using the clock page-replacement algorithm and reading simulation commands from the file "sim1":

./hw4  -n 8 -t 100 -a clock < sim1

Below is a small sample of the kind of input that will be given to your simulator:

start 0
start 1
start 3
read 1 29
read 0 20
read 3 38
read 1 26
read 0 38
read 0 49
read 0 45
read 3 1
read 3 35
start 2
read 2 16
read 1 14
read 1 32
read 3 16
read 3 13
read 0 0
read 0 11
read 2 8
read 0 2
read 1 4
read 1 55
read 1 41
read 0 30
read 2 2
terminate 2
read 1 3
read 3 5
read 1 18
read 1 7
read 3 41
read 3 38
start 2
read 2 15
read 2 10
read 2 39
read 1 33
read 1 41
read 3 55
read 1 42
read 1 9
read 2 50
read 2 7
read 1 31
read 2 7
read 2 21

Simulator Output

Your simulator should always generate the following output:

Here is the kind of output we expect:

./hw4 -a clock -n 8 < samp1
Page Size: 32
# page frames: 8
Replacement Algorithm: clock
Memory operations: 42
Faults: 9

Here is the kind of stuff we want to see when using verbose:

./sim.pl -v -a clock -n 8 < samp1
Starting process 0
Starting process 1
Starting process 3
READ 1 29 (virtual page 0): MISS, place in physical page 0
READ 0 20 (virtual page 0): MISS, place in physical page 1
READ 3 38 (virtual page 1): MISS, place in physical page 2
READ 1 26 (virtual page 0 is in physical page 0): HIT
READ 0 38 (virtual page 1): MISS, place in physical page 3
READ 0 49 (virtual page 1 is in physical page 3): HIT
READ 0 45 (virtual page 1 is in physical page 3): HIT
READ 3 1 (virtual page 0): MISS, place in physical page 4
READ 3 35 (virtual page 1 is in physical page 2): HIT
Starting process 2
READ 2 16 (virtual page 0): MISS, place in physical page 5
READ 1 14 (virtual page 0 is in physical page 0): HIT
READ 1 32 (virtual page 1): MISS, place in physical page 6
READ 3 16 (virtual page 0 is in physical page 4): HIT
READ 3 13 (virtual page 0 is in physical page 4): HIT
...

- Page Replacement Algorithms

When grading, we will compare the output of your program to ours, so you need to make sure that you understand how each algorithm works. Below is a brief description of each algorithm and a reference to where in the textbook you can find the details.

Optimal Algorithm

Ref: Page 215 in the text

The optimal algorithm can see into the future, and removes the page that will be idle the longest. In order to implement this algorithm you will need to process the input twice (once to record the memory reference string so you know what happens and when, a second time to actually simulate virtual memory). Your program will only be given the input once, so it needs to record the input! When using the recorded memory reference string to look ahead in time, don't ignore the possibility that a process named 0 may be started and then terminate, and later another process 0 may be started (this second one has nothing to do with the first process 0, we are just reusing the process number).

Least Recently Used

Ref: Page 218 in the text

LRU keeps track of the relative order of pages according to when they have last been accessed. When a page needs to be removed, the page that has been idle for the longest time is removed. You need to implement a complete LRU (not an approximation), so you need to update the page order every memory reference.

Clock (aka Second Chance)

Ref: Page 217,218 in the text

The FIFO algorithm always removes the page that has been around the longest. Clock operates much like FIFO, except that instead of always removing the oldest page, it first checks to see if the page was recently used (used during the current time interval). If it has been used recently, the page gets a second chance and the second oldest page is now considered for removal. If the second oldest page has not been recently accessed, it is removed, otherwise we move on to the third oldest page, and so on. If the algorithm goes through all the pages and they have all been used recently, the algorithm will go back and take the oldest page and remove it.

To implement Clock you need to keep some information about the relative order of when pages are assigned to physical memory, and you need to keep track of the R bits for each page (that indicate whether the page has been accessed during the current time interval).

- Project Requirements

The following are the requirements for the project:

Grading: Your code will be graded according to the formula below. Note that to get full credit we must be able to understand your code (it must be commented!)

30% Basic simulator functionality (processes command line properly and reads, processes input lines correctly, etc).
60% Page replacement algorithm implementations (20% each).
10%Code quality (comments, organization, how hard is it to understand ?).

You can get partial credit for any part.

If you code does not compile and run under FreeBSD, you will lose at least 1/2 the relevant points ( partial credit will be awarded based on visual inspection of the code).

- How to Submit

Log in to WebCT at webct.rpi.edu using your RCS id and password. Once you get to MyWebCT click on "Operating Systems", and from there go to the homework drop boxes. Submit your files (individually, zipped or tarred) to the drop box labeled HW4.

-Resources