SlideShare a Scribd company logo
7C H A P T E R
Deadlocks
Practice Exercises
7.1 List three examples of deadlocks that are not related to a computer-
system environment.
Answer:
• Two cars crossing a single-lane bridge from opposite directions.
• A person going down a ladder while another person is climbing up
the ladder.
• Two trains traveling toward each other on the same track.
7.2 Suppose that a system is in an unsafe state. Show that it is possible for
the processes to complete their execution without entering a deadlock
state.
Answer: An unsafe state may not necessarily lead to deadlock, it just
means that we cannot guarantee that deadlock will not occur. Thus, it
is possible that a system in an unsafe state may still allow all processes
to complete without deadlock occurring. Consider the situation where
a system has 12 resources allocated among processes P0, P1, and P2. The
resources are allocated according to the following policy:
Max Current Need
P0 10 5 5
P1 4 2 2
P2 9 3 6
Currently there are two resources available. This system is in an un-
safe state as process P1 could complete, thereby freeing a total of four
resources. But we cannot guarantee that processes P0 and P2 can com-
plete. However, it is possible that a process may release resources before
requesting any further. For example, process P2 could release a resource,
thereby increasing the total number of resources to five. This allows pro-
cess P0 to complete, which would free a total of nine resources, thereby
allowing process P2 to complete as well.
21
22 Chapter 7 Deadlocks
for (int i = 0; i < n; i++) {
// first find a thread that can finish
for (int j = 0; j < n; j++) {
if (!finish[j]) {
boolean temp = true;
for (int k = 0; k < m; k++) {
if (need[j][k] > work[k])
temp = false;
}
if (temp) { // if this thread can finish
finish[j] = true;
for (int x = 0; x < m; x++)
work[x] += work[j][x];
}
}
}
}
Figure 7.1 Banker’s algorithm safety algorithm.
7.3 A possible method for preventing deadlocks is to have a single, higher-
order resource that must be requested before any other resource. For
example, if multiple threads attempt to access the synchronization ob-
jects A· · · E, deadlock is possible. (Such synchronization objects may
include mutexes, semaphores, condition variables, and the like.) We can
prevent the deadlock by adding a sixth object F. Whenever a thread
wants to acquire the synchronization lock for any object A· · · E, it must
first acquire the lock for object F. This solution is known as containment:
the locks for objects A · · · E are contained within the lock for object F.
Compare this scheme with the circular-wait scheme of Section 7.4.4.
Answer: This is probably not a good solution because it yields too
large a scope. It is better to define a locking policy with as narrow a
scope as possible.
7.4 Prove that the safety algorithm presented in Section 7.5.3 requires an
order of m × n2
operations.
Figure 7.1 provides Java code that implement the safety algorithm of
the banker’s algorithm (the complete implementation of the banker’s
algorithm is available with the source code download).
Answer: As can be seen, the nested outer loops—both of which loop
through n times—provide the n2
performance. Within these outer loops
are two sequential inner loops which loop m times. The big-oh of this
algorithm is therefore O(m × n2
).
7.5 Consider a computer system that runs 5,000 jobs per month with no
deadlock-prevention or deadlock-avoidance scheme. Deadlocks occur
about twice per month, and the operator must terminate and rerun about
10 jobs per deadlock. Each job is worth about $2 (in CPU time), and the
jobs terminated tend to be about half-done when they are aborted.
A systems programmer has estimated that a deadlock-avoidance algo-
rithm (like the banker’s algorithm) could be installed in the system with
Practice Exercises 23
an increase in the average execution time per job of about 10 percent.
Since the machine currently has 30-percent idle time, all 5,000 jobs per
month could still be run, although turnaround time would increase by
about 20 percent on average.
a. What are the arguments for installing the deadlock-avoidance al-
gorithm?
b. What are the arguments against installing the deadlock-avoidance
algorithm?
Answer: An argument for installing deadlock avoidance in the system
is that we could ensure deadlock would never occur. In addition, despite
the increase in turnaround time, all 5,000 jobs could still run.
An argument against installing deadlock avoidance software is that
deadlocks occur infrequently and they cost little when they do occur.
7.6 Can a system detect that some of its processes are starving? If you answer
“yes,” explain how it can. If you answer “no,” explain how the system
can deal with the starvation problem.
Answer: Starvation is a difficult topic to define as it may mean different
things for different systems. For the purposes of this question, we will
define starvation as the situation whereby a process must wait beyond
a reasonable period of time—perhaps indefinitely—before receiving a
requested resource. One way of detecting starvation would be to first
identify a period of time—T —that is considered unreasonable. When a
process requests a resource, a timer is started. If the elapsed time exceeds
T, then the process is considered to be starved.
One strategy for dealing with starvation would be to adopt a policy
where resources are assigned only to the process that has been waiting
the longest. For example, if process Pa has been waiting longer for re-
source X than process Pb, the request from process Pb would be deferred
until process Pa ’s request has been satisfied.
Another strategy would be less strict than what was just mentioned. In
this scenario, a resource might be granted to a process that has waited less
than another process, providing that the other process is not starving.
However, if another process is considered to be starving, its request
would be satisfied first.
7.7 Consider the following resource-allocation policy. Requests for and re-
leases of resources are allowed at any time. If a request for resources
cannot be satisfied because the resources are not available, then we
check any processes that are blocked waiting for resources. If a blocked
process has the desired resources, then these resources are taken away
from it and are given to the requesting process. The vector of resources
for which the blocked process is waiting is increased to include the
resources that were taken away.
For example, consider a system with three resource types and the
vector Available initialized to (4,2,2). If process P0 asks for (2,2,1), it gets
them. If P1 asks for (1,0,1), it gets them. Then, if P0 asks for (0,0,1), it
is blocked (resource not available). If P2 now asks for (2,0,0), it gets the
available one (1,0,0) and one that was allocated to P0 (since P0 is blocked).
24 Chapter 7 Deadlocks
P0’s Allocation vector goes down to (1,2,1), and its Need vector goes up
to (1,0,1).
a. Can deadlock occur? If you answer “yes,” give an example. If you
answer “no,” specify which necessary condition cannot occur.
b. Can indefinite blocking occur? Explain your answer.
Answer:
a. Deadlock cannot occur because preemption exists.
b. Yes. A process may never acquire all the resources it needs if they
are continuously preempted by a series of requests such as those
of process C.
7.8 Suppose that you have coded the deadlock-avoidance safety algorithm
and now have been asked to implement the deadlock-detection algo-
rithm. Can you do so by simply using the safety algorithm code and
redefining Max[i] = Waiting[i] + Allocation[i], where Waiting[i] is a vector
specifying the resources for which process i is waiting and Allocation[i]
is as defined in Section 7.5? Explain your answer.
Answer: Yes. The Max vector represents the maximum request a
process may make. When calculating the safety algorithm we use the
Need matrix, which represents Max — Allocation. Another way to think
of this is Max = Need + Allocation. According to the question, the Waiting
matrix fulfills a role similar to the Need matrix, therefore Max = Waiting
+ Allocation.
7.9 Is it possible to have a deadlock involving only one single process?
Explain your answer.
Answer: No. This follows directly from the hold-and-wait condition.

