CMSC 412 (shankar, exam 1, oct 27, 1994)                       Name:-------------

Total points 50. Total time 75 mins. Closed book. There are 7 problems over 5 pages.



1. [7 pts]  The following 3 processes implement a mutual exclusion algorithm.
 

tex2html_wrap548 tex2html_wrap550 tex2html_wrap552

Assume the following:

(a) What is the throughput, i.e. number of critical sections executed per second.

(b) What can you say about the waiting time of a process, i.e. the time taken to execute tex2html_wrap_inline438 .


2. [5 pts]  Using semaphores, modify the following so that at any time at most three of the processes, tex2html_wrap_inline452 , are executing their tex2html_wrap_inline454 's.
 

tex2html_wrap554 tex2html_wrap556 tex2html_wrap_inline464 tex2html_wrap558


3. [5 pts]  Is the following following a correct solution to the 2-process mutual exclusion problem? Justify your answer.

tex2html_wrap_inline470 : array [0..1] of { true, false }. Initialized to false.
tex2html_wrap_inline472 : 0..1. Initialized to 0.
 

tex2html_wrap560 tex2html_wrap562
 


4. [8 pts]  You have to solve the 4-process mutual exclusion problem by repeatedly applying Peterson's 2-process mutual exclusion solution. Assume the following constructs for Peterson's solution:

Using the above constructs, solve the 4-process mutual exclusion problem, i.e. supply the variables and code for the boxes below. Your solution must not use any other synchronization primitives (such as test-and-sets, disabling/enabling interrupts, semaphores, etc.).

tex2html_wrap564    // shared variables

tex2html_wrap566 tex2html_wrap568 tex2html_wrap570 tex2html_wrap572

Hint: think of a binary tree with four leaves representing A, B, C, and D.


5. [5 pts]  In project 2, the top of the stack of a ready process contained the address of dispatch(), and you had the following:

void scheduler ()
 { ...
   /* get next ready process,
      put it in run queue,
      update \_SP and \_SS to process's stack */
      asm retf  /* go to dispatcher */
 }

void dispatch ()
 { asm {
    mov sp, bp
    pop bp
    pop bp
    pop di
    ...
  }
If you replaced the asm retf in the scheduler by dispatch(), what changes would be required elsewhere?


6. [5 pts]  In project 2, you had

void interrupt system_service ( sbp, sdi, ssi, sds, ses, sdx, ... , proc, argc, argv )

What is the purpose of specifying all these parameters in the definition of system_service?


7. [15 pts]  You have to solve a variation of the readers-writers problem, in which multiple writers can write at the same time. Specifically, there are readers and writers. Multiple reads at the same time are allowed. Multiple writes at the same time are allowed. A read and a write at the same time is not allowed.

Provide a solution using semaphores with the following properties:

Below is a skeleton program for you to build upon by supplying code for the boxes and perhaps introducing more variables. You are also welcome to disregard this skeleton and come up with something else.

tex2html_wrap_inline536 integer initialized to 0.       /* number of reads currently executing */
tex2html_wrap_inline538 integer initialized to 0.          /* number of readers currently waiting */
tex2html_wrap_inline540 semaphore initialized to 0.      /* readers wait here */
tex2html_wrap_inline542 integer initialized to 0.       /* number of writes currently executing */
tex2html_wrap_inline544 integer initialized to 0.          /* number of writers currently waiting */
tex2html_wrap_inline546 semaphore initialized to 0.     /* writers wait here */

tex2html_wrap574 tex2html_wrap576




SOLUTION TO EXAM 1 CMSC 412 (shankar): Fall 94

1. [7 pts]

Assume without loss of generality that round-robin queue has processes in the order A, B, and C. Here is the evolution starting from time 0.

     A executes tex2html_wrap_inline444 till time                     5 ms          A  waited 0 ms
     B executes tex2html_wrap_inline438 till time                            10 ms         B  starts wait at 5 ms
     C executes tex2html_wrap_inline438 till time                          15 ms        C starts wait at 10 ms
    A executes tex2html_wrap_inline444,tex2html_wrap_inline440 till time    20 ms        A  completes

    B executes tex2html_wrap_inline444 till time                     25 ms       B waited 15 ms
    C executes tex2html_wrap_inline438 till time                           30 ms
    A executes tex2html_wrap_inline438 till time                            35 ms       A starts wait at 30 ms
    B executes tex2html_wrap_inline444tex2html_wrap_inline440 till time   40 ms       B completes

   C executes tex2html_wrap_inline444 till time                      45 ms      C waited 30 ms
   A executes tex2html_wrap_inline438 till time                              50 ms
   B executes tex2html_wrap_inline438 till time                              55 ms      B starts wait at 50 ms
   C executes tex2html_wrap_inline444tex2html_wrap_inline440 till time    60 ms      C completes

and so on

(a) [5 pts] one tex2html_wrap_inline444 executed every 20 ms, or equivalently 50 per second.

(b) [2 pts] average waiting time is 30 ms (ignoring initial transient).

2. [5 pts]  Introduce the following: a semaphore, say gate, initialized to 3; P(gate) before every tex2html_wrap_inline454 ; and V(gate) after every tex2html_wrap_inline454 .

3. [5 pts]  No. It allows both processes to enter CS as follows.
 tex2html_wrap_inline670  executes  tex2html_wrap_inline672  true 
- while tex2html_wrap_inline674 do 
- while tex2html_wrap_inline502 do skip 
tex2html_wrap_inline670 at tex2html_wrap_inline680
tex2html_wrap_inline682 executes  tex2html_wrap_inline684 true 
- while tex2html_wrap_inline686 do 
tex2html_wrap_inline682 at CS)
tex2html_wrap_inline670 executes  tex2html_wrap_inline680 
- while tex2html_wrap_inline674 do 
tex2html_wrap_inline670 at CS)
 

