Week 10 Laboratory Exercises

Objectives

  • practicing file operations generally
  • learn how to build a pathname from a environment variable
  • understanding how virtual memory works

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started

Set up for the lab by creating a new directory called lab10 and changing to this directory.
mkdir lab10
cd lab10

There are some provided files for this lab which you can fetch with this command:

1521 fetch lab10

If you're not working at CSE, you can download the provided files as a zip file or a tar file.

Exercise — individual:
Append to a Diary File

We wish to maintain a simple diary in the file $HOME/.diary

Write a C program, diary.c, which appends 1 line to $HOME/.diary.

The line should be its command-line arguments separated by a space followed by a '\n'.

diary.c should print nothing on stdout. It should only append to $HOME/.diary.

dcc diary.c -o diary
./diary Lisa
cat $HOME/.diary
Lisa
./diary in this house
./diary we obey the laws of thermodynamics
cat $HOME/.diary
Lisa
in this house
we obey the laws of thermodynamics

When you think your program is working, you can use autotest to run some simple automated tests:

1521 autotest diary 

When you are finished working on this exercise, you must submit your work by running give:

give cs1521 lab10_diary diary.c

You must run give before Monday 22 November 21:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Simulate LRU Replacement of Virtual Memory Pages

Your task is to simulate least-recently-used (LRU) replacement of virtual memory pages.

Write a C program, lru.c, which takes two arguments, both integers.

This will be respectively the size in pages of simulated physical memory and and the size in pages of simulated virtual memory.

lru.c should then read integers from stdin until EOF.

Each integer will be the number of a virtual page being accessed.

lru.c should print one line of output for indicate what action occurs with a phyical memory of the given size and LRU replacement.

dcc lru.c -o lru
./lru 4 6
Simulating 4 pages of physical memory, 6 pages of virtual memory
5
Time 0: virtual page 5 loaded to physical page 0
3
Time 1: virtual page 3 loaded to physical page 1
5
Time 2: virtual page 5 -> physical page 0
3
Time 3: virtual page 3 -> physical page 1
0
Time 4: virtual page 0 loaded to physical page 2
1
Time 5: virtual page 1 loaded to physical page 3
2
Time 6: virtual page 2  - virtual page 5 evicted - loaded to physical page 0
2
Time 7: virtual page 2 -> physical page 0
3
Time 8: virtual page 3 -> physical page 1
5
Time 9: virtual page 5  - virtual page 0 evicted - loaded to physical page 2

In the files for this week's lab you'be been give code which implements a suitable data structure to store information about accesses,

For each access this function is called:

void access_page(int virtual_page, int access_time, int n_physical_pages, struct ipt_entry *ipt) {

    // PUT YOUR CODE HERE TO HANDLE THE 3 cases
    //
    // 1) The virtual page is already in a physical page
    //
    // 2) The virtual page is not in a physical page,
    //    and there is free physical page
    //
    // 3) The virtual page is not in a physical page,
    //    and there is no free physical page
    //
    // don't forgot to update the last_access_time of the virtual_page

    printf("Time %d: virtual page %d accessed\n", access_time, virtual_page);
}
You can complete the lab by adding code to this function (you are also free to write your own program).

When you think your program is working, you can use autotest to run some simple automated tests:

1521 autotest lru 

When you are finished working on this exercise, you must submit your work by running give:

give cs1521 lab10_lru lru.c

You must run give before Monday 22 November 21:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Challenge Exercise — individual:
Simulate Virtual Memory

Your task is a simple simulation of virtual memory pages.

Write a C program, page_table.c, which takes two arguments, both integers.

This will be respectively the size in pages of simulated physical memory and and the size in pages of simulated virtual memory.

page_table.c should then read from stdin until EOF.

Each line will contains a letter describing an action and the number of a virtual page.

page_table.c should print one line of output for indicate what action occurs with a phyical memory of the given size and LRU replacement.

An action will be described by one of these 5 letters.

  • R make page available for read access
  • W make page available for write access
  • U make page no longer available for access
  • r make a read access to a page
  • w make a write access to a page
Page accesses are checked:
dcc page_table.c -o page_table
./page_table 4 6
Simulating 4 pages of physical memory, 6 pages of virtual memory
r 0
Time 0: virtual page 0  - read access - illegal
R 0
Time 1: virtual page 0 mapped read-only
r 0
Time 2: virtual page 0  - read access - loaded to physical page 0
r 0
Time 3: virtual page 0  - read access - at physical page 0
w 0
Time 4: virtual page 0  - write access - illegal
W 0
Time 5: virtual page 0 mapped read-write
w 0
Time 6: virtual page 0  - write access - at physical page 0
And a LRU replacement strategy should be implemented like the previous exercise, for example:
./page_table 4 6
Simulating 4 pages of physical memory, 6 pages of virtual memory
R 0
Time 0: virtual page 0 mapped read-only
R 1
Time 1: virtual page 1 mapped read-only
R 2
Time 2: virtual page 2 mapped read-only
W 3
Time 3: virtual page 3 mapped read-write
W 4
Time 4: virtual page 4 mapped read-write
r 0
Time 5: virtual page 0  - read access - loaded to physical page 0
r 1
Time 6: virtual page 1  - read access - loaded to physical page 1
w 4
Time 7: virtual page 4  - write access - loaded to physical page 2
w 3
Time 8: virtual page 3  - write access - loaded to physical page 3
r 0
Time 9: virtual page 0  - read access - at physical page 0
r 2
Time 10: virtual page 2  - read access -  virtual page 1 evicted - loaded to physical page 1
A reference implementation is available by running
1521 page_table 4 6
Simulating 4 pages of physical memory, 6 pages of virtual memory
...

When you think your program is working, you can use autotest to run some simple automated tests:

1521 autotest page_table 

When you are finished working on this exercise, you must submit your work by running give:

give cs1521 lab10_page_table page_table.c

You must run give before Monday 22 November 21:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 11 Monday 21:00:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1521 classrun -sturec