ENع
←Kernel designs
Lesson 04 · The kernel and system calls

Monolithic kernels and microkernels

12 min11 steps5 questionsBeginner
About this lesson

The kernel gives programs a file system, device drivers, scheduling and memory. But which of those should run in kernel mode, with full control of the machine? A monolithic kernel runs them all there as one program; a microkernel keeps only the bare minimum and runs the rest as ordinary processes that talk by messages. Follow one read() of 5 bytes through both designs: count the crossings into the kernel, then see what a single driver bug does to each. It builds on the lessons What an operating system does, Interrupts and I/O, User mode and kernel mode, and Traps, exceptions and the interrupt table.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 4 · Interactive
01 / 11

What the kernel provides

Programs lean on the kernel for many services: a file system, which turns file names into disk blocks, drivers, the code that talks to each device, plus scheduling and memory. The big design question is which of these run in kernel mode.

services the operating system provides:
  file system    file names -> blocks on the disk
  disk driver    code that knows how to work one device
  scheduling     which program runs next
  memory         who may use which memory

kernel mode   full control, no safety net
user mode     limited, must ask the kernel

design question: how much runs in kernel mode?
Loading figure…
or ← →
Read the lesson step by step
  1. What the kernel provides

    Programs lean on the kernel for many services: a file system, which turns file names into disk blocks, drivers, the code that talks to each device, plus scheduling and memory. The big design question is which of these run in kernel mode.

    services the operating system provides:
      file system    file names -> blocks on the disk
      disk driver    code that knows how to work one device
      scheduling     which program runs next
      memory         who may use which memory
    
    kernel mode   full control, no safety net
    user mode     limited, must ask the kernel
    
    design question: how much runs in kernel mode?
  2. Monolithic: all in one

    A monolithic kernel puts every service in kernel mode, as one big program. The file system and the drivers share its memory and call each other like ordinary functions. Linux and xv6 are built this way.

    monolithic kernel = one program in kernel mode
    
    in kernel mode:  scheduling, memory, messages, file system, disk driver
    in user mode:    only your programs
    
    parts talk by    plain function calls
    examples         Linux, xv6, most Unix kernels
                     (Linux loads drivers as modules,
                      straight into kernel space)
  3. One trip into the kernel

    read() traps into the kernel: crossing 1. The file system works out which disk block holds the first 5 bytes of notes.txt and calls the disk driver directly. It is a plain function call: no crossing, no message.

    the whole program (C99, POSIX):
    
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
        int fd = open("notes.txt", O_RDONLY);
        if (fd < 0) return 1;
        char buf[5];
        ssize_t n = read(fd, buf, 5);
        if (n < 0) return 1;
        printf("%zd %.*s\n", n, (int)n, buf);
        close(fd);
        return 0;
    }
    
    line 2     n = read(fd, buf, 5);
    trap       user -> kernel          crossing 1
    fs         notes.txt, offset 0 -> disk block
    fs calls   driver_read(block)      (a function call)
  4. Back with the bytes

    The driver reads the block from the disk. The file system copies "hello" into buf, and return-from-trap takes the program back to user mode: crossing 2. read() returns 5. Two crossings in all.

    driver     reads the block from the disk
    fs         copies "hello" into buf
    return     kernel -> user          crossing 2
    read()     returns 5    (bytes read)
    
    monolithic total: 2 crossings for one read()
  5. One bug stops everything

    Now give the driver a bug: a bad pointer, an address aimed at the wrong memory. It runs in kernel mode, so nothing stops it from wrecking kernel memory. The whole kernel fails, every program stops with it, and the computer must reboot.

    driver bug   writes through a bad pointer
    mode         kernel: no check stops it
    damage       any kernel memory, any data structure
    
    result       the kernel fails
                 every program stops
                 the computer must reboot
    
    "In a monolithic kernel, a mistake is fatal" (xv6 book)
  6. Microkernel: move them out

    A microkernel keeps only what truly needs kernel mode: scheduling, memory and passing messages. The file system and the driver move out to user mode and run as ordinary processes, called servers.

    microkernel = keep the kernel small
    
    in kernel mode:  scheduling, memory, messages
    in user mode:    file system, disk driver,
                     and your programs
    
    servers          OS services running as processes
    parts talk by    messages (IPC), carried by the kernel
    examples         MINIX 3, QNX, L4 and seL4
  7. read() becomes a message

    Now read() sends a message. The program traps into the kernel (crossing 1). The kernel copies the message to the file server and switches to it, back in user mode (crossing 2).

    line 2     n = read(fd, buf, 5);
               becomes a message: "read notes.txt, 5 bytes"
    trap       app -> kernel            crossing 1
    kernel     copies the message, switches process
    return     kernel -> file server    crossing 2
    program    waits for the reply
  8. File server asks the driver

    The file server finds the disk block, but it cannot call the driver directly: the driver is another process. So it sends a second message through the kernel (crossings 3 and 4). The driver reads the block from the disk.

    file server   notes.txt, offset 0 -> disk block
    message 2     file server -> driver
                  trap in      crossing 3
                  return out   crossing 4
    driver        reads the block, using the device
                  access the kernel gave it
  9. Two replies, four crossings

    The bytes travel back as two reply messages, driver to file server and file server to program, each through the kernel. read() returns 5 again, but four messages made 8 crossings, against 2.

    message 3    driver -> file server    crossings 5, 6
    message 4    file server -> app       crossings 7, 8
    read()       returns 5, buf = "hello"
    
    monolithic   1 trip    2 crossings
    microkernel  4 trips   8 crossings
    
    each crossing and message costs time, so
    microkernels work hard on fast messages
  10. The same bug, contained

    Give the driver the same bad pointer. It is a user-mode process now, so it can only damage its own memory. The kernel ends it; the kernel and every other program keep running. MINIX 3 then starts a fresh copy of the driver.

    driver bug   writes through a bad pointer
    mode         user: the CPU refuses the access
    kernel       ends the driver process
    
    still fine   kernel, file server, other programs
    MINIX 3      its reincarnation server restarts
                 the crashed driver automatically
  11. Speed or isolation?

    Monolithic kernels are fast, because the parts call each other directly. Microkernels are small and keep failures apart, at the cost of more messages. Real systems mix both, and experts still argue which is better.

    both designs as a Python simulation:
    
    # each trip through the kernel = 2 crossings: trap in, return out
    TRIPS = {
        "monolithic":  ["app -> kernel"],   # fs + driver live inside
        "microkernel": ["app -> file server", "file server -> driver",
                        "driver -> file server", "file server -> app"],
    }
    IN_KERNEL = {
        "monolithic":  {"scheduling", "memory", "messages", "file system", "driver"},
        "microkernel": {"scheduling", "memory", "messages"},
    }
    
    def driver_bug(design):
        if "driver" in IN_KERNEL[design]:
            return "kernel fails, every program stops"
        return "driver process ends, a fresh copy starts"
    
    for design, trips in TRIPS.items():
        print(design, 2 * len(trips), "crossings;", driver_bug(design))
    
    prints:
    monolithic 2 crossings; kernel fails, every program stops
    microkernel 8 crossings; driver process ends, a fresh copy starts

Quiz Unlocks after the last step.