Can also have starvation as follows:

 (1)  tex2html_wrap_inline682  executes -  tex2html_wrap_inline684  true ; ... ; enters  tex2html_wrap_inline444
 (2) tex2html_wrap_inline670 executes - tex2html_wrap_inline672 true ; enters while tex2html_wrap_inline502 do skip
 (3) tex2html_wrap_inline682 executes - tex2html_wrap_inline684 false ; branches back ; tex2html_wrap_inline684 true ; tex2html_wrap_inline444
Steps (2), (3), (2), (3), ... can repeat endlessly. Each process is getting a share of CPU, but tex2html_wrap_inline670 starves.

4. [8 pts]  Introduce three sets of variables, say, tex2html_wrap_inline720tex2html_wrap_inline722 tex2html_wrap_inline724tex2html_wrap_inline726 tex2html_wrap_inline728tex2html_wrap_inline730
 

tex2html_wrap838 tex2html_wrap840 tex2html_wrap842 tex2html_wrap844
 

[3 points for recognizing three applications of Peterson's algorithm, 2 points for declaring the variables, 3 points for the entry and exit code.]

5. [5 pts]  Calling dispatch() instead of using retf places the return address (a location within scheduler) on the stack, after which dispatch() puts its frame on the stack.

The simplest fix is to modify dispatch() so that it removes the frame (as is currently done), then removes the next 4 words on stack (skipping return address and address of dispatch on stack), and continue as before (restore registers).

A more natural fix is to not have the address of dispatch() on top of a ready process's stack. proc_start and defer should not put address of dispatch () on stack. The beginning of dispatch() should be modified to remove the frame (as is currently done) and the return address to scheduler.

6. [5 pts]  To access variables that are already on the process's stack, and have the compiler generate the addressing code.

7. [15 pts]

Add semaphore tex2html_wrap_inline746 initialized to 1 to protect the variables.

tex2html_wrap846 tex2html_wrap848

[2 points for tex2html_wrap_inline746 , 4 for updating tex2html_wrap_inline818 and tex2html_wrap_inline820 (consitently), 3 for updating tex2html_wrap_inline822 and tex2html_wrap_inline824 , 2 for P( tex2html_wrap_inline826 and P( tex2html_wrap_inline828 , 4 for P( tex2html_wrap_inline826 and P( tex2html_wrap_inline828 .]

Common mistakes: Using more than one tex2html_wrap_inline746 . Doing only one V when last reader (or writer) leaves (causes readers and writers to alternate in heavy load, readers can be left stranded if no writers show up, ...).
 


A. Udaya Shankar

Wed Oct 7 12:17:05 EDT 1998