More Related Content

What's hot

Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
Midhun Sankar
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
Sonali Chauhan
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
Wayne Jones Jnr
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
TamannaSharma70
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
C.U
 
Deadlock
DeadlockDeadlock
Deadlock
Abhinaw Rai
 
9 deadlock
9 deadlock9 deadlock
9 deadlock
GRajendra
 
Operating System
Operating SystemOperating System
Operating System
Subhasis Dash
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
Nivedita Kasturi
 
Deadlock
DeadlockDeadlock
Ch8
Ch8Ch8
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
DHIVYADEVAKI
 
Deadlock
DeadlockDeadlock
Deadlock
Mohd Arif
 
OS_Ch8
OS_Ch8OS_Ch8
Deadlock
DeadlockDeadlock
Deadlock
VISHAL DONGA
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
Sakshi Tiwari
 
Deadlock
DeadlockDeadlock
Deadlock
Mayuri Verma
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
Rai University
 
Ice
IceIce

What's hot (20)

Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
Deadlock
DeadlockDeadlock
Deadlock
 
9 deadlock
9 deadlock9 deadlock
9 deadlock
 
Operating System
Operating SystemOperating System
Operating System
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Ch8
Ch8Ch8
Ch8
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
Deadlock
DeadlockDeadlock
Deadlock
 
OS_Ch8
OS_Ch8OS_Ch8
OS_Ch8
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
 
Ice
IceIce
Ice
 

Similar to 7 web

Dead Lock
Dead LockDead Lock
Dead Lock
Ramasubbu .P
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
Dilshan Sudaraka
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
amadayshwan
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
jbri1395
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
sangrampatil81
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
wahab13
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
MogilicharlaPavanKal
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
ImranKhan880955
 
