Operating Systems (CS 377):
Homework 3

Due: October 24, 2002 23:59

What to Hand In

Everyone must hand in their own work. Place in your Hand-In directory (~/cs377/hw3/) one of the following: README (raw text file), README.ps (a postscript file), README.pdf (pdf file), README.html. The file must contain your name, user id, and answers to the questions listed below.

Q1. Define "Hold and wait," one of the necessary conditions required for deadlock to occur.

Q2. In what situation does multilevel feedback queue scheduling lead to starvation?


Readers/Writers problem with favored writers.

Below is the implementation of readers/writers that we have been examining in class. However, we will assume that the implementation of the semaphore is the decrement-last implementation (the one that we are using in Semaphore.java). The task for the next set of questions is to trace the reader/writer code for multiple threads for multiple scenarios and track the state of the 5 semaphores (both the semaphore value and its corresponding wait queue) and the "readers" and "writers" state variables. Assume for simplicity (except where noted otherwise) that the threads maintain the CPU unless they explicitly block or are in the middle of the reading or writing operation (R-10 and W-7, respectively).
LINE   CODE
       
       Write(){
W?-1    write_mutex.Wait();  // ensure mutual exclusion for writer bookkeeping
W?-2      writers += 1;      // another pending writer
W?-3       if (writers == 1)  // block readers
W?-4          read_block.Wait(); 
W?-5    write_mutex.Signal();

W?-6    write_block.Wait();  // ensure mutual exclusion for data write
W?-7      ##perform write##
W?-8    write_block.Signal();

W?-9    write_mutex.Wait();  // ensure mutual exclusion for writer bookkeeping
W?-10     writers -= 1;      // writer done
W?-11     if (writers == 0)  // enable readers
W?-12        read_block.Signal(); 
W?-13   write_mutex.Signal(); 
        }


       Read(){
R?-1    write_pending.Wait();  // ensures at most one reader will go
                               // before a pending write
R?-2      read_block.Wait();
R?-3        read_mutex.Wait();      // ensure mutual exclusion
R?-4           readers += 1;         // another reader
R?-5           if (readers == 1)     // synchronize with writers
R?-6              write_block.Wait(); 
R?-7        read_mutex.Signal();
R?-8      read_block.Signal();
R?-9    write_pending.Signal();

R?-10   ##perform read##

R?-11   read_mutex.Wait();      // ensure mutual exclusion
R?-12     readers -= 1;         // reader done
R?-13     if (readers == 0)     // enable writers
R?-14        write_block.Signal(); 
R?-15   read_mutex.Signal(); 
        }

Example: Starting from the initial configuration, a writer begins to write (we will refer to this as W-a). While the writing is taking place, three different readers (R-a, R-b, R-c) attempt to read the database. Once all three readers have blocked, the writer completes, allowing the three readers to begin reading. One possible trace is as follows:

thread and line write_mutex read_mutex write_pending write_block read_block writers readers
value Q value Q value Q value Q value Q
Init 1 - 1 - 1 - 1 - 1 - 0 0
W-a1 0
W-a2 1
W-a4 0
W-a5 1
W-a6 0
SWITCH: R-a
R-a1 0
R-a2 0 R-a
SWITCH: R-b
R-b1 0 R-b
SWITCH: R-c
R-c1 0 R-b, R-c
SWITCH: W-a
W-a8 1
W-a9 0
W-a10 0
W-a12 0 -
W-a13 1
SWITCH: R-a
R-a3 0
R-a4 1
R-a6 0
R-a7 1
R-a8 1
R-a9 0 R-c
SWITCH: R-b
R-b2 0
R-b3 0
R-b4 2
R-b7 1
R-b8 1
R-b9 0 -
SWITCH: R-c
R-c2 0
R-c3 0
R-c4 3
R-c7 1
R-c8 1
R-c9 1

Q3. Show the corresponding table for the following scenario: From the initial configuration, a reader (R-a) starts reading. A writer (W-a) then attempts to initiate a write; a second reader (R-b) next attempts to initiate a read. The first reader then completes, which allows the first writer to begin writing. On completion of the write, the 2nd reader starts and then completes the read.

Q4. Show the corresponding table for the following scenario: A reader (R-a) begins reading, but is swapped off of the CPU after executing line 2. Two other readers (R-b, R-c) are then initiated. Once these two block, R-a continues into the reading state (line 10). R-b and R-c then move into the reading state. Then, one-by-one, the three readers complete the reading process.

Q5. How much time did you spend on this homework assignment?


fagg@cs.umass.edu

Last modified: Mon Oct 14 09:32:53 2002