What is the Operating System Kernel?


Definition: The kernel is the core component of an operating system that manages system resources and provides low-level services to applications.

Kernel Architecture

graph TD Apps[Applications] --> Shell[Shell/GUI] Shell --> SysCalls[System Calls] SysCalls --> Kernel[Kernel] Kernel --> PM[Process Management] Kernel --> MM[Memory Management] Kernel --> FS[File System] Kernel --> DM[Device Management] PM --> Hardware[Hardware] MM --> Hardware FS --> Hardware DM --> Hardware

Kernel Responsibilities

1. Process Management
// Linux: Create a new process
pid_t pid = fork();

if (pid == 0) {
    // Child process
    execl("/bin/ls", "ls", "-l", NULL);
} else {
    // Parent process
    wait(NULL);  // Wait for child to finish
}
2. Memory Management
// Allocate memory
void *ptr = malloc(1024);  // User space
// Kernel translates to physical memory

// Virtual to Physical Address Translation
Virtual Address → Page Table → Physical Address
3. File System Management
// System call to open file
int fd = open("/path/to/file", O_RDONLY);
read(fd, buffer, size);
close(fd);

// Kernel handles:
// - Permission checking
// - Disk I/O
// - Buffering
// - File locking
4. Device Management
// Device I/O
write(fd, data, length);  // User space

// Kernel:
// 1. Validates request
// 2. Calls device driver
// 3. Driver communicates with hardware
// 4. Returns result to user space

Types of Kernels

1. Monolithic Kernel

All OS services run in kernel space.

graph TD User[User Space] --> Kernel[Monolithic Kernel] Kernel --> Drivers[Device Drivers] Kernel --> FS[File System] Kernel --> Net[Networking] Kernel --> MM[Memory Mgmt] Drivers --> HW[Hardware] FS --> HW Net --> HW MM --> HW
2. Microkernel

Minimal kernel with services in user space.

graph TD User[User Space] --> Services[OS Services] Services --> Kernel[Microkernel] Kernel --> HW[Hardware] Services --> Drivers[Device Drivers] Services --> FS[File System] Drivers --> Kernel FS --> Kernel
3. Hybrid Kernel

Combination of monolithic and microkernel.

Kernel Modes

Mode Privileges Access Examples
Kernel Mode Full All memory, hardware Kernel, drivers
User Mode Limited Own memory only Applications
Mode Switching
sequenceDiagram participant App as Application (User Mode) participant Kernel as Kernel (Kernel Mode) App->>Kernel: System Call (e.g., read()) Note over Kernel: Switch to Kernel Mode Kernel->>Kernel: Execute privileged operation Kernel->>App: Return result Note over App: Switch back to User Mode

System Calls

System calls are the interface between user applications and the kernel.

Category Examples Purpose
Process Control fork(), exec(), exit() Manage processes
File Management open(), read(), write(), close() File operations
Device Management ioctl(), read(), write() Device I/O
Information getpid(), alarm(), sleep() System info
Communication pipe(), shmget(), msgget() IPC

Kernel Scheduling

CPU Scheduling Algorithms

Real-World Kernels

Linux Kernel
Windows NT Kernel
XNU (macOS/iOS)

Kernel Panic vs Crash