CPE 453 Chapter 7

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

7.11 Consider the traffic deadlock depicted in Figure 7.10. a. Show that the four necessary conditions for deadlock hold in this example. b. State a simple rule for avoiding deadlocks in this system.

(a) Each cross of the streets is considered as a resource, each line of cars is considered as a process. Mutual exclution: only one line of cars at a time can use the resource. Hold and wait: Each line of cars is holding one resource and is waiting for the next resource. No preemption: the resouce can not be released until the whole line of cars have passed it. Circular wait: there are 4 lines of cars l1, l2, l3, l4, l1 is waiting for l2, l2 is waiting for l3, l3 is waiting for l4, l4 is waiting for l1. (b) There are many ways to avoid the deadlocks in this system. one way is break the second condition: a line of cars can't hold a cross and wait, it's that no car of a line can stay in the cross.

7.15 Compare the circular-wait scheme with the various deadlock-avoidance schemes (like the banker's algorithm) with respect to the following issues: a. Runtime overheads b. System throughput

A deadlock-avoidance scheme tends to increase the runtime overheads due to the cost of keep track of the current resource allocation. However, a deadlock-avoidance scheme allows for more concurrent use of resources than schemes that statically prevent the formation of deadlock. In that sense, a deadlock avoidance scheme could increase system throughput.

7.14 In Section 7.4.4, we describe a situation in which we prevent deadlock by ensuring that all locks are acquired in a certain order. However, we also point out that deadlock is possible in this situation if two threads simultaneously invoke the transaction() function. Fix the transaction() function to prevent deadlocks.

Add a new lock to this function. This third lock must be acquired before the two locks associated with the accounts are acquired. The transaction() function now appears as follows: void transaction(Account from, Account to, double amount) { Semaphore lock1, lock2, lock3; wait(lock3); lock1 = getLock(from); lock2 = getLock(to); wait(lock1); wait(lock2); withdraw(from, amount); deposit(to, amount); signal(lock3); signal(lock2); signal(lock1); }

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.

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.

7.13 The program example shown in Figure 7.4 doesn't always lead to deadlock. Describe what role the CPU scheduler plays and how it can contribute to deadlock in this program.

If thread_one is scheduled before thread_two and thread_one is able to acquire both mutex locks before thread_two is scheduled, deadlock will not occur. Deadlock can only occur if either thread_one or thread_two is able to acquire only one lock before the other thread acquires the second lock.

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

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.

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

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.12 Assume a multithreaded application uses only reader-writer locks for synchronization. Applying the four necessary conditions for deadlock, is deadlock still possible if multiple reader-writer locks are used?

YES. (1) Mutual exclusion is maintained, as they cannot be shared if there is a writer. (2) Hold-and-wait is possible, as a thread can hold one reader—writer lock while waiting to acquire another. (3) You cannot take a lock away, so no preemption is upheld. (4) A circular wait among all threads is possible.

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

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.16 In a real computer system, neither the resources available nor the demands of processes for resources are consistent over long periods (months). Resources break or are replaced, new processes come and go, and new resources are bought and added to the system. If deadlock is controlled by the banker's algorithm, which of the following changes can be made safely (without introducing the possibility of deadlock), and under what circumstances? a. Increase Available (new resources added). b. Decrease Available (resource permanently removed from system). c. Increase Max for one process (the process needs or wants more resources than allowed). d. Decrease Max for one process (the process decides it does not need that many resources). e. Increase the number of processes. f. Decrease the number of processes.

a. Increase Available (new resources added)—This could safely be changed without any problems. b. Decrease Available (resource permanently removed from system)—This could have an effect on the system and introduce the possibility of deadlock as the safety of the system assumed there were a certain number of available resources. c. Increase Max for one process (the process needs more resources than allowed, it may want more)—This could have an effect on the system and introduce the possibility of deadlock. d. Decrease Max for one process (the process decides it does not need that many resources)—This could safely be changed without any problems. e. Increase the number of processes—This could be allowed assuming that resources were allocated to the new process(es) such that the system does not enter an unsafe state. f. Decrease the number of processes—This could safely be changed without any problems.

7.3 Consider the following snapshot of a system: Allocation Max Available ABCD ABCD ABCD P0 0012 0012 1520 P1 1000 1750 P2 1354 2356 P3 0632 0652 P4 0014 0656 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 process P1 arrives for (0,4,2,0), can the request be granted immediately?

a. The values of Need for processes P0 through P4 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? Yes. With Available being equal to (1, 5, 2, 0), either process P0 or P3 could run. Once process P3 runs, it releases its resources, which allow all other existing processes to run. c. The request can be granted immediately? This results in the value of Available being (1, 1, 0, 0). One ordering of processes that can finish is P0, P2, P3, P1, and P4.

7.1 List three examples of deadlocks that are not related to a computer system environment.

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


संबंधित स्टडी सेट्स

Interpersonal Relations Ch. 9-12!

View Set

Nutrition Chapter 18: Nutrition During the Adult Years

View Set

Chapter 7 - Small Business Strategies: Imitation with a Twist

View Set

Chapter 24: The Origin Of Species

View Set

Integrated Chinese Lesson 17 Dialogue 1

View Set