Vous êtes sur la page 1sur 16

1 | Page

OPERATING SYSTEMS
UNIT-III

1. What is dead lock? Explain with an example.


In a multiprogramming environment, several processes may
compete for a finite number of resources. A process requests resources;
if the resources are not available at that time, the process enters a wait
state. It may happen that waiting processes will never again change state,
because the resources they have requested are held by other waiting
processes. This situation is called a deadlock.
2. What is race condition? Explain briefly.
A situation where several processes access and manipulate the same data
concurrently and the outcome of the execution depend on the particular
order in which the access takes place, is called a race condition.
To guard against race condition we need to ensure that only one
process at a time can be manipulating the shared data or variable. To
make such a guarantee, we require some form of synchronization of the
processes.
3. Explain Resource-Allocation graph.
Deadlocks can be described more precisely in terms of a directed graph
called a system resource-allocation graph. This graph consists of a set of
vertices V and a set of edges . The set of vertices V is partitioned into
two different types of nodes P = {P1, P2, ..., Pn}, the set consisting of all
the active processes in the system, and R = {R1, R2, ..., Rm}, the set
consisting of all resource types in the system.
A directed edge from process Pi to resource type Rj is denoted by
P; Rj it signifies that process Pi, requested an instance of resource type
Rj and is currently waiting for that resource. A directed edge from
resource type Rj to process Pi is denoted by RjPi; it signifies that an
instance of resource type Rj has been allocated to process Pi. A directed
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

2 | Page

edge PiRj is called, a request edge; a directed edge RjPi is called an


assignment edge.
When process Pi, requests an instance of resource type Rj, a request edge
is inserted in the resource-allocation graph. When this request can be
fulfilled, the request edge is instantaneously transformed to an
assignment edge. When the process no longer needs access to the
resource it releases the resource, and as a result the assignment edge is
deleted.
The resource-allocation graph depicts the following situation:
The sets P, R and E
o P = {P1, P2, P3}
o R = {R1, R2, R3, R4}
o E = {P1R1, P2R3, R1P2, R2P2, R2P1, R3P3}
Resource Instances
o One instance of resource type R1
o Two instances of resource type R2
o One instance of resource type R3
o Three instances of resource type R4

Process states:
o Process P1 is holding an instance of resource type R2, and is
waiting for an instance of resource type R1.
o Process P2 is holding an instance of R1 and R2, and is waiting
for an instance of resource type R3.
o Process P3 is holding an instance of R3.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

3 | Page

4. Explain how we can prevent deadlocks.


A deadlock to occur, each of the four necessary conditions must
hold. By ensuring that at least one of these conditions cannot hold, we
can prevent the occurrence of a deadlock.
Mutual Exclusion:
The mutual-exclusion condition must hold for non sharable
resources. Sharable resources, on the other hand, do not require
mutually exclusive access. Read-only files are a good example of a
sharable resource. A process never needs to wait for sharable
resource. However we cannot prevent deadlocks by denying the
mutual exclusive condition.
Hold and wait:
To ensure that the hold-and-wait condition never occurs in the
system, we must guarantee that, whenever a process requests a
resource, it does not hold any other resources. One protocol that can
be used requires each process to request and be allocated all its
resources before it begins execution.
An alternative protocol allows a process to request resources only
when the process has none. Before it can request any additional
resources it must release all the resources that are currently allocated.
These protocols have two main disadvantages. First, resource
utilization may be low, since many of the resources may be allocated
but unused for a long period. Second, starvation is possible. A
process that needs several popular resources may need to wait
indefinitely, because at least one of the resources that that it needs is
always allocated to some other process.
No preemption:
If a process is holding some resources and requests another
resource that cannot be immediately allocated to it then all resources
currently being held are preempted. The preempted resources that are
added to the list of resources for which the process is waiting. The
process will be restarted only when it can regain its old resources, as
well as that it is requesting.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

4 | Page

If a process requests some resources, we check if it is available. If


