Introduction
The UNIX Kernel is the core of the UNIX operating system. It acts as an intermediary between the user (via the shell) and the hardware. Understanding its architecture is essential for grasping how UNIX manages files and processes.
Overall Architecture
The UNIX system architecture has three main layers:
- Hardware — Physical devices (CPU, Memory, Disk, I/O devices).
- Kernel — Core OS that manages hardware and provides services.
- User Programs / Shell — Commands and applications run by users.
The kernel itself is divided into two major subsystems:
- File Subsystem — Manages files and directories.
- Process Control Subsystem — Manages process creation, scheduling, and termination.
System Call Interface
- User programs cannot access hardware directly.
- They use system calls to request services from the kernel.
- System calls act as a bridge between user space and kernel space.
- Examples:
open(),read(),write(),fork(),exec(),exit().
---
1. File Subsystem
The File Subsystem manages all file-related operations. In UNIX, everything is treated as a file — regular files, directories, devices, and even inter-process communication mechanisms.
Key Components:
a) Inode (Index Node):
- Every file in UNIX is represented internally by an inode.
- The inode stores metadata about the file:
- File type (regular, directory, device)
- File size
- Owner and group
- Permissions (rwx)
- Timestamps (created, modified, accessed)
- Pointers to data blocks on disk
- Note: The inode does NOT store the file name. File names are stored in directory entries that map names to inode numbers.
b) Super Block:
- Contains critical information about the entire file system:
- Size of the file system
- Number of free blocks
- Number of free inodes
- Block size
- It is loaded into memory when the file system is mounted.
c) Buffer Cache:
- A region of kernel memory that stores recently accessed disk blocks.
- Purpose: Reduces the number of slow disk I/O operations by caching frequently used data.
- When a process reads a file, the kernel first checks the buffer cache before accessing the disk.
d) Device Drivers:
- Programs that control hardware devices (disk, keyboard, printer).
- Divided into:
- Block Devices: Transfer data in blocks (e.g., hard disk).
- Character Devices: Transfer data one character at a time (e.g., terminal, keyboard).
File System Operations:
open()— Open a file and get a file descriptor.read()/write()— Read from or write to a file.close()— Close a file descriptor.lseek()— Move the file pointer to a specific position.stat()— Get file metadata (inode information).
---
2. Process Control Subsystem
The Process Control Subsystem manages the creation, execution, scheduling, and termination of processes.
Key Components:
a) Process:
- A process is a program in execution.
- Each process has a unique PID (Process ID).
- Every process (except
init, PID 1) is created by a parent process.
b) Process Table:
- A kernel data structure that stores information about all active processes.
- Each entry contains:
- PID and PPID (Parent PID)
- Process state (Running, Sleeping, Stopped, Zombie)
- CPU registers
- Memory pointers
- Open file descriptors
- Signal information
c) Scheduler:
- Decides which process gets the CPU and for how long.
- UNIX uses a preemptive, priority-based scheduling algorithm.
- Processes with higher priority get CPU time first.
- Priorities are recalculated periodically to ensure fairness.
d) Memory Management:
- Handles allocation and deallocation of memory for processes.
- Uses techniques like swapping (moving entire processes to disk) and paging (moving pages of memory to disk) to manage limited physical memory.
e) Inter-Process Communication (IPC):
- Mechanisms for processes to communicate with each other:
- Pipes: Unidirectional data flow between processes.
- Signals: Notifications sent to processes (e.g.,
SIGKILL,SIGTERM). - Shared Memory: Multiple processes share a common memory segment.
- Message Queues: Processes exchange messages via a queue.
- Semaphores: Synchronization mechanisms to control concurrent access.
Process Operations:
fork()— Creates a new child process (copy of parent).exec()— Replaces the current process image with a new program.wait()— Parent waits for child process to finish.exit()— Terminates the calling process.kill()— Sends a signal to a process.
---
Architecture Diagram (Conceptual)
┌─────────────────────────────────────────┐
│ User Programs / Shell │
├─────────────────────────────────────────┤
│ System Call Interface │
├───────────────────┬─────────────────────┤
│ File Subsystem │ Process Control │
│ ┌─────────────┐ │ Subsystem │
│ │ Inode Table │ │ ┌─────────────────┐ │
│ │ Super Block │ │ │ Process Table │ │
│ │Buffer Cache │ │ │ Scheduler │ │
│ │Dev. Drivers │ │ │ Memory Mgmt │ │
│ └─────────────┘ │ │ IPC │ │
│ │ └─────────────────┘ │
├───────────────────┴─────────────────────┤
│ Hardware Control Layer │
├─────────────────────────────────────────┤
│ Hardware (CPU, Disk, I/O) │
└─────────────────────────────────────────┘
Summary
- The UNIX kernel sits between user programs and hardware.
- File Subsystem manages files using inodes, super blocks, buffer cache, and device drivers.
- Process Control Subsystem manages processes using process tables, schedulers, memory management, and IPC.
- System calls are the gateway for user programs to access kernel services.