Deadlocks prefinal
Deadlocks prefinalDeadlocks prefinal
Deadlocks prefinal
marangburu42
 
Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456
marangburu42
 
Deadlocks final
Deadlocks finalDeadlocks final
Deadlocks final
marangburu42
 
Deadlocks.ppt
Deadlocks.pptDeadlocks.ppt
Deadlocks.ppt
jamilmalik19
 
Deadlock
DeadlockDeadlock
Deadlock
Mahershi ACT
 
Chapter 5(five).pdf
Chapter 5(five).pdfChapter 5(five).pdf
Chapter 5(five).pdf
amanuel236786
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
marangburu42
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
KAnurag2
 
Ch8: Deadlocks
Ch8: DeadlocksCh8: Deadlocks
Ch8: Deadlocks
Ahmar Hashmi
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
ssusere16bd9
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
sheraz7288
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
madhuributani
 

Similar to 7 web (20)

Dead Lock
Dead LockDead Lock
Dead Lock
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Deadlocks prefinal
Deadlocks prefinalDeadlocks prefinal
Deadlocks prefinal
 
Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456Deadlocksprefinal 161014115456
Deadlocksprefinal 161014115456
 
Deadlocks final
Deadlocks finalDeadlocks final
Deadlocks final
 
Deadlocks.ppt
Deadlocks.pptDeadlocks.ppt
Deadlocks.ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Chapter 5(five).pdf
Chapter 5(five).pdfChapter 5(five).pdf
Chapter 5(five).pdf
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Ch8: Deadlocks
Ch8: DeadlocksCh8: Deadlocks
Ch8: Deadlocks
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 

Recently uploaded

Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
Sease
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
Fwdays
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
Sunil Jagani
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
DanBrown980551
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
LizaNolte
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
AlexanderRichford
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 

Recently uploaded (20)

Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 

