CACS251 Operating System

Operating SystemUnit 1112 min read

Kernel & System Structures: Types, Layers, Monolithic vs Microkernels

Unit 11 of Operating System explores the core architecture of OS kernels—monolithic vs. microkernels, layered vs. modular designs, system calls, and boot processes—with real-world examples from eSewa, Ncell, and Linux servers.

TAKEAWAYS:

  • The kernel is the OS core that manages hardware, processes, and memory—visible only via system calls.
  • Monolithic kernels (Linux, Windows) bundle all services in one address space for speed, while microkernels (QNX, MINIX) isolate services for safety.
  • Layered models (e.g., OSI) separate functions (e.g., process management → memory → I/O) but add overhead; modular kernels (Linux) load only needed modules.
  • System calls (e.g., open(), fork()) bridge user apps to kernel services via traps/interrupts.
  • Boot process: BIOS → MBR → OS loader → kernel initialization → user space launch (trace this with a timeline).
  • Distributed systems (e.g., Ncell’s SMS gateway) use remote procedure calls (RPC) and clock synchronization (NTP) to coordinate across machines.

1. The Kernel: Definition and Role

The kernel is the heart of an OS—it manages:

  • Processes (CPU scheduling, context switching)
  • Memory (allocation, protection)
  • Hardware (drivers, interrupts)
  • File systems (disk I/O, permissions)

Why hide it?

  • Security: User apps run in user mode; kernel runs in privileged mode (ring 0).
  • Abstraction: Apps interact via system calls (e.g., read(), write()), not hardware directly.
User Mode (App)System Call InterfaceKernel Mode (Ring 0)Hardware
Privilege levels: User apps (ring 3) trap to kernel (ring 0) via system calls.

2. Types of Kernels

All services in kernel spaceExample: Linux (traditional)MonolithicMinimal core, services in user spaceExample: QNX, MINIXMicrokernelCore monolithic + microkernel modulesExample: Windows NT, macOSHybridKernel Types
Comparison of kernel architectures (monolithic vs. microkernel vs. hybrid).

A. Monolithic Kernel

Definition: All OS services (process management, memory, I/O) run in the same address space. Examples: Linux, Windows NT, macOS (Darwin). Pros:

  • Speed: No inter-process communication (IPC) overhead between kernel components.
  • Simplicity: Easier to develop for single-CPU systems. Cons:
  • Bloat: Large codebase (e.g., Linux kernel ~30M lines).
  • Crash risk: One bug can crash the entire system (e.g., Windows BSOD).

Real-world tie-in:

  • eSewa’s payment gateway runs on Linux (monolithic kernel) for high-speed transaction processing. A crash would halt all payments—hence, heavy testing for stability.

B. Microkernel

Definition: Only core functions (process management, memory) run in kernel; others (file systems, drivers) run as user-space servers. Examples: QNX (used in medical devices), MINIX, macOS (since 2000). Pros:

  • Safety: A driver crash won’t take down the system (e.g., Ncell’s SMS server can isolate faulty modules).
  • Portability: Easier to port to new hardware. Cons:
  • Overhead: IPC between servers slows performance (e.g., 10–20% slower than monolithic).
  • Complexity: Harder to design and debug.

Comparison Table:

Feature Monolithic Kernel Microkernel
Address Space Single Split (kernel + servers)
Performance High Lower (IPC overhead)
Stability Low (crash-prone) High (isolated servers)
Examples Linux, Windows QNX, MINIX
Use Case High-speed apps (eSewa) Safety-critical (medical)

3. System Structures

A. Layered Approach

Idea: OS divided into layers (e.g., hardware → low-level → high-level). Example: OSI model (though not an OS, it’s a layered abstraction). Pros:

  • Modularity: Easier to update one layer (e.g., file system).
  • Debugging: Isolate issues to a specific layer. Cons:
  • Overhead: Each layer adds indirection (e.g., a read() call traverses 3+ layers).
  • Performance: Slower than monolithic (e.g., Windows NT uses layers but with optimizations).

