13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

One-shot allocation 131clever, starves to death.The problem, then, is to follow a strategy that is not so liberal that it leads todeadlock or starvation, but not so conservative that it serializes, because serialization is awaste of resources.4 ONE-SHOT ALLOCATIONTo prevent deadlock, we only need to ensure that a cycle of waiting can never build up.One way to avoid a cycle is to partition all processes into two kinds: those that have allthe resources they will ever need and those that have none at all. Processes in the firstgroup are not blocked. Processes in the second group are blocked waiting for resourcesheld by the first group. Therefore, no cycle of waiters can exist. This line of reasoningleads to one-shot allocation, which requires each process to acquire all its resources atonce. The process is blocked until the request can be satisfied. <strong>An</strong>y attempt to requestresources after the first time is an error.In its pure form, the one-shot method requires each process to acquire all itsneeded resources at one time and never request resources again. Under this rule, eachphilosopher would request both chopsticks at the outset and never return them. Only twophilosophers would ever eat; the other three would starve.A more liberal form of one-shot allocation follows this rule:One-shot allocationA process may request resources only when it has none at all.A process may repeatedly acquire a number of resources, use them, return them all, andthen later acquire a new set. To follow this policy, the philosophers should ask for bothchopsticks at once. A resource manager that employs the one-shot policy should be carefulto block any philosopher that cannot get both chopsticks at once. It must never grantrequests partially (like allocating one of the two chopsticks requested by a philosopher),because such an allocation is equivalent to letting the philosopher make two requests.Now if all the philosophers ask for chopsticks at the same time, the resourcemanager will grant one philosopher, say number 1, two chopsticks. Philosophers 2 and 5cannot be satisfied at the moment, so they are given no chopsticks at all. Philosophers 3and 4 are still candidates for resources; say the resource manager gives 3 two chopsticks.Now 4 must also be blocked. There is no deadlock, since there is no cycle in the graph,as shown in Figure 4.9. This allocation strategy has prevented deadlock, but it does notaddress starvation.Our earlier situation in which processes A and B each need two tapes is alsosolved, since the one-shot method will enforce serialization.However, one-shot allocation is too conservative to be practical. A process thatwill eventually need many resources will have to acquire them all at once and thusprevent other processes from using the resources, even though the process that holds

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!