HW 4 Solutions

advertisement

7. 19 Answer: Consider a system with resources A , B , and C and processes

P

0

, P

1

, P

2

, P

3

, and P

4 with the following values of Allocation :

Allocation

A B C

P

0

0 1 0

P

1

3 0 2

P

2

3 0 2

P

3

2 1 1

P

4

0 0 2 and the following value of Need :

Need

A B C

P

0

7 4 3

P

1

0 2 0

P

2

6 0 0

P

3

0 1 1

P

4

4 3 1

If the value of Available is (2 3 0), we can see that a request from process

P

0 for (0 2 0) cannot be satisfied as this lowers Available to (2 1 0) and no process could safely finish.

However, ifwe treat the three resources as three single-resource types of the banker’s algorithm, we get the following:

For resource A (of which we have 2 available),

Allocated Need

P

0

0 7

P

1

3 0

P

2

3 6

P

3

2 0

P

4

0 4

Processes could safely finish in the order P

1

, P

3

, P

4

, P

2

, P

0

.

For resource B (of which we now have 1 available as 2 were assumed assigned to process P

0

),

Allocated Need

P

0

3 2

P

1

0 2

P

2

0 0

P

3

1 1

P

4

0 3

Processes could safely finish in the order P

2

, P

3

, P

1

, P

0

, P

4

.

And finally, for For resource C (of which we have 0 available),

Allocated Need

P

0

0 3

P

1

2 0

P

2

2 0

P

3

1 1

P

4

2 1

Processes could safely finish in the order P

1

, P

2

, P

0

, P

3

, P

4

. As we can see, if we use the banker’s algorithm for multiple resource types, the request for resources (0 2 0) from process P

0 is denied as it leaves the system in an unsafe state. However, if we consider the banker’s algorithm for the three separate resources where we use a single resource type, the request is granted. Therefore, if we have multiple resource types, we must use the banker’s algorithm for multiple resource types.

7. 20 Answer: a. What is the content of the matrix Need ? 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. Is the system in a safe state? Yes. With Available being 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 allow all other existing processes to run. c. If a request from process P

1 arrives for (0,4,2,0), can the request be granted immediately? Yes, it can. This results in the value of

Available being (1, 1, 0, 0). One ordering of processes that can finish is P

0

, P

2

, P

3

, P

1

, and P

4

.

7.22

Answer: semaphore ok to cross = 1; void enter bridge() { ok to cross.wait();

} void exit bridge() { ok to cross.signal();

}

Download