7 web

  • 1. 7C H A P T E R Deadlocks Practice Exercises 7.1 List three examples of deadlocks that are not related to a computer- system environment. Answer: • Two cars crossing a single-lane bridge from opposite directions. • A person going down a ladder while another person is climbing up the ladder. • Two trains traveling toward each other on the same track. 7.2 Suppose that a system is in an unsafe state. Show that it is possible for the processes to complete their execution without entering a deadlock state. Answer: An unsafe state may not necessarily lead to deadlock, it just means that we cannot guarantee that deadlock will not occur. Thus, it is possible that a system in an unsafe state may still allow all processes to complete without deadlock occurring. Consider the situation where a system has 12 resources allocated among processes P0, P1, and P2. The resources are allocated according to the following policy: Max Current Need P0 10 5 5 P1 4 2 2 P2 9 3 6 Currently there are two resources available. This system is in an un- safe state as process P1 could complete, thereby freeing a total of four resources. But we cannot guarantee that processes P0 and P2 can com- plete. However, it is possible that a process may release resources before requesting any further. For example, process P2 could release a resource, thereby increasing the total number of resources to five. This allows pro- cess P0 to complete, which would free a total of nine resources, thereby allowing process P2 to complete as well. 21
  • 2. 22 Chapter 7 Deadlocks for (int i = 0; i < n; i++) { // first find a thread that can finish for (int j = 0; j < n; j++) { if (!finish[j]) { boolean temp = true; for (int k = 0; k < m; k++) { if (need[j][k] > work[k]) temp = false; } if (temp) { // if this thread can finish finish[j] = true; for (int x = 0; x < m; x++) work[x] += work[j][x]; } } } } Figure 7.1 Banker’s algorithm safety algorithm. 7.3 A possible method for preventing deadlocks is to have a single, higher- order resource that must be requested before any other resource. For example, if multiple threads attempt to access the synchronization ob- jects A· · · E, deadlock is possible. (Such synchronization objects may include mutexes, semaphores, condition variables, and the like.) We can prevent the deadlock by adding a sixth object F. Whenever a thread wants to acquire the synchronization lock for any object A· · · E, it must first acquire the lock for object F. This solution is known as containment: the locks for objects A · · · E are contained within the lock for object F. Compare this scheme with the circular-wait scheme of Section 7.4.4. Answer: This is probably not a good solution because it yields too large a scope. It is better to define a locking policy with as narrow a scope as possible. 7.4 Prove that the safety algorithm presented in Section 7.5.3 requires an order of m × n2 operations. Figure 7.1 provides Java code that implement the safety algorithm of the banker’s algorithm (the complete implementation of the banker’s algorithm is available with the source code download). Answer: As can be seen, the nested outer loops—both of which loop through n times—provide the n2 performance. Within these outer loops are two sequential inner loops which loop m times. The big-oh of this algorithm is therefore O(m × n2 ). 7.5 Consider a computer system that runs 5,000 jobs per month with no deadlock-prevention or deadlock-avoidance scheme. Deadlocks occur about twice per month, and the operator must terminate and rerun about 10 jobs per deadlock. Each job is worth about $2 (in CPU time), and the jobs terminated tend to be about half-done when they are aborted. A systems programmer has estimated that a deadlock-avoidance algo- rithm (like the banker’s algorithm) could be installed in the system with
  • 3. Practice Exercises 23 an increase in the average execution time per job of about 10 percent. Since the machine currently has 30-percent idle time, all 5,000 jobs per month could still be run, although turnaround time would increase by about 20 percent on average. a. What are the arguments for installing the deadlock-avoidance al- gorithm? b. What are the arguments against installing the deadlock-avoidance algorithm? Answer: An argument for installing deadlock avoidance in the system is that we could ensure deadlock would never occur. In addition, despite the increase in turnaround time, all 5,000 jobs could still run. An argument against installing deadlock avoidance software is that deadlocks occur infrequently and they cost little when they do occur. 7.6 Can a system detect that some of its processes are starving? If you answer “yes,” explain how it can. If you answer “no,” explain how the system can deal with the starvation problem. Answer: Starvation is a difficult topic to define as it may mean different things for different systems. For the purposes of this question, we will define starvation as the situation whereby a process must wait beyond a reasonable period of time—perhaps indefinitely—before receiving a requested resource. One way of detecting starvation would be to first identify a period of time—T —that is considered unreasonable. When a process requests a resource, a timer is started. If the elapsed time exceeds T, then the process is considered to be starved. One strategy for dealing with starvation would be to adopt a policy where resources are assigned only to the process that has been waiting the longest. For example, if process Pa has been waiting longer for re- source X than process Pb, the request from process Pb would be deferred until process Pa ’s request has been satisfied. Another strategy would be less strict than what was just mentioned. In this scenario, a resource might be granted to a process that has waited less than another process, providing that the other process is not starving. However, if another process is considered to be starving, its request would be satisfied first. 7.7 Consider the following resource-allocation policy. Requests for and re- leases of resources are allowed at any time. If a request for resources cannot be satisfied because the resources are not available, then we check any processes that are blocked waiting for resources. If a blocked process has the desired resources, then these resources are taken away from it and are given to the requesting process. The vector of resources for which the blocked process is waiting is increased to include the resources that were taken away. For example, consider a system with three resource types and the vector Available initialized to (4,2,2). If process P0 asks for (2,2,1), it gets them. If P1 asks for (1,0,1), it gets them. Then, if P0 asks for (0,0,1), it is blocked (resource not available). If P2 now asks for (2,0,0), it gets the available one (1,0,0) and one that was allocated to P0 (since P0 is blocked).
  • 4. 24 Chapter 7 Deadlocks P0’s Allocation vector goes down to (1,2,1), and its Need vector goes up to (1,0,1). a. Can deadlock occur? If you answer “yes,” give an example. If you answer “no,” specify which necessary condition cannot occur. b. Can indefinite blocking occur? Explain your answer. Answer: a. Deadlock cannot occur because preemption exists. b. Yes. A process may never acquire all the resources it needs if they are continuously preempted by a series of requests such as those of process C. 7.8 Suppose that you have coded the deadlock-avoidance safety algorithm and now have been asked to implement the deadlock-detection algo- rithm. Can you do so by simply using the safety algorithm code and redefining Max[i] = Waiting[i] + Allocation[i], where Waiting[i] is a vector specifying the resources for which process i is waiting and Allocation[i] is as defined in Section 7.5? Explain your answer. Answer: Yes. The Max vector represents the maximum request a process may make. When calculating the safety algorithm we use the Need matrix, which represents Max — Allocation. Another way to think of this is Max = Need + Allocation. According to the question, the Waiting matrix fulfills a role similar to the Need matrix, therefore Max = Waiting + Allocation. 7.9 Is it possible to have a deadlock involving only one single process? Explain your answer. Answer: No. This follows directly from the hold-and-wait condition.