CSCI.4210 Operating Systems
Fall, 2003
Programming Assignment 4

For this assignment, you will write a simulation of a virtual memory system. This program does not require any system calls, and so your program should compile and run without warnings or errors on either windows or Unix.

Each logical address is 16 bits, and each process has a total of 64K of virtual address space. Each page and each page frame is 1K bytes ($1024$ or $2^{10}$). There are a total of 20 pages of physical memory available for user processes (numbered 0 .. 19) (the kernel has its own pages, but that is not a part of this assignment).

The TLB has room for 8 entries.

Your program will read a file called data.txt. This will consist of a long list of memory references representing individual instructions. Each entry will have three fields, the process number, the memory address of the instruction, and the memory address of the data. Each process has its own memory map. Assume that all memory references of data modify the data, but no memory reference to instructions ever modifies it.

You are to simulate the execution of this run stream by keeping track of the memory map and the contents of the TLB.

When a page fault occurs, your program should call a page fault handler which executes the clock algorithm. You should have two versions of this. In version 1, the program replaces the first unreferenced page that it comes to, copying the contents to disk if necessary. In the second version, your algorithm replaces the first page which is both unreferenced and where the dirty bit is off so it does not need to copy the contents to disk. If there is no such page, it copies the first unreferenced page as in the first version. If your program is run without arguments, use the first version of the clock algorithm, and if your program is run with any argument, run thge second version of the clock algorithm.

The TLB uses a pure least recently used algorithm.

Address 0 is a call to the exit system call, this means that that process is terminating. When a process terminates, all of its pages can be marked as available. If there are available pages when a page fault occurs, you should not run the clock algorithm, just use the first available page. The same is true of the TLB.

Here is a short data file

1 792 35284
1 4468 33604
2 5256 38132
2 2296 32772
At line 1, process one executes an instruction at address 792 which accesses data at address 35283. At line 2 process 1 executes an instruction at line 4468 which accesses data at address 33604. At line 3, process 2 executes an instruction at address 5256 which accesses data at address 38132. Note that these are logical (virtual addresses) and so process 1's data at 35284 is different from process 2's data at 35284.

All addresses are in decimal. Hint: the most significant 6 bits represent the frame number. Thus address 4468 is in page 4 (the offset within the frame is 372, but you do not need to do anything with this), address 33604 is in page 32, address 38132 is in page 37, and so on.

The output should be written to a file called out.txt. Here is what the output should look like: Each time there is a page fault, print a line like this
Page Fault: line n1, proc n2, LP n3, PP n4
where n1 is the line number of the input file, n2 is the process number, n3 is the logical page which triggered the page fault (0 .. 63) and n4 is the physical page which was replaced (0..19).

Whenever the TLB changes, print a line like this:
TLB: line n1 Entry n2 proc n3 LP n4 PP n5
where n1 is the line number of the input file, n2 is the entry number in the TLB (0..7), n3 is the process number, n4 is the logical page (0..63) and PP is the physical page (0..19).

Note that there should be a TLB entry following every Page Fault.

The result should be deterministic, i.e. everyone should get the same answer. In order to assure this, if there are several possible empty slots in the TLB or in physical memory, choose the lowest numbered one.

You will be provided with a working executable. and sample input files. If the output of your program differs at all from the output from the working executable, at least one of us made an error.

The name of the input file will be data.txt and the name of the output file will be out.txt.

At the termination of the simulation, your program should display the number of steps executed (i.e. the number of lines in data.txt), the number of TLB misses, the number of Page faults, and the number of dirty pages that had to be written back to disk.

This project is due at 11:59 on Friday, November 7.