they are we allocate them. If not we check if it is allocated to some
other process that is waiting for additional resources. If so we
preempt the desired resources from the waiting process and allocate
them to the requesting process. If the resource is neither available nor
allocated to some other process that is waiting for additional
resources, the requesting process must wait. While it is waiting its
some resources can be preempted if another process requests it.
Circular Wait:
One way to ensure that the circular-wait condition never
holds is to impose a total ordering of all resource types, and to
require that each process requests resources in an increasing order
of enumeration.
Let R = {R1, R2,.Rm} be the set of resource types. We
assign to each resource type a unique integer number, which
allows us to compare two resources and to determine whether one
precedes another in our ordering. Formally, we define a one-to-one
function F: R > N, where N is the set of natural numbers. For
example, if the set of resource types R includes tape drives, disk
drives, and printers, then the function F might be defined as
follows:
F(tape drive) = 1,
F(disk drive) = 5,
F(Printer) = 12,
Consider the protocol to prevent deadlock. Each process can
request resources only in an increasing order of enumeration. That is,
a process can initially request any number of instances of resource
type say Ri. After that, the process can request instances of resource
type Rj, if and only if F(Rj)>=F(Ri). Alternatively we can require
that, whenever a process requests an instance of resource type Rj, it
has released any resources I such that F(Ri)>=F)Rj). If these two
protocols are used, then the circular wait condition cannot hold.

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

5 | Page

5. What is critical section problem? What are the requirements for


solutions to critical section problem and also explain the two process
solutions.
Critical section: a segment of code of a process, in which the
process may be changing common variables, updating a table, writing a
file, and so on, that segment of a process is called a critical section or
critical region.
A solution to critical section problem must satisfy the following three
requirements:
Mutual Exclusion: if process Pi is executing in its no other
process can be executing in their critical sections.
Progress: If no process is executing in its critical section and there
exist some process that wish to enter their critical section, then
only those processes that are not executing in their remainder
section can participate in the decision of which will enter its
critical section next, and this selection cannot be postponed
indefinitely.
Bounded waiting: There exists a bound on the number of time
that other processes are allowed to enter their critical sections after
a process has made a request to enter its critical section and before
that request is granted.

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

6 | Page

There are two process solutions to critical section problem that satisfy
these three requirements. The processes are numbered P0 and P1.
Algorithm 1:One first approach is to let the process share a common integer
variable turn initialized to 0. If turn=0, then process P0 is allowed
to execute in its critical section. The structure of process P0 is
shown below:

This solution satisfies mutual exclusion, but not progress


Algorithm 2:The problem with algorithm is that it does not retain sufficient
information about the state of each process; it remembers only
which process is allowed to enter that process critical section to
remedy this problem, we can replace variable with the following
array;
var flag : array[01] of boolean;
The elements of the array are initialized to false. If flag[0] is
true, this value indicates that P0 is ready to enter critical section.
The structure of process P0 is shown below:

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

7 | Page

In this algorithm, process


P0 first sets flag [0] to be true, signaling that it is ready to enter its
critical section.
Then, P0 checks to verify that process P1 is not also ready to enter
its critical section.
If P1 were ready, then P0 would wait until P1 had indicated that it
no longer needed to be in the critical section (that is, until flag[1]
was false).
At this point, P0 would enter the critical section.
On exiting the critical section, P0 would set its flag to be false,
allowing the other process (if it is waiting) to enter its critical
section.
This solution satisfies mutual exclusion, but not progress
requirement.
6. What is a semaphore? What are its operations? List different
types of semaphores and its uses.
To overcome critical section problem we can use a synchronization
tool called semaphore.
Definition: A semaphore is a protected integer variable, apart from
initialization, is accessed two standard atomic operations: wait and
signal. The classic definitions of wait and signal are
wait(S): while S<0
;
//do no-op
S:S-1;
signal(S):
S :=S + 1;
Types of Semaphores:
Counting Semaphore: Counting semaphore is a semaphore where
its integer value can range over an unrestricted domain.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

8 | Page

Binary Semaphore: A binary semaphore is a semaphore with an


integer value that can range only between 0 and 1. A binary
semaphore is simpler to implement than a counting semaphore.
7. Explain Readers and Writers problem.
A data object (such as a file or record) is to be shared among
several concurrent processes. Some of these processes may want only to
read the content of the shared object, whereas others may want to update
(that is, to read and write) the shared object.
We distinguish between these two types of processes by referring
to those processes that are interested in only reading as readers, and to
the rest as writers. Obviously, if two readers access the shared data
object simultaneously, no adverse effects will result.
However, if a writer and some other process access the shared
object simultaneously, confusion may result.
To ensure that these difficulties do not arise, we require that the
writers have exclusive access to the shared object. This synchronization
problem is referred to as the readers-writers problem.
In the readers writers problem no reader will wait for other readers
to finish reading simply because a writer is waiting.
In the solution to readers-writers problem, the reader processes
share the following data structures:
var mutex, wrt: semaphore;
readcount: integer;
The semaphores mutex and wrt are initialized to 1;
Readcount is initialized to 0.
The semaphore wrt is common to both the reader and writer
processes,
The mutex semaphore is used to ensure mutual exclusion
when the variable readcount is updated.
Readcount keeps track of how many processes are currently
reading the object.
The semaphore wrt functions exclusion semaphore for the
writers. It also is used by the first or last reader that enter or
exits the critical section.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

