Monolithic kernels and microkernels
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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?
اقرأ الدرس خطوة بخطوة
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?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)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)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()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)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 seL4read() 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 replyFile 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 itTwo 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 messagesThe 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 automaticallySpeed 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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What makes a kernel monolithic?
Monolithic is about where the code runs, not how it is stored: the file system, drivers, scheduling and memory management all run in kernel mode and call each other directly. Linux and xv6 work this way.
02In a microkernel, where does the file system run?
A microkernel keeps only scheduling, memory and message passing in kernel mode. Services such as the file system and drivers run as user-mode server processes, and programs reach them by sending messages through the kernel.
03In this lesson's model, one read() makes 2 user/kernel crossings with a monolithic kernel. How many with the microkernel, and why?
The file server cannot call the driver directly, because the driver is another process. Every message goes through the kernel: a trap into kernel mode and a return to user mode in the receiver. Four messages make 8 crossings.
04A disk driver writes through a bad pointer. What happens with a monolithic kernel?
In kernel mode no check stands between the driver and the rest of the kernel. As the xv6 book puts it, in a monolithic kernel a mistake is fatal: if the kernel fails, all applications fail too and the computer must reboot.
05Why do monolithic kernels such as Linux remain popular?
Direct function calls avoid the extra crossings and message copies of a microkernel. The price is weaker isolation. Real systems mix both ideas, and there is no conclusive evidence that one design is better overall.
المسرد
- Kernel
- The core of the operating system. It runs in kernel mode with full control of the CPU, memory and devices.
- Monolithic kernel
- A kernel in which all operating-system services, including the file system and device drivers, run together in kernel mode as one program.
- Microkernel
- A small kernel that keeps only what needs kernel mode (scheduling, memory, message passing) and runs other services as user-mode processes.
- Server
- An operating-system service, such as a file system or a driver, running as its own user-mode process in a microkernel system.
- Device driver
- Code that knows how to work one specific device, such as a disk.
- IPC (inter-process communication)
- A way for processes to send each other messages. In a microkernel the kernel carries every message.
- Trap
- A switch from user mode into kernel mode at a fixed kernel entry point, for example when a program calls read(). Return-from-trap switches back.
- Crossing
- One switch between user mode and kernel mode. A trip through the kernel costs two: a trap in and a return out.
الملاحظات والمصادر
- The crossing counts are for this simple model: one read() and the user-mode/kernel-mode switches its messages need. They leave out the disk interrupt and how a user-mode driver reaches its device, which differs between systems.
- Real microkernels trim the cost of messages. In seL4, one call can send a request and wait for the reply, a server can reply and wait for the next request in one call, and small messages travel in CPU registers. Every message still passes through the kernel.
- Many systems sit in between. Linux is monolithic but loads drivers as modules at run time, straight into kernel space (init_module). Some microkernel systems run a few servers in kernel mode for speed.
- In MINIX 3 each device driver is a separate user-mode process, and a reincarnation server pings the drivers and restarts one that crashes.
- Because a microkernel is small, it can be checked very carefully: seL4, a member of the L4 family, has been formally verified for memory safety and other security properties.
- R. Cox, F. Kaashoek, R. Morris, xv6: a simple, Unix-like teaching operating system (MIT 6.1810), section 2.3 Kernel organization
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 2: Introduction to Operating Systems
- Berkeley CS162, Lecture 2: Four Fundamental OS Concepts (dual mode operation)
- Linux man-pages: read(2)
- Linux man-pages: init_module(2)
- MINIX 3 wiki: Reliability
- seL4 tutorials: IPC
- seL4 Foundation: About seL4
اختبار يتاح بعد الخطوة الأخيرة.