Each logical address in infile is saved as 8 bytes (unsigned long) in binary format. So don't open it with a text editor because the content will look like garbage. Instead, your program should open the file in binary format and read each logical address into an unsigned int variable. To verify that you read the correct sequence of memory accesses, you can first print out the logical address that you have read. You can test your program with the given simple part1testsequence, where the first logical address should be 0x0000000000000044 and the second one should be 0x0000000000000224. When printing it use "%lx" format specifier. But the leading 0's will not be seen, so you may see 44 and 224 in HEX format. But 0's are in the memory (see the help videos at the end for more explanation)!
For each logical address in the
input sequence file, you will use the pagetable
given in the above figure to perform the address translation and generate a corresponding
physical address that will be printed out into the file specified as the 2nd
cmd-line parameter to part1.c.
Your program need to translate each logical address in the intput file to a corresponding physical address using the static page table (PT) given in the above figure, and print out each phsical address into the file specified as the 2nd cmd-line parameter. The outfile must have the same format as the given part1testsequence file. So, each physical address must be written in binary as an 8 byte (unsigned long) value into the output file. Note: you need to hard code the above page table with the given frame numbers as int PT[8]={2, 4, 1, 7, 3, 5, 6, -1}; Then you can use this table to translate a given logical address into a physical addresses and print it into the outfile using the same binary format of input file (i.e., represent each physical address as unsigned int, too).
Once you test your program with the part1testsequence, and
you are sure the program performs correct address translation (see the
sample output at the end), use the
following sequence file as
the input file for logical addresses sequence to generate the translated
physical addresses and put them into a file called part1-output.
Then, compute the md5sum checksum on part1-output.
Type the checksum for part1-output into REPORT.txt.
Then you can delete part1-output. Note: the md5sum
checksum program computes a magic number whihc we can use to determine if
you genarated the same output file or not without actually receiving your
output file. Please google md5sum to learn more about it.
Note: to simplify Part 1, we asked you to hardcode page table so you
can easily map page numbers to frame numbers when performing any address translation.
In the next part, you will dynamically fill the page table!
Now your page table should be an array of page table entry (PTE) structure with at least two fields: a valid-bit and frame-number. Initially all valid-bit fields should be set to 0. Also all frames are initially free except frame 0, which is allocated for OS. So you may also need an array to keep track of which frame is free and which frame is allocated. Then the program reads a logical address and finds the page number. It then checks the valid-bit of the corresponding PTE. If it is 1, we can easily utilize the same functions from part 1 to map this logical (virtual) addresses into a physical address. If not, the program needs to find an available frame for this page and update its PTE (i.e., set valid-bit to 1 and store the frame numer of the allocated frame into frame-number field).
You will initially use the following frame allocation scheme: allocate the physical frames in the order of 1, 2, 3, ....
But, when there are no free physical frames, you will need to use the
LRU policy for page replacement. That means, the page that is least
recently used (accessed) will be removed and the frame holding it will be allocated
for the new page.
Note that, once a frame is selected to be freed, you need to do two things:
(1) First, you should invalidate the old entry of page table so that we
don't have two virtual pages pointing to the same physical frame.
(2) Second, you need to initialize a new pagetable entry (PTE) to point to
the allocated frame. You should also set up a reverse mapping on the frame to
the PTE for quick PTE modifications in the future when a frame is
re-allocated.
If a page is accessed, you must update its placement in the frame list so
that it will not be evicted soon (based on the LRU policy).
Watch the help videos at the and for more information about implementation
details.
The run your program for translating
part2sequence into the output
for part2-output.
As in part 1, type the md5sum of part2-output into
REPORT.txt along with the number of pagefaults encountered
when part 2 is translating logical addresses to physical addresses.
Assign03-part1-help: Watch tk07-assign03-help-for-part1-15min
In the help video (13:00), I wrote PA = fnum << d + dnum; Actually, I should have written PA = (fnum << d) + dnum; or PA = fnum << d | dnum;
In the help video, I used printf("%lx ", PA); Actually you should also use printf("%lx", LA); to see what you read from the input file: the first two values of LA are 44 and 224 in HEX. I got some questions that even though unsigned long is 8 bytes you don't see leading 0's. Actually, in computer's memory, LA and PA oocupy 8-byte space and hold all the leadign 0's. So, you don't need to do anything. Just look at LA and PA as a 64-bit binary numbers! Also we read/write them from/to files in binary format, so all 8 bytes will be in the files as well. However, printf ignores these leading 0's; but don't worry about that because we just use it to show the values for us. If you like to see all leading 0's then use the following printfs: printf("%016lx\n", LA); ... printf("%016lx\n", PA);
Also I did not include error checkings. You should check what the I/O functions return.. For example, it is a good idea to use if(fread(&LA, sizeof(unsigned long), 1, fp) != 1) break; instead of just fread(&LA, sizeof(unsigned long), 1, fp);
Assign03-part2-help: Watch tk08-assign03-help-for-part2-26min
Clarification: you need to initially set all valid/invalid values in page table to 0 as I described in the video. But, when writing the code (12:50), I did not write this initialization step and just started with fopen... I was expecting you to take care of initialization step! So simply set all valid/invalid values to 0 before fopen...