You are on page 1of 23

Topic 2 Part 5

MEMORY AND PROCESS


MANAGEMENT

DEADLOCK SITUATION IN AN OPERATING


SYSTEM
Learning Outcome
 State the definition of threads
 Describe the benefits of multithreading
 Describe threads relationship to
 Define deadlock
 Describe four necessary conditions for a

deadlock to occur
 Identify the methods for handling deadlock
Thread
 A thread is a basic unit of
CPU utilization. It comprises
a thread ID, a program
counter, a register set, and a
stack

 A thread is a subdivision of a
process

 Each process has its own


memory space

 Threads of a process share


the memory space of its
parent process
Multi-Threading
 A traditional process has a single thread of control. If a
process has multiple threads of control, it can perform
more than one task at a time.

 A word processor may have a thread for displaying


graphics, another thread for responding to keystrokes
from the user, and a third thread for performing
spelling and grammar checking in the background.
Single vs. Multi-Threading
Benefits of Multi-Threading
Responsiveness: Resource sharing:
Allows a program to Allows an
continue running application to have
even if part of it is several different
blocked threads of activity

Economy: Threads Scalability: Threads


share the resources of may be running in
the process to which parallel on different
they belong processors.
Once upon a time…
o Boss said to secretary: For a week we will go abroad, so make arrangement.
o Secretary called Husband: For a week my boss and I will be going abroad, you look after yourself.
o Husband make call to secret lover: My wife is going abroad for a week, so lets spend the week
together.
o Secret lover make call to small boy whom she is giving private tuition: I have work for a week, so
you need not come for class.
o Small boy make call to his grandfather: Grandpa, for a week I don't have class 'coz my teacher is
busy. Lets spend the week together.
o Grandpa (he is the 1st boss) make call to his secretary: This week I am spending my time with my
grandson. We cannot attend that meeting.
o Secretary make call to her husband: This week my boss has some work, we cancelled our trip.
o Husband make call to secret lover: We cannot spend this week together, my wife has cancelled her
trip.
o Secret lover make call to small boy whom she is giving private tuition: This week we will have class
as usual.
o Small boy make call to his grandfather: Grandpa, my teacher said this week I have to attend class.
Sorry I can't give you company.
o Grandpa make call to his secretary: Don't worry this week we will attend that meeting, so make
arrangement……………………………………………………
Definition of Deadlock
“…my work is
taking ages to come “…my computer has
out of the printer” hanged up…”

“Program X is not
responding”
Deadlock !
Definition: A deadlock is a
situation in which two or
more processes are each
waiting for the other to finish,
but none ever does. none of
them can
release any
resources
none of the none of
processes them can be
can run awakened

Deadlock
FLASH BACK: System Resources
Printers, Disk Drives, Memory
Space, network connections,
CPU Cycles, and more…

Ever Wondering?
“It takes money to make money”
“You can’t get a job without
experience. You can’t get experience
without a job”
Four Necessary Conditions
For A Deadlock To Occur
a. mutual exclusion
b. hold-and-wait
c. no-preemption
d. circular-wait condition
Necessary Conditions for Deadlock to
Occur
ALL of these four MUST happen at once for a
deadlock to occur:

a. Mutual exclusion
b. Hold – and – wait
c. No – preemption
d. Circular – wait condition

Please listen carefully and understand the


four conditions. Your class activity will
follow, based on these conditions.
Mutual Exclusion
 One or more than one resource must be held by a
process in a non-sharable (exclusive) mode. It
means that particular process does not want to
share/give.
Explanation:
At least one resource must be held in a non-sharable
mode, that is, a process claims exclusive control of
the resource. If another process requests that
resource, the requesting process must be delayed
until the resource has been released.
Hold and Wait
 A processholds a resource while waiting for
another resource.

Explanation:

There must exist a process that is holding a resource


while waiting for additional resource that are
currently being held by other process.
No-Preemption
 Nobody can make a process to give up its
resource. Everybody must wait until that
process finished using the resource.

Explanation:

Resources already allocated to a process cannot be


preempted. Resource cannot be removed from the
process unless released voluntarily by the process
holding it.
Circular Wait
 The processes in the system form a circular list or
chain where each process in the list is waiting for a
resource held by the next process in the list.

Explanation:

Circular list or chain is something like this: Process


A waits for Process B waits for Process C …… waits
for Process A.
Brainwork!

Consider the traffic deadlock in this following figure


 Consider each section of the street as a resource
What is the respective deadlock condition?
section of the street that is
occupied by a vehicle cannot be
taken away from it
ANo-
preemption

each vehicle is occupying a


section of the street, and waiting
to move on to the next section B
Hold-and-wait
of the street.

only one vehicle can be on a


section of the street at a time,
two vehicles can’t share a C
Mutual
exclusion
section

each vehicle is waiting for the


next vehicle to move D
Circular wait
Summary
4 conditions that MUST happen for Deadlock to occur:

Mutual Hold and Wait No- Circular wait


exclusion Condition preemption
• One or • A process • Nobody • Processes
more than holds a else can form a
one resource make a circular list
resource while process give or chain
must be waiting for up its where each
held by a another resource, process in
process in a resource process the list is
non- cannot be waiting for
sharable preempted a resource
(exclusive) held by the
mode next process
in the list
Identify methods for
handling deadlock
• Prevention and avoidance of
deadlock
• Allow deadlock, detect
deadlock and recover
deadlock
• Ignore deadlock
• Ignore deadlock
• A system may enter a deadlocked state has no way of recognizing what Ignore
has happened
• Eventually, the system will stop functioning
• Used by most operating systems since it is cheaper
• Allow deadlock, detect deadlock and recover deadlock Allow and
• In this environment, the system provides an algorithm that examines recover
whether a deadlock has occurred and an algorithm to recover from the
deadlock
• Prevention and avoidance of deadlock Prevent
• OS can use algorithms to prevent or avoid deadlocks, ensuring that the
system will never enter a deadlock state
Deadlock Handling Methods
Group Activity
 Foreach of the deadlock handling
method, suggest an action that can be
taken to overcome traffic deadlock in
slide #13
Q&A

You might also like