Operating Systems

================ Start Lecture #7 ================

Note: The department would like to know how you feel about a recitation being added the same way as in Fundamental Algorithms and Programming Languages. It would be 1 hour per week, probably on a different evening (but surely on an evening).

3.5: Deadlock Avoidance

Let's see if we can tiptoe through the tulips and avoid deadlock states even though our system does permit all four of the necessary conditions for deadlock.

An optimistic resource manager is one that grants every request as soon as it can. To avoid deadlocks with all four conditions present, the manager must be smart not optimistic.

3.5.1 Resource Trajectories

We plot progress of each process along an axis. In the example we show, there are two processes, hence two axes, i.e., planar. This procedure assumes that we know the entire request and release pattern of the processes in advance so it is not a practical solution. I present it as it is some motivation for the practical solution that follows, the Banker's Algorithm.

Homework: 10, 11, 12.

3.5.2: Safe States

Avoiding deadlocks given some extra knowledge.

Definition: A state is safe if there is an ordering of the processes such that: if the processes are run in this order, they will all terminate (assuming none exceeds its claim).

Recall the comparison made above between detecting deadlocks (with multi-unit resources) and the banker's algorithm

In the definition of a safe state no assumption is made about the running processes; that is, for a state to be safe termination must occur no matter what the processes do (providing the all terminate and to not exceed their claims). Making no assumption is the same as making the most pessimistic assumption.

Give an example of each of the four possibilities. A state that is

  1. Safe and deadlocked--not possible.

  2. Safe and not deadlocked--trivial (e.g., no arcs).

  3. Not safe and deadlocked--easy (any deadlocked state).

  4. Not safe and not deadlocked--interesting.

Is the figure on the right safe or not?

A manager can determine if a state is safe.

The manager then follows the following procedure, which is part of Banker's Algorithms discovered by Dijkstra, to determine if the state is safe.

  1. If there are no processes remaining, the state is safe.

  2. Seek a process P whose max additional requests is less than what remains (for each resource type).
  3. The banker now pretends that P has terminated (since the banker knows that it can guarantee this will happen). Hence the banker pretends that all of P's currently held resources are returned. This makes the banker richer and hence perhaps a process that was not eligible to be chosen as P previously, can now be chosen.

  4. Repeat these steps.

Example 1

A safe state with 22 units of one resource
processinitial claimcurrent allocmax add'l
X312
Y1156
Z19109
Total16
Available6

Example 2

An unsafe state with 22 units of one resource
processinitial claimcurrent allocmax add'l
X312
Y1156
Z19127
Total18
Available4

Start with example 1 and assume that Z now requests 2 units and the manager grants them.

Remark: An unsafe state is not necessarily a deadlocked state. Indeed, if one gets lucky all processes in an unsafe state may terminate successfully. A safe state means that the manager can guarantee that no deadlock will occur.

3.5.3: The Banker's Algorithm (Dijkstra) for a Single Resource

The algorithm is simple: Stay in safe states. Initially, we assume all the processes are present before execution begins and that all initial claims are given before execution begins. We will relax these assumptions very soon.

Homework: 13.

3.5.4: The Banker's Algorithm for Multiple Resources

At a high level the algorithm is identical: Stay in safe states.

Limitations of the banker's algorithm

Homework: 21, 27, and 20. There is an interesting typo in 20: A has claimed 3 units of resource 5, but there are only 2 units in the entire system. Change the problem by having B both claim and be allocated 1 unit of resource 5.

3.7: Other Issues

3.7.1: Two-phase locking

This is covered (MUCH better) in a database text. We will skip it.

3.7.2: Non-resource deadlocks

You can get deadlock from semaphores as well as resources. This is trivial. Semaphores can be considered resources. P(S) is request S and V(S) is release S. The manager is the module implementing P and V. When the manager returns from P(S), it has granted the resource S.

3.7.3: Starvation

As usual FCFS is a good cure. Often this is done by priority aging and picking the highest priority process to get the resource. Also can periodically stop accepting new processes until all old ones get their resources.

3.8: Research on Deadlocks

Skipped.

3.9: Summary

Read.

Chapter 4: Memory Management

Also called storage management or space management.

Memory management must deal with the storage hierarchy present in modern machines.

We will see in the next few weeks that there are three independent decision:

  1. Segmentation (or no segmentation)
  2. Paging (or no paging)
  3. Fetch on demand (or no fetching on demand)

Memory management implements address translation.

Homework: 6.

When is address translation performed?

  1. At compile time
  2. At link-edit time (the “linker lab”)
  3. At load time
  4. At execution time

Extensions

  1. Dynamic Loading
  2. Dynamic Linking
Note: I will place ** before each memory management scheme.

NOTE: Lab 3 (banker's algorithm) will be assigned next week and due two NYU weeks (three calendar weeks) later.

4.1: Basic Memory Management (Without Swapping or Paging)

Entire process remains in memory from start to finish and does not move.

The sum of the memory requirements of all jobs in the system cannot exceed the size of physical memory.

** 4.1.1: Monoprogramming without swapping or paging (Single User)

The “good old days” when everything was easy.

**4.1.2: Multiprogramming with fixed partitions

Two goals of multiprogramming are to improve CPU utilization, by overlapping CPU and I/O, and to permit short jobs to finish quickly.

4.1.3: Modeling Multiprogramming (crudely)

Homework: 1, 2 (typo in book; figure 4.21 seems irrelevant).

4.1.4: Analysis of Multiprogramming System Performance

Skipped

4.1.5: Relocation and Protection

Relocation was discussed as part of linker lab and at the beginning of this chapter. When done dynamically, a simple method is to have a base register whose value is added to every address by the hardware.

Similarly a limit register is checked by the hardware to be sure that the address (before the base register is added) is not bigger than the size of the program.

The base and limit register are set by the OS when the job starts.