9 | Page

It is not used by users who enter or exit while other readers


are in their critical sections.
If a writer is in the critical section and n readers are waiting then
one reader is queued on wrt, and n-1 readers are queued on
mutex.
When writer executes signal(wrt), we may resume the
execution of either the writing reader or a single waiting writer.
The selection is made by the scheduler.
wait(wrt);

writing is performed

signal(wrt);
The structure of writer process
wait(mutex);
readcount:=readcount + 1;
if readcount =1 then wait(wrt);
signal(mutex);

reading is performed

wait(mutex);
readcount:= readcount - 1;
if readcount=0 then signal(wrt);
signal(mutex);
The structure of reader process
8. Explain briefly how we can recover from deadlocks.
When a deadlock is determined in a system by detection algorithm,
the operator be informed to deal with deadlock manually. The other
option is to let system recover from deadlock automatically. There are
two options for breaking the deadlock. One is simply to abort one or
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

10 | P a g e

more processes. The second option is to preempt some resources from


one or more of the deadlocked processes.
Process Termination
To eliminate deadlocks by aborting a process, we use one of two
methods.
Abort all deadlocked processes: This method clearly will break
the deadlock cycle, but at a great expense, these processes may
have computed for a long time. All the processes must be started
once again.
Abort one process at a time until the deadlock cycle is
eliminated: This method incurs considerable overhead, since, after
each process is aborted, a deadlock-detection algorithm must be
invoked to determine whether the deadlock still exists or not.
Resource Preemption
To eliminate deadlocks using resource preemption, we
successively preempt some resources from processes and give these
resources to other processes until the deadlock cycle is broken.
1. Selecting a victim: Here we must determine the order of
preemption to minimize cost. Cost factors may include such
parameters as the number of resources a deadlock process is
holding, and the amount of time a deadlocked process has thus far
consumed during its execution.
2. Rollback: If we preempt a resource from a process, it cannot
continue with its normal execution; it is missing some needed
resource. We must roll back the process to some safe state, and
restart it from that state. Since it is difficult to determine what a
safe state is, the simplest solution is total rollback: Abort a process
and then restart it. It is more to rollback the process only as far as
necessary to break the deadlock. On the other hand this method
requires to keep more information about the state of all running
processes.

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

11 | P a g e

3. Starvation: We must ensure that resources will not always be


preempted from the same process. If every time the resource is
preempted from same process then it will never complete its task,
hence this should not happen. Clearly we must ensure that process
can be picked as a victim only a finite number of times.
9. Briefly explain the characterization of deadlocks.
A deadlock situation can arise if the following conditions hold
simultaneously in a system.
1. Mutual exclusion: At least one resource must be held in a nonsharable node, that is only one process at a time can use the resource. If
another process requests that resource, that requesting process must be
delayed until the resource has been released.
2. Hold and Wait: There must exist a process that is holding at least one
resource and is waiting to acquire additional resources that are currently
being held by other processes.
3. No Preemptive: Resources cannot be preempted that is a resource can
be released only voluntarily by the process holding it, after that process
has completed its task.
4. Circular wait: There must exist a set of Processes. In that set of
processes P0 is waiting for a resource held by P1, p1 is waiting for a
resource that is held by P2, P2 is waiting for a resource that is held by
P3, P3 is waiting for a resource that is held by P4 and P4 is waiting for a
resource that is held by P0 process. All four conditions must hold for a
deadlock to occur.
10. Explain Dining-Philosophers Problem.
The dining-philosophers problem is considered a classic
synchronization problem, because it is an example for a large class of
concurrency-control problems. It is a simple representation of the need
to allocate several resources among several processes in a deadlock and
starvation free manner.
One simple solution is to represent each chopstick by a semaphore.
A philosopher tries to grab the chopstick by executing a wait operation
on that semaphore; she releases her chopsticks by executing the signal
operation on the appropriate semaphores. Thus, the shared data are
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

