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
- Creating and terminating processes
- Scheduling CPU time
- Inter-process communication (IPC)
- Thread 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
- Virtual memory allocation
- Paging and swapping
- Memory protection
- Cache 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
- File creation, deletion, reading, writing
- Directory management
- Access control and permissions
- File system mounting
// 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 driver interface
- I/O operations
- Interrupt handling
- DMA (Direct Memory Access)
// 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.
- Examples: Linux, Unix
- Pros: Fast, efficient
- Cons: Large, less modular
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.
- Examples: Minix, QNX
- Pros: Modular, stable
- Cons: Slower (more context switches)
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.
- Examples: Windows NT, macOS
- Pros: Balance of performance and modularity
- Cons: Complex design
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
- Round Robin: Each process gets equal time slice
- Priority Scheduling: Higher priority processes run first
- Multilevel Queue: Different queues for different priorities
- Completely Fair Scheduler (CFS): Linux default
Real-World Kernels
Linux Kernel
- Monolithic with loadable modules
- Open source
- Highly customizable
- Used in servers, Android, embedded systems
Windows NT Kernel
- Hybrid kernel
- Proprietary
- HAL (Hardware Abstraction Layer)
- Used in Windows desktop and server
XNU (macOS/iOS)
- Hybrid kernel (Mach + BSD)
- Open source core
- Optimized for Apple hardware
Kernel Panic vs Crash
- Kernel Panic: Fatal error, system halts (Linux/Unix)
- Blue Screen of Death (BSOD): Windows equivalent
- Causes: Hardware failure, driver bugs, memory corruption