0% found this document useful (0 votes)
51 views3 pages

Tutorial 3 Solutions

The document discusses deadlocks and methods to avoid them. It describes Dijkstra's Banker's Algorithm which regulates resource allocation to prevent deadlocks. The document also provides an example of detecting deadlocks using a directed resource graph and identifies two potential deadlock cycles. Finally, it explains semaphores and their use for synchronization between processes through operations like P and V.

Uploaded by

Byron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Tutorial 3 Solutions

The document discusses deadlocks and methods to avoid them. It describes Dijkstra's Banker's Algorithm which regulates resource allocation to prevent deadlocks. The document also provides an example of detecting deadlocks using a directed resource graph and identifies two potential deadlock cycles. Finally, it explains semaphores and their use for synchronization between processes through operations like P and V.

Uploaded by

Byron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 3 – Deadlocks (Solutions)

Answer the following questions:

1) Describe in detail how deadlocks can be avoided in a system.

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.

Dijkstra’s Banker’s Algorithm (1965) regulates resource allocation to avoid deadlock


- No customer granted loan exceeding bank’s total capital
- All customers given a maximum credit limit
- No customer allowed to borrow over the limit
- The sum of all loans won’t exceed bank’s total capital

To avoid deadlock, OS must:


o Never satisfy a request that moves it from a safe state to an unsafe one
o Identify the job with the smallest number of remaining resources
o Ensure that the number of available resources is always equal to, or greater than, the
number needed for the selected job to run to completion

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.

The system is now in a safe state.

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.

The detection algorithm:


1. Remove a process that is currently using a resource and not waiting for one
2. Remove a process that’s waiting only for resource classes that aren’t fully allocated
3. Go back to Step 1 and continue with Steps 1 and 2 until all connecting lines have been
removed.

Two deadlock cycles can form: P3, R6, P4, R5, P3 or


P3, R6, P4, R7, P3

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

There are two solutions:


- Readers have priority. Readers are kept waiting only if a writer is modifying the data.
This policy results in writer starvation if there is a continuous stream of readers.
- Writers have priority. As soon as a writer arrives, any readers that are already active are
allowed to finish processing, but all new readers are put in hold until the writer has finished
modifying the data. This policy results in reader starvation if there is a continuous stream
of writers.

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).

5) Describe the operation of Semaphores.

– A semaphore is a general format of a mechanism used to provide control of access to a


resource or a critical part of code through synchronization in parallel programming
environment.
– A semaphore is a flag of a non-negative integer variable that’s used to control if and
when a resource is free and can be accessed by a process in parallel programming.
– An indivisible machine instruction executed in a single machine cycle to see if the key is
available and, if it is, sets it to unavailable.
– A binary semaphore is a semaphore which limits access to a critical section at once to
one process only.
– A counting semaphore may allow more than one process access to a critical section at
once.

Value of semaphores can only be changed by the operations:


1. Set the initial value of the semaphore (number of concurrent accesses allowed).
2. P (proberen means to test).
3. V (verhogen means to increment).

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.

You might also like