Sie sind auf Seite 1von 5

EXAM 1

CS 550 -- Spring 2014


ANSWER KEY
Lane Department of Computer Science and Electrical Engineering
West Virginia University
Feb. 11, 2014
This is a closed book test. No notes or books are allowed, and no electronic devices may be
used. Read each question carefully and pace yourself. Put all answers on the exam paper, in the
space provided.
1. (20 points: 4 each)
Explain briefly each of the following terms.
a)

Determinism
A requirement that processes maintain correct behavior, that is, their outputs are
determined only by their inputs, in spite of the actions of other processes. This is the key
goal of strategies focused on mutual exclusion, synchronization, and deadlock.

b)

Busy waiting
A strategy in which processes waiting for some condition repeatedly check the condition,
using processor time, instead of sleeping until they receive a signal indicating that the
condition is true.

c)

Monitor condition
A special data type in a monitor that accepts wait and signal operations but has
no memory. A wait operation places the calling process in a queue associated
with the condition and allows another process to use the monitor. A signal
operation releases the next waiting process, if any.

d)

Conditional Critical Region


A language construct in Concurrent Pascal that allows the programmer to
construct a critical section on some resource with an extra condition that must be
true before using the resource. The implementation is handled by the compiler.

e)

Ostrich algorithm
The strategy that deals with deadlock by ignoring it and hoping it does not happen often.
Based on the myth of an ostrich hiding its head in the sand.

-1-

CS 550 Spring 2014 Exam 1 Answer Key

2.

2. (15 points: 5 each)


Briefly explain and show the difference between each of the three terms definition,
implementation, and usage.
Definition: A specification of the properties of a mechanism, that is, how it should behave.
Implementation: A specification for how the mechanism can be built, using other
mechanisms that are available.
Usage: An example or description of how the mechanism may be put to use to solve a
problem. The example should not require any other mechanisms.

3. (15 points: 5, 10)


Below is one of the incorrect algorithms for mutual exclusion that we studied. As usual,
the algorithm is given for Process i.
INITIALIZATION:
flags[i] = FREE;
flags[j] = FREE;
ENTRY PROTOCOL:
flags[i] = BUSY;
while (flags[j]==BUSY) {do nothing};
EXIT PROTOCOL:
flags[i] = FREE;
a) Which of Dijkstras conditions is violated by this algorithm?
The no deadlock condition is violated. Deadlock is possible. Bounded waiting is also
violated (it is not necessary for you to include this).
b) Give a specific sequence of steps by Pi and Pj that illustrates the problem.
Pi: sets flags[i] to BUSY.
Interrupt occurs.
Pj sets flags[j] to BUSY.
Each process enters the while loop and continues to find the other process busy.

CS 550 Spring 2014 Exam 1 Answer Key

3.

4. (20 points: 5 each)


Consider the monitor solution for the readers and writers problem given on the last page
of this exam. Answer the following questions:
a) Why should the variable writing not be a semaphore?
The monitor ensures mutual exclusion. Semaphores have no place in an example of
monitor usage.
b) We know of three variations of the monitor: signal and wait, signal and continue,
signal and exit. Which of these variations would work correctly with this example?
Since all the monitor procedures that invoke signal do exit immediately afterwards,
any of the three variations would work properly.
c) Instead of the two monitor procedures start_read and end_read, why should we
not have a single procedure called read?
If the reading itself were done inside the monitor, only one process would be able to
read at a time. It is always important to put as little activity as possible inside the
monitor.
d) Write a model for a Reader process that uses this monitor.
READER:
while TRUE {
start_read();
(do the reading)
end_read();
(do something else)
}

CS 550 Spring 2014 Exam 1 Answer Key

4.

5. (10 points)
Are there any synchronization problems that can be solved with semaphores but not with
monitors? If so, give an example. If not, justify your answer.
NO -- each mechanism can solve exactly the same set of problems. To show this, note that
a monitor can be implemented using semaphores. Also, a semaphore can be implemented
using a monitor, although it may not be practical to do so.
6. (5 points)
Give one practical example of deadlock prevention in a computing system.
The Mutual Exclusion condition may be removed for printers by using spooling.
The Hold and Wait condition may be removed for memory by requiring all memory to
be allocated at once (not too practical but OK)
The No Preemption condition may be removed for memory by swapping.
The Circular Waiting condition may be removed for network channels by requiring
that they be requested in order.
(etc.)
7. (15 points)
Consider a system with three resource types and four processes. The system state is given
below. Is the system currently deadlocked? If not, show a sequence in which all processes can be
released. If so, identify the deadlocked processes.
ALLOCATED
p1
p2
p3
p4

A
0
1
1
0

B
1
1
0
0

C
0
0
0
2

REQUEST
A
0
0
0
0

B
0
2
1
2

C
1
0
2
0

AVAILABLE
A B C
0 0 1

The system is deadlocked. p1 can be removed; however, p2, p3, and p4 are all stuck.

CS 550 Spring 2014 Exam 1 Answer Key

READERS AND WRITERS PROBLEM: MONITOR SOLUTION


MONITOR
DATA:
condition OK_to_read, OK_to_write;
boolean writing;
int readers_active, readers_waiting;
INITIALIZATION:
writing = FALSE;
readers_waiting = 0;
readers_active = 0;
PROCEDURES:
void startread() {
if (writing) then {
readers_waiting = readers_waiting + 1;
OK_to_read.wait;
readers_waiting = readers_waiting - 1;
}
readers_active = readers_active + 1;
OK_to_read.signal;
}
void endread() {
readers_active = readers_active - 1;
if (readers_active == 0) OK_to_write.signal;
}
void startwrite() {
if (writing OR (readers_active > 0)) OK_to_write.wait;
writing = TRUE;
}
void endwrite() {
writing = FALSE;
if (readers_waiting > 0) OK_to_read.signal;
else OK_to_write.signal;
}

END OF MONITOR

5.

Das könnte Ihnen auch gefallen