Tutorial 3 Solutions
Tutorial 3 Solutions
Deadlock can be avoided if system knows ahead of time the sequence of requests
associated with each of the active processes. When a system receives a request, the
system plays the “What-if this request is satisfied then will it leave the system in an Un-
safe state?” If it does then the request is denied.
2) Suppose the current system resource allocation is described by the following table. A
request is received by job 1 for 2 resources. Determine whether or not the request
should be served in order to avoid a deadlock. Explain how the decision was reached.
Suppose a request is received from job 1 for 2 resources. If the request is granted it will
leave the system in the following un-safe state:
Reason: Only one unit will be left which will not satisfy any one job’s remaining needs. This
request should be refused.
3) Explain the deadlock detection algorithm using Directed resource graphs. Implement
the algorithm on the Directed resource graph below and determine the deadlocked
processes if any.
Deadlocks can be detected by building directed resource graphs and looking for cycles.
Algorithm used to detect circularity can be executed whenever it is appropriate.
4) Name and explain one method in which synchronisation can be achieved between
processes.
A. Producers and Consumers algorithm: One process produces data that another process
consumes. Requires counting semaphores to track the number of full and empty positions
in the buffer. Requires a binary semaphore (mutex) to achieve mutual exclusion when the
process enters its critical region, i.e. where data is written to or read from the buffer.
B. Readers and Writers algorithm: There is a data area (e.g. a file) shared among a
number of processes. There could be a number of processes that only read the data area
(readers) and a number that only write to the data area (writers). The conditions that must
be satisfied are:
1) Any number of readers can simultaneously read the file
2) Only one writer at a time can write to the file
3) If a writer is writing to a file, no reader can read it
To prevent either type of starvation, two semaphores are used to ensure mutual exclusion
between readers and writers. A resource can be given to all readers provided that no
writers are processing (W2=0). A resource can be given to a writer provided that no
readers are reading (R2=0) and no writers are writing (W2=0).
If “s” is a semaphore variable then s = 0 implies a busy critical region and the process
calling the P operation must wait until s > 0.
P(s): If s > 0 then s = s – 1 (test, fetch, decrement, and store sequence) {Critical section}.
V(s): s = s + 1 (fetch, increment, and store sequence).
Choice of which of the waiting jobs will be processed next depends on the algorithm used
by this portion of the Process Scheduler.