Docstoc

lab05_review

Document Sample
lab05_review Powered By Docstoc
					        COMP 252
    Operating Systems


Review Question week #7 & 8
Q. 1
 What is the meaning of the term busy
  waiting?


 What other kinds of waiting are there in an
  operating system?


 Can busy waiting be avoided altogether?
  Explain your answer.
Q. 1
 Busy waiting means

      a process is waiting for a condition to be satisfied in a
       tight loop without relinquishing the processor.



 Alternatively, a process could wait by relinquishing
  the processor, and block on a condition (e.g., I/O,
  semaphore) and wait to be awakened at some
  appropriate time in the future.
Q. 1
 Busy waiting can be avoided but incurs the
  overhead associated with:

     putting a process to sleep and having to wake
      it up when the appropriate program state is
      reached.
Q. 2
 Show that, if the P() and V() operations are
  not executed atomically, then mutual
  exclusion may be violated.
Q. 2
 Suppose the value of semaphore S=1 and
  threads P1 and P2 execute P(S) concurrently.

 Imagine the following situation:

     At time T0 thread P1 determines that the value
      of S is 1. At this point P1 gets interrupted.

     At time T1 thread P2 determines that the value
      of S is 1. At this point P2 gets interrupted.
Q. 2
    At time T2 thread P1 resumes execution,
     decrements S by 1 and enters the critical
     section.

    Later P1 gets interrupted while inside the
     critical section.

    At time T3 thread P2 resumes execution,
     decrements S by 1 and enters the critical
     section.
Q. 3
 A file is to be shared among different
  processes
     each process has a unique number

     the file can be accessed simultaneously by
      several processes, subject to the following
      constraint

          The sum of all unique numbers associated with
           all the processes currently accessing the file must
           be less than n
               Please complete the following monitor to
    Q. 3             coordinate access to the file
monitor file access {
                                              Initialization:
 int curr_sum = 0;
                                                  Lock
 int n;
 condition c;                               Condition variable


    void access file (int my_num) {
         while (curr_sum + my_num >= n)
         c.wait();
         curr_sum += my_num;
    }

    void finish access (int my_num) {
         curr_sum -= my_num;
         c.signal();
     }
}
Q. 4
 Please correct all errors in the following
  solution for the Bounded-Buffer problem.

      The buffer has size N, and is initially empty.
                  semaphore mutex, empty, full;
                  mutex=1;
                  empty=0;  //Should be empty=N
                  full=N;   //Should be full=0
Producer:                              Consumer:
do{                                    do{
        …                                      wait (mutex);
                                                          // Order should
        // Produce an item in nextp          wait (full); // be switched
        …                                      …
  wait(mutex);       // Order should           // remove an item from
        wait(empty); be switched
                     //                   buffer to nextc
        …                                      …
    // Add nextp to buffer                     signal (mutex);
        ….                                   signal (empty);
        signal (mutex);                        …
      signal (full);                           // consume the item in
}while(true);                             nextc
                                               …
                                       } while(true);
       Q. 5
        Consider this solution to the readers-writers problem:
            Writer                                Reader
do {                             do {
        wait (wrt) ;                      wait (mutex) ;
                                          readcount ++ ;
       // writing is performed            if (readcount = = 1) wait(wrt) ;
                                          signal (mutex)
       signal (wrt) ;
   } while (true)                               // reading is performed
                                           wait (mutex) ;
                                           readcount - - ;
                                           if (readcount = = 0) signal(wrt) ;
                                           signal (mutex) ;
                                        } while (true)
Q. 5
 What is the purpose of the semaphore “wrt”?

     To guarantee mutual exclusion to the critical
      section

 What is the purpose of the semaphore
  “mutex”?

     To guarantee mutual exclusion when updating
      the shared variable readcount
Q. 5
 Suppose a writer process is inside its critical section,
  while another writer and n readers are waiting outside
  their critical sections. Which semaphores are they
  waiting on, respectively?

      the writer is waiting on wrt

      the 1st reader is waiting on wrt

      and the other n-1 readers are waiting on mutex

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:159
posted:2/19/2010
language:English
pages:14