Introduction to Operating Systems
1.1 Role of the Operating System
Section titled “1.1 Role of the Operating System”An operating system (OS) is system software that manages hardware resources and provides services To application programs. It serves as an intermediary between users and the underlying hardware.
Core responsibilities:
- Process management: Create, schedule, and terminate processes.
- Memory management: Allocate and protect memory; implement virtual memory.
- File system management: Organise, store, retrieve, and protect data.
- I/O management: Control and mediate access to I/O devices.
- Protection and security: Enforce access controls and resource isolation.
1.2 Kernel Architectures
Section titled “1.2 Kernel Architectures”Monolithic Kernel. All OS services (file system, device drivers, network stack, scheduler) run In a single address space in kernel mode. Examples: Linux, FreeBSD.
- Advantage: Low overhead from function-call latency.
- Disadvantage: A bug in any component can crash the entire system.
Microkernel. Only essential services (IPC, scheduling, basic memory management) run in kernel Mode. Other services run in user mode as separate processes. Examples: MINIX 3, seL4, QNX.
- Advantage: Fault isolation; easier to verify formally.
- Disadvantage: Higher overhead from message passing between user-mode servers.
Hybrid Kernel. A pragmatic compromise combining monolithic and microkernel ideas. Some Non-essential services run in kernel mode for performance, but the architecture is more modular Than a pure monolith. Examples: Windows NT, macOS XNU.
1.3 System Calls
Section titled “1.3 System Calls”System calls provide the interface between user-mode applications and kernel-mode OS services. They Are invoked via software interrupts (e.g., syscall on x86-64, svc on ARM).
Categories:
| Category | Examples |
|---|---|
| Process control | fork``exec``wait``exit |
| File management | open``read``write``close |
| Device I/O | ioctl``read``write |
| Communication | pipe``shmget``mmap``socket |
| Information | getpid``stat``sysconf |
| Protection | chmod``chown``setuid |
System call overhead. A transition from user mode to kernel mode involves saving user Registers, switching to the kernel stack, validating arguments, executing kernel code, and Returning. Typical overhead: ns on modern hardware.
1.4 Process States and Scheduling
Section titled “1.4 Process States and Scheduling”A process transitions through several states during its lifetime:
| State | Description |
|---|---|
| New | The process is being created. |
| Ready | The process is in memory and waiting to be assigned to a CPU. |
| Running | Instructions are being executed by the CPU. |
| Waiting | The process is waiting for some event (e.g., I/O completion). |
| Terminated | The process has finished execution. |
Scheduling algorithms determine which ready process receives CPU time:
- First-Come, First-Served (FCFS): Simple, but suffers from the convoy effect (short jobs wait behind long ones).
- Shortest Job First (SJF): Optimal average waiting time but requires advance knowledge of CPU bursts.
- Round Robin (RR): Each process receives a fixed time quantum; good responsiveness for interactive workloads.
- Priority Scheduling: Processes with higher priority run first; can cause starvation of low-priority processes.
1.5 Memory Management: Virtual Memory
Section titled “1.5 Memory Management: Virtual Memory”Virtual memory separates logical addresses seen by processes from physical memory addresses. The MMU (Memory Management Unit) translates virtual addresses to physical ones using page tables.
Paging divides memory into fixed-size frames (physical) and pages (virtual). A virtual address consists of a page number and an offset . The page table maps to a frame number , giving physical address .
Page replacement policies (FIFO, LRU, Optimal) determine which page to evict when memory is full. Belady’s anomaly shows that FIFO can have higher miss rates with more frames; LRU and Optimal do not suffer from this.
1.6 Interrupts and Exception Handling
Section titled “1.6 Interrupts and Exception Handling”An interrupt is a signal from hardware or software that causes the CPU to pause its current execution and run an interrupt handler. Types include:
- Hardware interrupts: Generated by devices (keyboard, disk, timer) via the interrupt controller.
- Software interrupts: Generated by instructions like
intorsyscall(used for system calls). - Exceptions: Generated by the CPU on error conditions (page fault, division by zero).
The interrupt vector table (IVT) maps interrupt numbers to handler addresses. After handling, the CPU returns to the interrupted instruction (or terminates the process for fatal exceptions).
1.7 File System Concepts
Section titled “1.7 File System Concepts”File systems organise persistent data on storage devices. Common layouts:
- Contiguous allocation: Fast sequential access but suffers from external fragmentation.
- Linked allocation: Each block points to the next; no fragmentation but poor random access.
- Indexed allocation: An index block contains pointers to data blocks; good for both sequential and random access (used in Unix inode-based systems).
1.8 Worked Example: System Call Flow
Section titled “1.8 Worked Example: System Call Flow”Problem. Trace the execution path of a user-mode call to read(fd, buf, count).
Solution
- The C library wrapper (glibc) places arguments in registers and executes a
syscallinstruction. - The CPU switches to kernel mode, saves the return address and user stack pointer.
- The system call handler dispatches to the kernel’s
sys_readfunction based on the syscall number. sys_readvalidates the file descriptor, checks buffer accessibility, and invokes the file system or device driver.- Data is transferred from kernel buffers to user-space memory (via
copy_to_user). - Control returns to user mode; the wrapper function returns the number of bytes read.
1.9 Deadlocks
Section titled “1.9 Deadlocks”A deadlock occurs when two or more processes are each waiting for resources held by the other. Four necessary conditions (Coffman conditions):
- Mutual exclusion: Resources cannot be shared.
- Hold and wait: Processes hold resources while waiting for others.
- No preemption: Resources cannot be forcibly taken.
- Circular wait: A cycle of processes each waiting for the next.
Deadlock prevention strategies: eliminate one of the four conditions. Deadlock avoidance (banker’s algorithm) requires advance knowledge of maximum resource needs. Detection and recovery (resource allocation graph) is used in practice.