12 | P a g e

var chopstick: array [0..4] of semaphore;


where all the elements of chopstick are initialized to 1.

repeat
wait (chopstick[i]);
wait (chopstick[(i+1)%5)

eat

signal (chopstick[i]);
signal (chopstick[(i+1)%5]);

think

until false;
Although this solution guarantees that no two neighbors are eating
simultaneously, it nevertheless must be rejected because it has the
possibility of creating a deadlock. Suppose that all five philosophers
become hungry simultaneously, and each grabs her left chopstick. All
the elements of chopstick will now be equal to 0. When each philosopher
tries to grab her right chopstick, she will be delayed forever.
Several possible remedies to the deadlock problem are listed below: Allow at most four philosophers to be sitting simultaneously at the
table.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

13 | P a g e

Allow a philosopher to pick up her chopsticks only if both


chopsticks are available (note that she must pick them up in a
critical section).
Use an asymmetric solution; that is, an odd philosopher picks up
first her left chopstick and then her right chopstick, whereas an
even philosopher picks up her right chopstick and then her left
chopstick.
A deadlock-free solution does not necessarily eliminate the
possibility of starvation.
11. a) Explain the data structures involved in Bankers Algorithm.
b) Consider the following snapshot of a system:

Answer the following question using the Bankers Algorithm.


1) What is the content of matrix Need?
2) Is the system in a safe state?
3) If request (0, 4, 2, 0) from process P1 arrives, can the request
be granted immediately?
a) Several data structures must be maintained to implement the bankers
algorithm n be the number of processes in the system and m be the
number of resource types need the following data structures.
1. Available: Vector of length m. If available [j] = k, there are k
instances of resource type Rj available.
2. Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj.
OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

14 | P a g e

3. Allocation: n x m matrix. If Allocation[i,j] = k then Pi is


currently allocated k instances of Rj.
4. Need: n x m matrix. If Need[i,j] = k, then Pi may need k
more instances of Rj to complete its task.
Need [i,j] = Max[i,j] Allocation [i,j].
We can treat each row in the matrices Allocation and Need as vectors
and refer to them as Allocationi and Needi rspectively.
b)
1) Need

2) Is the system in safe state or not?

Safety Sequence : P0 can be relapsed after completed since its need is (0, 0, 0, 0)

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

15 | P a g e

So,

(add allocation of P0 with Available resources)

Now, the safety sequence = < P0 >


P2 can be completed. Next as the need is less than the available
resources. After completion of P2, release all the resources of P2
and add them to available
So

Safety sequence = < p0, p2 >

Next P3 can be completed

So

Safety sequence = < p0, p2 >

Next P4 can be completed

So

Safety sequence = < p0, p2, p3, p4 >

Finally, P1 can be completed < p0, p2, p3, p4, p1 >


So, the system is in safety state.
3) If request (0, 4, 2, 0) from process P1 arrives, can the request be
granted immediately?
Add the request of P1 to max. Resources that can be requested by P1

Clearly as P0 Need = (0, 0, 0, 0)


It can be implemented and all the resources allocated to P0 will be
released and added to available resources.

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

16 | P a g e

So

the safety sequence = < p0 >

Next P2 can be complete

So

Safety sequence = < p0, p2 >

Next P3 can be complete

So

Safety sequence = < p0, p2, p3 >

Next P4 can be complete

So

Safety sequence = < p0, p2, p3, p4 > Next P1 can complete

So, the safety sequence = < p0, p2, p3, p4, p1 >
So, the request made by P1 (0, 4, 2, 0) can be granted
immediately as the system is still in safe state
12. Explain wait-for-graph with example.
If all resources have only a single instance, then we can define a
deadlock J detection algorithm that uses a variant of the resource-allocation
graph, called J a wait-for graph. We obtain this graph from the resourceallocation graph by removing the nodes of type resource and collapsing the
appropriate edges.
An edge from P1 to Pj in a wait-for-graph implies that process Pi is
waiting for process Pj to release a resource that Pi needs. An edge Pi Pj
exists in a wait-for-graph if and only if the corresponding resource-allocation
graph contains two edges Pi Rq and Rq Pj for some resource Rq
A deadlock exists in a system if and only if the wait-for-graph contains a
cycle.

OPERATING SYSTEMS

NOMAN MOHAMED HANIF III SEMESTER (B.C.A.)

Vous aimerez peut-être aussi