> released: 03/23, 11:00 > due: 03/29, 23:59 > > Answer the following questions. > Submit your answers to Canvas assignments. > There is an entry for this homework. > > > 1. Page replacement policy: CLOCK (3 points) > > Suppose you have a machine named CS5600-clover with 4 pages of memory and a large SSD. > A process uses 6 pages; page P0-P3 are in the memory; P4-P5 are on SSD. > OS runs CLOCK algorithm for page replacement, and a visualization is below. > > P0 (A=0) > ^ > | > P3 + P1 > (A=0) (A=0) > > P2 (A=0) > > For CLOCK algorithm: > - The "hand"/"pointer" will go clock-wise. > - When evicting page, check the page pointed by the "hand" > -- if access bit is set (A=1), clear the bit (A=0) and advance one position > (without evicting the page). > -- if A=0, evict the pointed page, load the new page, and advance one position. > -- what's the access bit of the newly loaded page? answer: A=1 > because the page is now being used. > > Notes: > * the CLOCK will only be triggered when "running out of memory": the required > page is not in the memory.) > * CLOCK is a family of algorithms. You might see different variants but the > core is the same---CLOCK can be efficiently implemented with few resources. > > If the memory accesses in the following are: > P0, P1, P2, P4, P5 > > > 1.a How many page swaps will happen? (1 point) > 2 page swaps > > 1.b For each swap, which page has been swapped > in and which page has been swapped out? (2 points) > > first time: P4 replaces P3 second time: P5 replaces P0 [ Here are how things happen: (1) after P0, P1, P2 P0 (A=1) ^ | P3 + P1 (A=0) (A=1) P2 (A=1) (2) then, P4 replaces P3 P0 (A=0) ^ | P4 + P1 (A=1) (A=0) P2 (A=0) (3) finally, P5 replaces P0 P5 (A=1) P4 +--> P1 (A=1) (A=0) P2 (A=0) ] > > > 2. Thrashing (3 points) > > We're using the same machine, CS5600-clover; and CS5600-clover runs CLOCK. > Now a different program is running. > It uses 5 pages and will linear scan all five pages > and repeat: P0, P1, P2, P3, P4, P0, P1, ... > > The initial state is: P0-P3 are in memory; P4 is on SSD. > > P0 (A=0) > ^ > | > P3 + P1 > (A=0) (A=0) > > P2 (A=0) > > How many swaps will happen for the first 20 page reads? > (that is, reading P0-P4 four times) > There will be 16 swaps. [Except for the fist four page reads hit memory, others will need to swap. This is thrashing---the system kicks out a page and loads it back immediately, a constant kicking-and-loading. ] > > > 3. Run the mmap experiment (2 points) > > Read the mmap handout (https://naizhengtan.github.io/23spring/notes/handout_w11b.pdf), > Copy the code to a file "mmap.c". > Do the exercise on the right panel of the handout with 1G file and answer the question. > > Question: > Which runs faster, option 1 or option 2? by how much on your machine? > Any answers (except empty) are fine. [ mmap should be faster. It can be anywhere between 10% faster to 1000x faster. ] > > > 4. Manipulating IO devices (2 points) > > Read "(b) Setting the cursor position" in week11.b handout > (https://naizhengtan.github.io/23spring/notes/handout_w11b.pdf). > > Given the console is 80x25 (80 characters per line with 25 lines) > If one wants to put the cursor at the first character of the second line, > what should they do? > (hint: you can find this piece of code in k-hardware.cc.) > Fill in the TODOs below. > > outb(0x3D4, 14); > outb(0x3D5, /*TODO: A*/); > outb(0x3D4, 15); > outb(0x3D5, /*TODO: B*/); > > > Your answer: > A: 0 B: 80 [Try it yourself. Hopefully, you've done that.]