Docsity

Breif Study of Network, Study notes of Network Design

Toggle sidebar
bg1
8
C H A P T E R
Deadlocks
Practice Exercises
8.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.
8.2 Suppose that a system is in an unsafe state. Show that it is possible for
the threads to complete their execution without entering a deadlocked
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
twelve resources allocated among processes P0,P1, and P2. The resources
are allocated according to the following policy:
Max Current Need
P010 5 5
P14 2 2
P29 3 6
Currently, there are two resources available. This system is in an unsafe
state. Process P1could complete, thereby freeing a total of four resources,
but we cannot guarantee that processes P0and P2can complete. How-
ever, it is possible that a process may release resources before requesting
any further resources. For example, process P2could release a resource,
thereby increasing the total number of resources to five. This allows pro-
273
pf3
pf4
pf5

Partial preview of the text

Download Breif Study of Network and more Study notes Network Design in PDF only on Docsity!

C H A P T E R

Deadlocks

Practice Exercises

8.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. 8.2 Suppose that a system is in an unsafe state. Show that it is possible for the threads to complete their execution without entering a deadlocked 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 twelve resources allocated among processes P 0 , P 1 , and P 2. The resources are allocated according to the following policy: Max Current Need P 0 10 5 5 P 1 4 2 2 P 2 9 3 6

Currently, there are two resources available. This system is in an unsafe state. Process P 1 could complete, thereby freeing a total of four resources, but we cannot guarantee that processes P 0 and P 2 can complete. How- ever, it is possible that a process may release resources before requesting any further resources. For example, process P 2 could release a resource, thereby increasing the total number of resources to five. This allows pro-

273

274 Chapter 8 Deadlocks

cess P 0 to complete, which would free a total of nine resources, thereby allowing process P 2 to complete as well. 8.3 Consider the following snapshot of a system:

Allocation Max Available A B C D A B C D A B C D T 0 0 0 1 2 0 0 1 2 1 5 2 0 T 1 1 0 0 0 1 7 5 0 T 2 1 3 5 4 2 3 5 6 T 3 0 6 3 2 0 6 5 2 T 4 0 0 1 4 0 6 5 6

Answer the following questions using the banker’s algorithm:

a. What is the content of the matrix Need? b. Is the system in a safe state? c. If a request from thread T 1 arrives for (0,4,2,0), can the request be granted immediately?

Answer: a. The values of Need for processes P 0 through P 4 , respectively, are (0, 0, 0, 0), (0, 7, 5, 0), (1, 0, 0, 2), (0, 0, 2, 0), and (0, 6, 4, 2). b. The system is in a safe state. With Available equal to (1, 5, 2, 0), either process P 0 or P 3 could run. Once process P 3 runs, it releases its resources, which allows all other existing processes to run. c. The request can be granted immediately. The value of Available is then (1, 1, 0, 0). One ordering of processes that can finish is P 0 , P 2 , P 3 , P 1 , and P 4. 8.4 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 objects A · · · E , deadlock is possible. (Such synchronization objects may include mutexes, semaphores, condition variables, and the like.) We can prevent 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 8.5.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. The circular wait approach is a reasonable approach to avoiding deadlock, and does not increase the scope of holding a lock. 8.5 Prove that the safety algorithm presented in Section 8.6.3 requires an order of m × n^2 operations.

276 Chapter 8 Deadlocks

An argument for installing deadlock avoidance in the system is that we could ensure that 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. 8.7 Can a system detect that some of its threads 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 star- vation as the situation in which 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 pol- icy whereby resources are assigned only to the process that has been waiting the longest. For example, if process Pa has been waiting longer for resource 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. In this scenario, a resource might be granted to a process that had waited less than another process, providing that the other process was not starving. However, if another process was considered to be starving, its request would be satisfied first. 8.8 Consider the following resource-allocation policy. Requests for and releases 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 threads that are blocked waiting for resources. If a blocked thread has the desired resources, then these resources are taken away from it and are given to the requesting thread. The vector of resources for which the blocked thread is waiting is increased to include the resources that were taken away. For example, a system has three resource types, and the vector Avail- able is initialized to (4,2,2). If thread T 0 asks for (2,2,1), it gets them. If T 1 asks for (1,0,1), it gets them. Then, if T 0 asks for (0,0,1), it is blocked (resource not available). If T 2 now asks for (2,0,0), it gets the available one (1,0,0), as well as one that was allocated to T 0 (since T 0 is blocked). T 0 ’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:

Practice Exercises 277

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. 8.9 Consider the following snapshot of a system:

Allocation Max A B C D A B C D T 0 3 0 1 4 5 1 1 7 T 1 2 2 1 0 3 2 1 1 T 2 3 1 2 1 3 3 2 1 T 3 0 5 1 0 4 6 1 2 T 4 4 2 1 2 6 3 2 5

Using the banker’s algorithm, determine whether or not each of the following states is unsafe. If the state is safe, illustrate the order in which the threads may complete. Otherwise, illustrate why the state is unsafe. a. Available = (0, 3, 0, 1) b. Available = (1, 0, 0, 2)

Answer: a. Not safe. Processes P 2 , P 1 , and P 3 are able to finish, but no remain- ing processes can finish. Safe. Processes P 1 , P 2 , and P 3 are able to finish. Following this, processes P 0 and P 4 are also able to finish.

8.10 Suppose that you have coded the deadlock-avoidance safety algorithm that determines if a system is in a safe state or not, and now have been asked to implement the deadlock-detection algorithm. 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 thread i is waiting and Allocation i is as defined in Section 8.6? 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.

8.11 Is it possible to have a deadlock involving only one single-threaded process? Explain your answer.

Answer: No. This follows directly from the hold-and-wait condition.