Real-world example:

  • NTC’s billing system uses a layered design:
    1. Hardware layer: Routers/switches handle network traffic.
    2. Network layer: TCP/IP manages data packets.
    3. Application layer: Billing software processes user payments.
User ApplicationsSystem CallsProcess ManagementMemory ManagementHardware AbstractionHardware (CPU/Disk)
Layered kernel architecture: Abstraction at each level (e.g., NTC billing system analogy).

B. Modular (Hybrid) Approach

Idea: Combine monolithic speed with microkernel safety by loading only needed modules (e.g., Linux kernel modules). Examples: Linux, FreeBSD. Pros:

  • Flexibility: Add/remove drivers at runtime (e.g., load a Wi-Fi driver only when needed).
  • Performance: Modules run in kernel space (no IPC overhead). Cons:
  • Complexity: Managing dependencies between modules.

Real-world example:

  • Daraz’s order processing uses Linux kernel modules to:
    • Dynamically load payment gateways (e.g., Khalti, eSewa).
    • Scale memory management for peak sales (e.g., during Dashain).

4. System Calls

Definition: Interface between user apps and kernel (e.g., fork(), execve()). How they work:

  1. App makes a system call (e.g., open("file.txt")).
  2. CPU switches to kernel mode via trap (software interrupt).
  3. Kernel executes the call (e.g., checks permissions, reads disk).
  4. Returns control to user mode.
016324863SystemCall Number8 bitsArguments56 bits
x86 syscall instruction format (e.g., `syscall` in Linux).

Common System Calls:

Category Examples
Process Control fork(), exec(), exit()
File Operations open(), read(), write()
Device Control ioctl() (e.g., configure network)
Information getpid(), time()

Worked Example: fork() in a Pathao Driver App

pid_t pid = fork(); // System call
if (pid == 0) {
    // Child process: Handle ride requests
    process_ride();
} else {
    // Parent process: Update driver location
    update_gps();
}
  • Why? Pathao’s app uses fork() to separate ride-logic from GPS updates, improving responsiveness.

5. Boot Process

Steps (trace with a timeline):

  1. BIOS/UEFI: Powers on hardware, runs POST (Power-On Self-Test).
  2. MBR (Master Boot Record): Loads the bootloader (e.g., GRUB).
  3. Bootloader: Loads the OS kernel into memory.
  4. Kernel Initialization:
    • Hardware detection (CPU, RAM, devices).
    • Initialize data structures (process table, memory pools).
  5. User Space: Launch init (Linux) or explorer.exe (Windows).

Real-world tie-in:

  • Ncell’s network switches boot via a custom microkernel to ensure rapid failover if a module crashes.
timeline
    title Boot Process Timeline
    BIOS/UEFI: Post and load MBR
    MBR: Load bootloader (GRUB)
    Bootloader: Load kernel
    Kernel: Initialize hardware
    Kernel: Start init process
    User Space: Launch apps

6. Distributed Systems and Kernel Design

Key Challenges:

  • Transparency: Hide distribution from users (e.g., Ncell’s SMS gateway appears as a single system).
  • Fault Tolerance: If one node fails, others take over (e.g., Google’s distributed file system).
  • Clock Synchronization: NTP (Network Time Protocol) ensures all nodes agree on time (critical for eSewa transactions).

Remote Procedure Call (RPC):

  • How it works:
    1. Client calls a procedure (e.g., transfer_money()).
    2. Stub marshals arguments into a packet.
    3. Network sends packet to server.
    4. Server stub unmarshals and executes the call.
    5. Result returns to client.

Example: Khalti’s payment API

sequenceDiagram
    App->>+KhaltiStub: transfer_money(1000, "user1")
    KhaltiStub->>Network: Send RPC packet
    Network-->>ServerStub: Deliver packet
    ServerStub->>-KhaltiDB: Deduct 1000 from user1
    KhaltiDB-->>ServerStub: Return success
    ServerStub->>Network: Send response
    Network-->>KhaltiStub: Deliver response
    KhaltiStub-->>App: Return transaction ID

Why it matters:

  • NEPSE’s trading system uses RPC to sync orders across multiple servers in Kathmandu and Pokhara.

