Vous êtes sur la page 1sur 3

CECS 526 -- Exercises on Concurrent Processes & Synchronization Mechanisms

Part I
1. Consider the following program. Determine the proper lower and upper bounds on the final value of the shared variable tally output by this concurrent program. Assume processes can execute at any relative speed and that a value can only be incremented after it has been loaded into a register by a separate machine instruction. const n := 5; var tally : integer; procedure total; var count : integer; begin for count := 1 to n do tally := tally + 1; end; begin (* main program *) tally := 0; parbegin total; total (* invoke two concurrent processes, both to execute the total procedure *) parend; write (tally) end.

2. The following program is a software solution to the mutual exclusion problem for two processes. Find a
counterexample that demonstrates that this solution is incorrect. var blocked : array[0..1] of boolean; (* blocked is an array of two Boolean elements *) turn : 0..1; (* turn can have value 0 or 1 *) procedure P ( id : integer ) begin repeat blocked[id] := true; while turn id do begin while blocked[1 id] do; (* a do-nothing loop to implement busy waiting *) turn := id end; < critical section > blocked[id] := false; < remainder of procedure > until false end; begin (* main program *) blocked[0] := false; blocked[1] := false; turn := 0; parbegin P(0); P(1) (* invoke two concurrent processes, both to execute procedure P *) parend end.

3. The following bakery algorithm due to Lamport is a software approach to mutual execution for n processes. The
shared arrays choosing and number in the algorithm are initialized to false and 0, respectively. The ith element of each array may be read and written by process i but only read by other processes. The notation (a, b) < (c, d) is defined as (a < c) or (a = c and b < d). a) Examine the algorithm to determine b) Does it enforce mutual exclusion? c) Can deadlock occur?

var choosing : array[0..n-1] of Boolean; (* array of n Boolean values *) number : array[0..n-1] of integer; (* array of n integers *) (* mutual exclusion algorithm for process i *) repeat choosing[i] := true; number[i] := 1 + max(number[0], number[1], , number[n-1]); choosing[i] := false; for j := 0 to n-1 do begin while choosing[j] do; (* a do-nothing loop to implement a busy waiting *) while number[j] 0 and (number[j],j)<(number[i],i) do; (* also a do-nothing loop *) end; < critical section > number[i] := 0; < remainder of process i> forever.

4. Recall that a process that enters a semaphore queue will stay there until awaken by another process. In some
applications a process may wish to be awaken when certain time is up, even if the condition that it is waiting for has not arrived. Semaphore will not work for such applications. Design a mechanism such that a process executing a procedure call WaitUntil(S, T) will be queued until either the condition represented by S has occurred or absolute time T has been reached. You may assume the existence of a clock and a timer interrupt. Your specification must include a timer interrupt handler and other associated procedures.

5. There are N blocks of storage, each of which can hold one unit of information. Initially these N blocks are empty and
linked on freelist. Three processes communicate using shared memory in a manner as depicted in the code below. Rewrite the code for the processes, using semaphores to implement the necessary mutual exclusion and synchronization. The solution must be deadlock-free and concurrency should not be unnecessarily restricted. In the code below, unlink(L) means remove a block from list L and return that block, and link(B, L) means insert block B into list L. (* shared variables *) var freelist, list1, list2 : list of block; (* Process 1 *) var b : ptr to block; repeat b := unlink(freelist); produce information in block b; link(b, list1) forever (* Process 2 *) var x, y : ptr to block; repeat x := unlink(list1); y := unlink(freelist); use block x and produce information in block y; link(x, freelist); link(y, list2) forever (* Process 3 *) var c : ptr to block; repeat c := unlink(list2); consume information in block c; link(c, freelist) forever 6. Do problem 5 using sequencers and event counts.

Part II (The following problems are selected from Chapter 2 of Advanced Concepts in Operating Systems by Singhal and Shivaratri.) 1. Why does the interrupt disable method to achieve mutual exclusion not work for multiprocessor systems? 2. In the design of the readers-writers problem using a monitor, why is it advisable to keep the protected resource external to the monitor? 3. Explain what the following path expressions do: a) path { open + read } ; close end b) path { openread ; read } ; { openwrite ; write } end

4. On the one hand, access to the monitor should be mutually exclusive while on the other hand, procedures of a monitor
should be reentrant. Why? 5. How do serializers solve several deficiencies of monitors?

6. Write a monitor to solve the readers-writers problem in a FCFS order. It should work as follows: It serves readers and
writers in a FCFS order; however, if there are many readers back to back, it will serve all those readers concurrently. 7. Write a monitor to solve the readers-writers problem that works as follows: If readers and writers are both waiting, then it alternates between readers and writers. Otherwise it processes them normally (i.e., readers concurrently and writers serially).

8. Write a monitor to solve the producer-consumer problem.


9. Give a solution to the producer-consumer problem using Ada. 10. Write an Ada task to solve the readers-writers problem with readers priority.

Vous aimerez peut-être aussi