540 likes | 3.81k Views
Chapter 6 Homework Solutions. 6.5, 6.6, 6.11, 6.14, 6.15, 6.17, 6.18. Problem 6.5. Consider the following snapshot of a system. There are no outstanding unsatisfied requests for resources. a. Compute what each resource might request and disp lay in the columns labeled “still needs”.
E N D
Chapter 6 Homework Solutions 6.5, 6.6, 6.11, 6.14, 6.15, 6.17, 6.18
Problem 6.5 • Consider the following snapshot of a system. There are no outstanding unsatisfied requests for resources a. Compute what each resource might request and display in the columns labeled “still needs”
Problem 6.5 • Consider the following snapshot of a system. There are no outstanding unsatisfied requests for resources a. Compute what each resource might request and display in the columns labeled “still needs”
Problem 6.5 • Consider the following snapshot of a system. There are no outstanding unsatisfied requests for resources b. Is this system currently in a safe or an unsafe state? Why? SAFE: Banker’s algorithm yields completion order p1,p4,p5,p2,p3 Thus (c) system not deadlocked; and (d) No processes are or may become deadlocked.
Problem 6.5 e. If a request from p3 arrives for (0,1,0,0), can that request be safely granted immediately?
Problem 6.5 e. If a request from p3 arrives for (0,1,0,0), can that request be safely granted immediately? Which processes, if any, are or may become deadlocked? Above chart shows state after granting p3’s request Now: p1, p4 and p5 can finish, but neither p2 nor p3’s “still needs” can be satisfied.
Problem 6.6 • Apply the deadlock algorithm to the following data and show the results. Available = (2, 1, 0, 0)Request = Allocation = 1. W = (2 1 0 0) 2. Mark P3; W = (2 1 0 0) + (0 1 2 0) = (2 2 2 0) 3. Mark P2; W = (2 2 2 0) + (2 0 0 1) = (4 2 2 1) 4. Mark P1; no deadlock detected
Problem 6.11 • Consider a system with 150 units of memory, allocated to three processes as shown: • Apply the banker’s algorithm to determine whether it would be safe to grant each of the following requests. If yes, indicate a sequence of terminations that could be guaranteed possible. If no, show the reduction of the resulting allocation table a. A fourth process arrives with a max need of 60 and initial need of 25. b. A fourth process arrives with a max need of 60 and initial need of 35.
Problem 6.11 • Consider a system with 150 units of memory, allocated to three processes as shown: • Apply the banker’s algorithm to determine whether it would be safe to grant each of the following requests. If yes, indicate a sequence of terminations that could be guaranteed possible. If no, show the reduction of the resulting allocation table. a. A fourth process arrives with a max need of 60 and initial need of 25. After creating the process, the table becomes as shown There is sufficient memory to guarantee the termination of either P1 or P2 After that, the remaining jobs can be completed in any order
Problem 6.11 • Consider a system with 150 units of memory, allocated to three processes as shown: • Apply the banker’s algorithm to determine whether it would be safe to grant each of the following requests. If yes, indicate a sequence of terminations that could be guaranteed possible. If no, show the reduction of the resulting allocation table. b. A fourth process arrives with a max need of 60 and initial need of 35. After creating the process, the table becomes as shown This is a trivially unsafe state: no process’s needs can be satisfied
Problem 6.14 a.Three processes share four resource units that can be reserved and released only one at a time. Each process needs a maximum of two units. Show that a deadlock cannot occur. Deadlock occurs if all resource units are reserved while one or more processes are waiting indefinitely for more units. But, if all 4 units are reserved, at least one process has acquired 2 units. Therefore, that process will be able to complete its work and release both units, thus enabling another process to continue.
Problem 6.14 b.N processes share M resource units that can be reserved and released only one at a time. The maximum need of each process does not exceed M and the sum of all maximum needs is less than N+M. Show that a deadlock cannot occur. Using terminology similar to that used for the banker's algorithm, define claim[i] = total amount of resource units needed by process i; allocation[i] =current number of resource units allocated to process i deficit[i] = amount of resource units still needed by i Then we have: Suppose that all resource units are reserved: From the two above equations: Therefore at least one process has deficit 0 and will be able to complete its task and release all its resources But this ensures further progress in the system. So a deadlock cannot occur.
Problem 6.15 Four processes and one resource, current claim and allocation matrices: What is the minimum number of units of the resource needed to be available for this state to be safe? • The number of available units required for the state to be safe is 3, making a total of 10 units in the system: • In the state shown in the problem, if one additional unit is available, P2 can run to completion, releasing its resources, making 2 units available. • This would allow P1 to run to completion making 3 units available. • But at this point P3 needs 6 units and P4 needs 5 units. • If to begin with, there had been 3 units available instead of 1 unit, there would now be 5 units available. • This would allow P4 to run to completion, making 7 units available, which would allow P3 to run to completion.
Problem 6.17 Comment on the following solution to the dining philosopher’s problem. A hungry philosopher first picks up his left fork; if his right fork is also available, he picks up his right fork and starts eating; otherwise, he puts down his left fork and repeats the cycle. • The philosophers can starve while repeatedly picking up and putting down their left forks in perfect unison.
6.18 Suppose there are two types of philosophers. One type always picks up his left fork first (a lefty) and the other always picks up his right fork ( a righty). a. Prove: Any seating arrangement with at least one of each avoids deadlock Assume there is a nonempty set D of philosophers such that each Pi in D holds one fork and waits for a fork held by its neighbor. Without loss of generality, assume that Pj D is a lefty. Since Pj clutches his left fork and cannot have his right fork, his right neighbor Pk never completes his dinner and is also a lefty. Therefore, Pk D. Continuing around the table, we see that all the philosophers at the table are lefties, contradicting our assumption. /* lefty: */ repeat think; wait(fork[i]); wait(fork[(i+1) mod 5]); eat(); signal(fork[(i+1) mod 5]); signal(for[i]); forever; /* righty: */ repeat think; wait(fork[(i+1) mod 5]); wait(fork[i]); eat(); signal(for[i]); signal(fork[(i+1) mod 5]); forever;
/* lefty: */ repeat think; wait(fork[i]); wait(fork[(i+1) mod 5]); eat(); signal(fork[(i+1) mod 5]); signal(for[i]); forever; /* righty: */ repeat think; wait(fork[(i+1) mod 5]); wait(fork[i]); eat(); signal(for[i]); signal(fork[(i+1) mod 5]); forever; b. Prove: Any seating arrangement with at least one of each avoids starvation Assume there is a stable pattern of dining in which lefty Pj never eats. Suppose Pj holds no fork. Then Pj's left neighbor Pi must continually hold his right fork and never finishes eating. Thus Pi is a righty holding his right fork, but never getting his left fork to complete a meal, i.e., Pi also starves. Now Pi's left neighbor must be a righty who continually holds his right fork. Proceeding leftward around the table with this argument shows that all philosophers are (starving) righties. But Pj is a lefty: a contradiction. Thus Pj must hold one fork.
/* lefty: */ repeat think; wait(fork[i]); wait(fork[(i+1) mod 5]); eat(); signal(fork[(i+1) mod 5]); signal(for[i]); forever; /* righty: */ repeat think; wait(fork[(i+1) mod 5]); wait(fork[i]); eat(); signal(for[i]); signal(fork[(i+1) mod 5]); forever; b. Prove: Any seating arrangement with at least one of each avoids starvation Thus Pj must hold one fork. As Pj continually holds one fork and waits for his right fork, Pj's right neighbor Pk never sets his left fork down and never completes a meal, i.e., Pk is also a lefty who starves. If Pk did not continually hold his left fork, Pj could eat; therefore Pk holds his left fork. Carrying the argument rightward around the table shows that all philosophers are (starving) lefties: a contradiction. Starvation is thus precluded.