CSE Roadmap

Core tier · about 2 hours · 8 parts

Part 4 of 8 · 2 min read

Layered systems

Think of a stack of boxes in which each box may touch only the box directly beneath it. A layered OS is built like this. Layer 0 is the hardware. The top layer is the user interface. Layer M offers a set of operations to the layer above it, and uses only the operations of the layers below it.

In plain words: A layered system builds the OS as a stack. Layer 0 is the hardware, and each layer uses only the operations of the layers directly below it. This makes each layer easy to test, because it relies only on layers already tested. It also adds a cost at each boundary and makes it hard to decide which function belongs in which layer.

   layer N   user interface
   ...
   layer 2   memory and device management
   layer 1   CPU scheduling
   layer 0   hardware

The main gain is simple construction and debugging. The first layer uses only the hardware, which is assumed correct, so it can be tested alone. Once it works, the second layer is built on it and tested, and so on. If a fault appears at layer 3, it must lie at layer 3, since the layers below are already trusted.

The costs are two. It is hard to place functions in the right layer, since a layer may need something from a layer above it (for example, the memory manager may need to wait for the disk, which in turn needs the scheduler). And every request must pass through every layer on its way down, which adds overhead. Pure layered kernels are therefore rare. Modern systems keep a loose form of the idea: a few layers with clear interfaces, but with fewer boundaries.

A worked example.

A worked count (made up to show the idea). Suppose each layer boundary adds 0.1 microsecond. A request that passes down through 6 layers and back up crosses 12 boundaries, adding 12 × 0.1 = 1.2 microseconds. A monolithic call into the same function adds near zero. If the program makes 500,000 requests a second, the layered overhead is 0.6 seconds of every second of work (500,000 × 1.2 µs), a large share of the processor.

Layered system

A layered system organises the operating system as a stack of layers, numbered from the hardware (layer 0) upward, in which each layer uses only the operations of the layers below it and offers its services to the layer above.

Example: a design where scheduling is layer 1 and memory management is layer 2, so layer 2 may call scheduling but scheduling may not call memory management.

Check yourself: 1. Which layer can be tested first, and why? 2. What makes layering hard to apply to a real kernel? 3. In the worked count, how many boundaries does a request cross going down and back up through 6 layers?