7. Security in Kernel Design

Threats:

  • Buffer Overflows: Exploit kernel memory (e.g., Heartbleed bug in OpenSSL).
  • Privilege Escalation: Gain unauthorized kernel access (e.g., via a vulnerable driver).
  • Denial of Service (DoS): Crash the kernel (e.g., via infinite recursion in a system call).

Mitigations:

Technique Example
Address Space Layout Randomization (ASLR) Scramble kernel memory locations.
Secure Boot Verify signed kernel modules (e.g., Windows).
Mandatory Access Control (MAC) Linux SELinux restricts kernel operations.

Real-world example:

  • Ncell’s core network uses ASLR and secure boot to prevent SIM-swapping attacks.

In the Real World

  1. eSewa’s Payment Gateway

    • Idea: Monolithic kernel (Linux) for low-latency transaction processing.
    • How: The kernel handles thousands of read()/write() system calls per second to validate payments. A microkernel would add IPC overhead, delaying confirmations.
  2. Ncell’s SMS Gateway

    • Idea: Microkernel (QNX-like) for fault isolation.
    • How: Each SMS routing module runs as a separate process. If one module crashes (e.g., due to a malformed message), others continue. Critical for 99.99% uptime.
  3. Daraz’s Order Fulfillment

    • Idea: Modular kernel (Linux) for dynamic scaling.
    • How: During Dashain sales, Daraz loads extra memory management modules to handle spikes in fork() calls (each order is a new process). Unused modules are unloaded to save RAM.
  4. Pathao’s Driver App

    • Idea: System calls (fork(), exec()) for multitasking.
    • How: The app uses fork() to separate:
      • Child process: Handles ride requests (high-priority).
      • Parent process: Updates GPS location (lower-priority).
    • Without fork(), GPS updates might delay ride matching.

Exam Tip

  1. Kernel Types:

    • Monolithic: Fast, but risky (e.g., "Linux uses a monolithic kernel for speed").
    • Microkernel: Safe, but slower (e.g., "QNX is microkernel-based for medical devices").
    • Modular: Best of both (e.g., "Linux loads drivers as modules").
  2. System Calls:

    • Always explain the trap mechanism (user → kernel mode switch).
    • Example answer for "How does open() work?":

      "The open() system call triggers a trap to kernel mode. The kernel checks file permissions in the file descriptor table, then calls the appropriate file system driver (e.g., ext4) to locate the file on disk. Finally, it returns a file descriptor to the user process."

  3. Boot Process:

    • Memorize the 5 steps (BIOS → MBR → Bootloader → Kernel → User Space).
    • Link to real hardware: "The GRUB bootloader in Linux is stored in the MBR of the disk."
  4. Distributed Systems:

    • For RPC questions, draw the sequence diagram (client → stub → network → server → stub → client).
    • Clock synchronization: Always mention NTP (e.g., "Ncell uses NTP to sync clocks across its base stations").
  5. Deadlocks and Kernels:

    • If asked about deadlocks in kernel design (e.g., "Can a monolithic kernel deadlock?"), answer:

      "Yes. A monolithic kernel’s shared data structures (e.g., process table) can lead to circular wait if two processes lock resources in opposite orders. Microkernels reduce this risk by isolating services."

  6. Common Pitfalls:

    • ❌ "Microkernels are faster than monolithic." → False (they’re slower due to IPC).
    • ❌ "All kernels use layers." → False (Linux is modular, not strictly layered).
    • ❌ Ignoring system call numbers (e.g., int 0x80 in Linux x86). Mention this in answers about traps.

Pro Tip: For numerical questions (e.g., "Calculate CPU usage"), assume:

  • Arrival rate (λ): 6 processes/minute = 0.1 processes/second.
  • Service time (1/μ): 8 seconds → μ = 0.125 processes/second.
  • Utilization (ρ): ρ = λ/μ = 0.1 / 0.125 = 0.8 (80% CPU busy).

Based on the TU BCA syllabus for Operating System (CACS251), unit 11.

Discussion

Loading…