CPU Scheduling
Exercises 51 request could be satisfied without causing deadlock given the current
allocation of chopsticks to philosophers.
Answer: The following rule prevents deadlock: when a philosopher makes a request for the first chopstick, do not satisfy the request only if there is no other philosopher with two chopsticks and if there is only one chopstick remaining.
7.9 Consider the same setting as the previous problem. Assume now that each philosopher requires three chopsticks to eat and that resource re-quests are still issued separately. Describe some simple rules for deter-mining whether a particular request could be satisfied without causing deadlock given the current allocation of chopsticks to philosophers.
Answer: When a philosopher makes a request for a chopstick, allocate the request if: 1) the philosopher has two chopsticks and there is at least one chopstick remaining, 2) the philosopher has one chopstick and there is at least two chopsticks remaining, 3) there is at least one chopstick remaining, and there is at least one philosopher with three chopsticks, 4) the philosopher has no chopsticks, there are two chopsticks remaining, and there is at least one other philosopher with two chopsticks assigned.
7.10 We can obtain the banker’s algorithm for a single resource type from the general banker’s algorithm simply by reducing the dimensionality of the various arrays by 1. Show through an example that the multiple-resource-type banker’s scheme cannot be implemented by individual application of the single-resource-type scheme to each resource type.
Answer: Consider a system with resources A, B, and C and processes P0, P1, P2, P3, and P4 with the following values of Allocation:
Allocation
A B C
P0 0 1 0
P1 3 0 2
P2 3 0 2
P3 2 1 1
P4 0 0 2
And the following value of Need:
Need
A B C
P0 7 4 3
P1 0 2 0
P2 6 0 0
P3 0 1 1
P4 4 3 1
If the value of Available is (2 3 0), we can see that a request from process P0for (0 2 0) cannot be satisfied as this lowers Available to (2 1 0) and no process could safely finish.
However, if we were to treat the three resources as three single-resource types of the banker’s algorithm, we get the following:
For resource A (which we have 2 available),
Allocated Need
P0 0 7
P1 3 0
P2 3 6
P3 2 0
P4 0 4
Processes could safely finish in the order of P1, P3, P4, P2, P0.
For resource B (which we now have 1 available as 2 were assumed assigned to process P0),
Allocated Need
P0 3 2
P1 0 2
P2 0 0
P3 1 1
P4 0 3
Processes could safely finish in the order of P2, P3, P1, P0, P4. And finally, for For resource C (which we have 0 available),
Allocated Need
P0 0 3
P1 2 0
P2 2 0
P3 1 1
P4 2 1
Processes could safely finish in the order of P1, P2, P0, P3, P4.
As we can see, if we use the banker’s algorithm for multiple resource types, the request for resources (0 2 0) from process P0 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.11 Consider the following snapshot of a system:
Allocation Max Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Exercises 53
7.12 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?
Answer:
a. What is the content of the matrix Need? The values of Need for processes P0through P4respectively 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 P0or P3could run. Once process P3runs, it releases its resources which allow all other existing processes to run.
c. If a request from process P1 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 P0, P2, P3, P1, and P4.
7.13 What is the optimistic assumption made in the deadlock-detection algo-rithm? How could this assumption be violated?
Answer: The optimistic assumption is that there will not be any form of circular-wait in terms of resources allocated and processes making requests for them. This assumption could be violated if a circular-wait does indeed in practice.
7.14 Write a multithreaded Java program that implements the banker’s al-gorithm discussed in Section 7.5.3. Create n threads that request and release resources from the banker. A banker will only grant the request if it leaves the system in a safe state. Ensure that access to shared data is thread-safe by employing Java thread synchronization as discussed in Section 7.8.
Answer: (Please refer to the supporting Web site for source code solu-tion.)
7.15 A single-lane bridge connects the two Vermont villages of North Tun-bridge and South TunTun-bridge. Farmers in the two villages use this Tun-bridge to deliver their produce to the neighboring town. The bridge can be-come deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are un-able to back up.) Using semaphores, design an algorithm that prevents deadlock. Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).
Answer:
semaphore ok to cross = 1;
void enter bridge() { ok to cross.wait();
}
void exit bridge() { ok to cross.signal();
}
7.16 Modify your solution to Exercise 7.15 so that it is starvation-free.
Answer:
monitor bridge {
int num waiting north = 0;
int num waiting south = 0;
int on bridge = 0;
condition ok to cross;
int prev = 0;
void enter bridge north() { num waiting north++;
while (on bridge ||
(prev == 0 && num waiting south > 0)) ok to cross.wait();
num waiting north--;
prev = 0;
}
void exit bridge north() { on bridge = 0;
ok to cross.broadcast();
}
void enter bridge south() { num waiting south++;
while (on bridge ||
(prev == 1 && num waiting north > 0)) ok to cross.wait();
num waiting south--;
prev = 1;
}
void exit bridge south() { on bridge = 0;
ok to cross.broadcast();
} }
8
C H A P T E R
Memory
Management
Although many systems are demand paged (discussed in Chapter 10), there are still many that are not, and in many cases the simpler memory management strategies may be better, especially for small dedicated systems. We want the student to learn about all of them: resident monitor, swapping, partitions, paging, and segmentation.
Exercises
8.1 Explain the difference between internal and external fragmentation.
Answer: Internal Fragmentation is the area in a region or a page that is not used by the job occupying that region or page. This space is unavailable for use by the system until that job is finished and the page or region is released.
8.2 Consider the following process for generating binaries. A compiler is used to generate the object code for individual modules, and a linkage editor is used to combine multiple object modules into a single program binary. How does the linkage editor change the binding of instructions and data to memory addresses? What information needs to be passed from the compiler to the linkage editor to facilitate the memory binding tasks of the linkage editor?
Answer: The linkage editor has to replace unresolved symbolic ad-dresses with the actual adad-dresses associated with the variables in the final program binary. In order to perform this, the modules should keep track of instructions that refer to unresolved symbols. During linking, each module is assigned a sequence of addresses in the overall program binary and when this has been performed, unresolved references to sym-bols exported by this binary could be patched in other modules since every other module would contain the list of instructions that need to be patched.
55
8.3 Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600KB(in order), how would each of the first-fit, best-fit, and worst-fit algorithms place processes of 212 KB, 417KB, 112 KB, and 426 KB (in order)? Which algorithm makes the most efficient use of memory?
Answer:
a. First-fit:
b. 212K is put in 500K partition c. 417K is put in 600K partition
d. 112K is put in 288K partition (new partition 288K = 500K - 212K) e. 426K must wait
f. Best-fit:
g. 212K is put in 300K partition h. 417K is put in 500K partition i. 112K is put in 200K partition j. 426K is put in 600K partition k. Worst-fit:
l. 212K is put in 600K partition m. 417K is put in 500K partition n. 112K is put in 388K partition o. 426K must wait
In this example, Best-fit turns out to be the best.
8.4 Most systems allow programs to allocate more memory to its address space during execution. Data allocated in the heap segments of programs is an example of such allocated memory. What is required to support dynamic memory allocation in the following schemes:
a. contiguous-memory allocation b. pure segmentation
c. pure paging Answer:
• contiguous-memory allocation: might require relocation of the entire program since there is not enough space for the program to grow its allocated memory space.
• pure segmentation: might also require relocation of the segment that needs to be extended since there is not enough space for the segment to grow its allocated memory space.
• pure paging: incremental allocation of new pages is possible in this scheme without requiring relocation of the program’s address space.
Exercises 57