Operating System Fundamentals and Architecture
Operating System Fundamentals
An Operating System (OS) is a program that acts as an intermediary between a computer user and the computer hardware. It is divided into four components: Hardware, OS, System/Applications, and Users. The system follows a Fetch-Decode-Execute cycle.
System Operations
- I/O and CPU: Execute concurrently.
- Device Controller: In charge of a particular device type.
- Local Buffer: Moves data to and from main memory and local buffers.
- I/O Process: Data moves from the device to the local buffer.
- Interrupts: The device controller informs the CPU it is done by causing an interrupt.
Interrupts and Execution
An Interrupt is a condition that needs to halt normal execution or triggers the execution of an associated exception handler. An OS is inherently interrupt-driven.
Handling Multiple Interrupts
Multiple interrupts occur when an interrupt takes place while an interrupt service routine is already executing. To handle this, the system can either disable interrupts or use a priority scheme.
Memory Hierarchy and Caching
- Main Memory (RAM): Volatile; stores instructions and data for processors.
- Secondary Memory (Disk): Non-volatile; used for bulk storage.
- Cache Memory (CPU): Expensive, fast memory that is invisible to the OS. It exists in several levels (L1, L2, etc.) to bridge the speed gap between the CPU and main memory.
The Principle of Locality suggests that memory references cluster. Highly accessed data is placed in higher levels of memory. Mapping Policies include direct-mapped, fully associative, and set-associative. Write Policies include Write-through vs. Write-back. The key benefit is improved CPU performance by exploiting temporal and spatial locality of reference.
Multicore Machines
Known as chip multiprocessors, these combine two or more processors (cores) on a single piece of silicon (die). Each core contains components of an independent processor, including L2 cache and, in some cases, L3.
- L1 Cache: Smallest, fastest, and private to each core.
- L2 Cache: Larger, slightly slower; may be shared or private.
- L3 Cache: Largest, slowest cache; usually shared across all cores.
- Consistency: A consistency protocol is required to keep caches in different cores synchronized.
Process Management
A Process is a program in execution. While a program is a passive entity, a process is an active entity that consumes resources and changes states. It consists of a text section (code), data section, and execution context (program counter, registers, etc.).
Process Elements and the PCB
The Process Control Block (PCB) is a vital data structure containing:
- Identifiers: PID, PPID, UID.
- State and Priority: Running, waiting, or ready.
- Program Counter (PC): Address of the next instruction.
- Context Data: Register contents (r1, r2, etc.).
- Memory Information: Pointers to code and pages.
- I/O Status: Allocated resources and requests.
A Process Image is the collection of program data, stack, and attributes. Process Creation involves spawning, where a parent process creates a child process (e.g., using fork() in C). Mode Switching occurs when the processor moves from user mode to kernel mode due to interrupts or traps.
Interprocess Communication (IPC)
Shared Memory is a method of IPC that allows multiple processes to access the same memory space. It is fast because data does not need to be copied. Common System Calls include shmget, shmat, shmdt, and shmctl.
CPU Scheduling
Scheduling occurs at three levels: Long-term (admission), Medium-term (swapping), and Short-term (CPU cycle allocation). The Short-term Scheduler is the main focus, selecting processes from the ready queue.
Scheduling Criteria and Algorithms
- Criteria: Maximize CPU utilization and throughput; minimize turnaround, waiting, and response times.
- Algorithms: First-Come First-Served (FCFS), Shortest Process Next (SPN), Shortest Remaining Time (SRT), Priority Scheduling, and Round Robin (RR).
- Priority Scheduling: Uses integers to rank processes; Aging is used to prevent starvation.
- Round Robin: Each process gets a small time quantum (q). If q is too small, context switching overhead increases; if too large, it behaves like FCFS.
- Linux Fair Scheduler: Uses a Red-Black tree for the ready queue to track execution time.
Operating System Equations
- Waiting Time: Sum of time spent in the ready queue.
- Average Waiting Time: Total waiting time / number of processes.
- CPU Burst Prediction:
t(n+1) = a*tn + (1 - a)*tn - Average Turnaround Time:
Finish Time - Arrival Time - UNIX Priority:
Priority = Base + (CPU_usage / 2) + nice
Disk Management and RAID
Magnetic Disks provide the bulk of secondary storage. Performance is measured by Seek Time (moving the arm to the cylinder) and Rotational Latency (waiting for the sector).
Disk Scheduling Algorithms
- FCFS: Simple but inefficient.
- SSTF: Shortest Seek Time First; may cause starvation.
- SCAN (Elevator): Moves end-to-end serving requests.
- C-SCAN: Circular SCAN; provides more uniform wait times.
- C-LOOK: A version of C-SCAN that only goes as far as the last request.
RAID Structures
- RAID 0: Block stripping for performance (no redundancy).
- RAID 1: Mirroring for reliability.
- RAID 2: Bit-level stripping with parity.
- RAID 3: Dedicated parity disk.
- RAID 4: Block-interleaved parity.
- RAID 5: Distributed parity across disks.
- RAID 6: Extra redundancy for multiple disk failures.
- RAID 10: A combination of mirroring and stripping.
File System Implementation
The file system resides on secondary storage and is organized in layers. On-Disk structures include the boot control block, volume control block, and directory structure. In-Memory structures include the mount table and open-file table. The Virtual File System (VFS) provides an object-oriented implementation for various file systems.
Allocation Methods
- Contiguous Allocation: Easy random access but suffers from external fragmentation.
- Linked Allocation: No external fragmentation but no random access.
- Indexed Allocation: Uses an index block to support random access without fragmentation.
- Linux File System: Uses VFS with four primary objects: inode, file, superblock, and dentry.
