CS2850 Full Detailed
Memory Management
1. Static vs. Dynamic Relocation
(Slide 25,26,28)
Static Relocation
- Addresses bound at compile/load time.
- Physical addresses are fixed once assigned.
- Used in early systems without multiprogramming (e.g., batch systems).
- Limitation: Cannot support process relocation or multiprogramming since physical addresses are hardcoded.
Dynamic Relocation
- Addresses bound at runtime using base and limit registers (Managed by the MMU).
- Enables virtual memory and multiprogramming:
- Base register stores the physical address of the process’s start.
- Limit register prevents processes from accessing other memory regions.
- Example: Modern OSes use this for virtual address translation (e.g., 2023 Q1a).
2. Paging & Virtual Memory
(Slides 27,28,31,32)
Virtual Address Translation
- Virtual address split into page number (indexing the page table) and offset (within the page).
Example:
- For a 32-bit virtual address with 4KB page size (12-bit offset):
- Page number = 20 bits → 1 million pages.
Two-Level Page Tables (Slide 32, 2023 Q2c)
- Reduces page table size by splitting the page number into multiple indices.
Example: With 12 bits for the top-level index:
- 2¹² = 4096 second-level tables.
- Each entry maps to a 4KB page frame.
Page Replacement Algorithms (Slides 29,30)
- NFU (Not Frequently Used): Evicts pages with the lowest usage counter.
- Example Q: Given R/M bits, justify eviction based on NFU (2023 Q2).
- LRU (Least Recently Used): Prioritizes recently accessed pages.
- FIFO (First-In, First-Out): Simple but suffers from Belady’s anomaly.
- Optimal (Belady’s Algorithm): Theoretical ideal (requires future knowledge).
3. Linux Memory Management
(Slide 44)
Virtual Address Space Layout
- Divided into segments:
- Text: Executable code.
- Data: Initialized variables.
- BSS: Uninitialized data (e.g., static int x;).
- Stack: Local variables and function calls.
- Heap: Dynamically allocated memory (via malloc/free).
- Mapped Files: Shared libraries or files mapped to memory (e.g., mmap).
Physical Memory Management
- Zones: Divides memory into regions (DMA, Normal, Highmem).
- Buddy System: Allocates memory in power-of-2 blocks for efficient fragmentation management.
- Example: Merging adjacent free blocks to form larger chunks.
- Slab Allocator: Manages kernel objects (e.g., task structs) to reduce internal fragmentation.
System Calls
- brk(): Adjusts the heap size by modifying the program break.
- mmap()/munmap(): Maps/unmaps files or anonymous memory into the process address space.
4. Page Replacement in Linux
(Slides 29,30,31,44)
PFRA (Page Frame Reclaimation Algorithm)
- Uses a combination of LRU and clock algorithm to evict pages.
- Prioritizes evicting clean pages (unmodified) over dirty pages (modified).
Swappiness
- Kernel parameter controlling preference for swapping pages to disk vs. dropping caches.
5. Design Issues for Paging
(Slide 31)
Local vs. Global Replacement
- Local: Replaces pages from the same process. Ensures fairness but may underutilize memory.
- Global: Replaces any page in memory. Improves memory utilization but risks thrashing.
Handling Page Faults
- On a page fault, the OS:
- Locates the required page on disk.
- Selects a victim page (using replacement policy).
- Writes back dirty victim pages.
- Loads the new page into memory.
6. Case Studies & Exam Focus
(Past Papers 2022-2024)
- Dynamic Relocation: Explain how it enables multiprogramming (2023 Q1a).
- Two-Level Page Tables: Calculate # of second-level tables and entries (2023 Q2c).
- NFU vs. LRU: Compare overheads and efficiency in a given scenario.
- Linux brk() vs. mmap(): Contrast heap expansion vs. dynamic memory mapping.
Key Formulas
Virtual Address Translation
- Physical Address = (Page Table Entry Frame Number) × Page Size + Offset
Two-Level Page Table Size
- Total second-level tables = 2^(top-level bits).
- Entries per second-level table = 2^(remaining virtual address bits).
Conclusion
Mastery of these concepts, along with past paper practice (e.g., 2023 Q1, Q2; 2022 Q3), ensures readiness for theory and problem-solving questions. Focus on algorithm trade-offs (NFU vs. LRU), Linux mechanisms (buddy system, slab allocator), and address translation workflows.
Process Scheduling
1. Batch Systems Scheduling (Slide 22)
First-Come-First-Served (FCFS)
- Processes are executed in the order they arrive.
- Advantage: Simple to implement; low overhead.
- Disadvantage: Suffers from the convoy effect where long processes delay shorter ones.
- Example: If processes arrive in order A (10ms), B (2ms), C (5ms), turnaround times under FCFS would be 10, 12, 17 (avg: 13ms).
Shortest Job First (SJF)
- Prioritizes jobs with the shortest burst time.
- Advantage: Minimizes average waiting time (optimal for batch systems).
- Disadvantage: Requires prior knowledge of burst times; can cause starvation for longer jobs.
- Exam Tip: Use a Gantt chart to visualize scheduling (e.g., reordering A, B, C as B (2ms), C (5ms), A (10ms) gives avg turnaround time of 6.3ms).
2. Interactive Systems Scheduling (Slide 23)
Round Robin (RR)
- Allocates CPU time in fixed quantum slices (e.g., 4ms) cyclically to each process.
Trade-offs:
- Small quantum → High context-switching overhead but fairer responsiveness.
- Large quantum → Degrades to FCFS behavior.
Example: For processes A (3ms), B (5ms), C (2ms), with 4ms quantum:
- Execution order: A (3ms done), B (4ms → 1ms remaining), C (2ms done), B (resumes for 1ms).
- Turnaround times: A=3, B=9, C=6 (avg=6ms).
Priority Scheduling
- Assigns priorities to processes (e.g., system processes > user processes).
- Risk: Priority inversion (low-priority process holds a resource needed by a high-priority one).
3. Real-Time Systems Scheduling (Slide 24)
Hard vs. Soft Real-Time
- Hard: Deadlines must be met (e.g., flight control systems).
- Soft: Deadlines are preferred but not catastrophic (e.g., streaming video).
Algorithms
- Rate-Monotonic: Assigns priorities inversely to task periodicity (shorter periods = higher priority).
- Earliest Deadline First (EDF): Dynamically schedules tasks based on closest deadlines.
4. Windows Scheduling (Slide 49)
Priority-Based with 32 Levels
- Priorities grouped into classes:
- Real-time (16–31): Kernel threads (e.g., I/O management).
- Variable (1–15): User-mode threads.
- Idle (0): Background tasks.
Mechanisms
- Quantum adjustment: Reduces quantum for foreground apps to improve responsiveness.
- Priority boosting: Temporarily raises priority of I/O-bound or starved threads.
Key Comparisons
Algorithm | Use Case | Pros | Cons |
---|---|---|---|
FCFS | Batch | Simplicity | Convoy effect |
SJF | Batch | Optimal avg wait time | Requires burst time knowledge |
Round Robin | Interactive | Fairness, responsiveness | Overhead with small quanta |
Priority | Real-time/General | Flexible prioritization | Starvation risk, priority inversion |
Exam Focus
Theory Questions
- Compare SJF vs. FCFS using avg waiting time calculations.
- Explain how dynamic quanta in RR balance throughput vs. latency.
Programming Practice
- Simulate RR scheduling for given burst times and quanta.
- Analyze Windows thread priorities using code snippets involving SetThreadPriority().
For further details, refer to the process life cycle in Slide 6 and context-switching optimizations in Slide 8 (threads vs. processes).
Inter-Process Communication (IPC)
IPC Methods in Operating Systems
The course covers two broad approaches to IPC: busy-waiting (active polling) and blocking (sleep/wakeup) methods. Below is a breakdown of the key techniques and problems:
1. Busy-Waiting Solutions
Key slides: 11 (Lock Variables), 12 (Peterson’s), 13 (Hardware), 14 (Sleep/Wakeup)
Lock Variables
- Mechanism: Processes check a shared “lock” variable (0 = unlocked, 1 = locked).
- Failure Case: Race condition if two processes read 0 simultaneously.
Example:
1 |
|
- Limitation: Atomicity issues (fails to enforce mutual exclusion reliably).
Strict Alternation
- Uses a shared turn variable to enforce alternating access (e.g., only process 0 runs if turn=0).
- Drawbacks: Starvation if one process exits prematurely.
Peterson’s Algorithm
- Combines turn and a flag array (interested[2]) for two processes.
- Ensures mutual exclusion, progress, and bounded waiting.
Code snippet:
1 |
|
Hardware Solutions
- Disabling interrupts: Prevents context switches (works for single-CPU only).
- Atomic instructions: TSL (Test-and-Set Lock) or XCHG instructions to implement locks.
2. Blocking (Sleep/Wakeup) Solutions
Key slides: 14 (Sleep/Wakeup), 15 (Semaphores), 16 (Mutexes), 17 (Monitors), 18 (Message Passing/Barriers/RCU)
Sleep/Wakeup
- Mechanism:
- sleep(): Process blocks if a resource is unavailable.
- wakeup(): Reactivates a process when the resource becomes available.
Example: Producer-consumer problem:
- Producer sleeps if the buffer is full; consumer calls wakeup(producer) after consuming.
Semaphores
- Operations: wait() (P) decrements, signal() (V) increments a counter.
- Binary Semaphores: Act as mutexes (0 or 1).
- Counting Semaphores: Track available resources.
Producer-consumer implementation:
1 |
|
Mutexes
- Simplified semaphore with only locked/unlocked states.
- Often implemented via atomic instructions (e.g., pthread_mutex_lock).
Monitors
- Definition: A high-level synchronization construct with built-in mutual exclusion.
- Combines procedures, variables, and condition variables (wait(), signal()).
Example (Bank account):
1 |
|
Message Passing
- System calls: send(destination, &message), receive(source, &message).
- Blocking vs. Non-blocking:
- send() blocks if the receiver’s buffer is full (synchronous).
Barriers
- Force processes to wait until all reach a synchronization point.
3. Classical IPC Problems
Key slides: 19 (Dining Philosophers), 20 (Readers-Writers)
Readers-Writers Problem
- Goal: Allow multiple readers or a single writer to access a shared resource.
Solutions:
- First Readers-Writers: Readers may starve writers.
- Second Readers-Writers: Writers have priority.
Semaphore implementation:
1 |
|
Dining Philosophers
- Problem: Five philosophers sharing forks; deadlocks if all pick up left forks simultaneously.
Solutions:
- Semaphores: Limit concurrent fork access (e.g., only 4 philosophers allowed to eat).
- Asymmetric picking: Odd-numbered philosophers pick left first, even-numbered pick right.
4. Common Pitfalls
- Lost wakeups: E.g., in early sleep/wakeup implementations, a process checks a condition, gets interrupted, and misses a wakeup signal.
- Priority inversion: A low-priority process holds a lock needed by a high-priority process.
Slides Reference
- Slide 11: Lock Variables
- Slide 12: Peterson’s Algorithm
- Slide 14: Sleep/Wakeup (Producer-Consumer example)
- Slide 15: Semaphores and Producer-Consumer implementation
- Slide 19: Dining Philosophers
- Slide 20: Readers-Writers problem
This framework aligns with exam focus areas like implementing synchronization primitives and analyzing classical IPC problems.
File System
1. Core Properties and Structure (Slide 34)
Purpose
- Provides long-term storage of information.
- Maintains data persistence beyond process termination.
- Supports concurrent access by multiple processes.
File Management by OS
- Structure: Organizational logic (e.g., byte stream, records).
- Naming: Rules for filenames and extensions (e.g., filename.ext).
- Access: Read/write/execute permissions and synchronization.
- Implementation: Allocation strategies (contiguous, linked, indexed).
2. File System Layout and Implementation (Slide 35)
Disk Partition Structure
- Master Boot Record (MBR): Contains partition table and boot loader.
Layout includes:
- Boot Block: Initialization code.
- Superblock: Metadata (e.g., disk size, block count, free block list).
- I-nodes: Indexed structures storing file metadata (owner, permissions, block pointers).
- Directories: Hierarchical organization of files.
Allocation Methods
- Contiguous Allocation: Files occupy consecutive disk blocks.
- Example: Slide 35 shows contiguous allocation for seven files.
- Disadvantage: Fragmentation after file deletion (e.g., gaps when Files D/F are removed).
3. File Allocation Table (FAT) vs. Linked List (Slide 36)
MS-DOS FAT
- Structure: Centralized table mapping file blocks.
- Directory entries store filenames, attributes, and the first block number.
- FAT entries act as pointers to subsequent blocks.
Advantages over Linked List:
- Faster random access (no sequential traversal for large files).
- Efficient block updates using a single table (2022 Q3a).
Linked-List Allocation
- Each block contains a pointer to the next block.
- Drawback: Sequential traversal increases seek time for large files.
4. UNIX File Systems (Slide 36 & 37)
I-nodes
- Store metadata (UID, timestamps, permissions) and direct/indirect block pointers.
- Enable efficient storage for small/large files (e.g., Ext4 features).
File System Examples
- MS-DOS: Flat directory structure (limited depth).
- UNIX V7: Hierarchical directories and symbolic links.
NFS (Network File System)
- Architecture: Clients mount remote directories via virtual file system (VFS) layer.
- Implementation: Uses RPC for file operations, buffers data in local caches.
5. Key Terminology (Slides 34–37)
- File Protection Modes: Read, write, execute permissions (e.g., chmod in Linux).
- Directory Implementation:
- MS-DOS: Fixed-size entries with direct block pointers.
- UNIX: Dynamic entries with i-node references.
- Superblock: Critical metadata structure; corruption can render the file system unusable.
Exam-Specific Focus
Question Types
- Compare FAT vs. Linked-List allocation (e.g., seek time analysis).
- Explain how UNIX i-nodes support file growth efficiently.
- Describe the role of the Superblock in FS recovery.
Case Study
- MS-DOS directory entries vs. UNIX V7’s hierarchical model.
This summary integrates concepts directly from the cited slides, ensuring alignment with the course material.
Deadlocks
1. Deadlock Definition (Slide 38)
A deadlock occurs when:
- A set of processes is blocked, each waiting for a resource held by another process in the set.
Example: Process A holds Resource R and requests Resource S, while Process B holds S and requests R.
Four Necessary Conditions (all must hold for deadlock)
- Mutual Exclusion: Resources are non-sharable (e.g., printers).
- Hold and Wait: Processes hold resources while waiting for others.
- No Preemption: Resources cannot be forcibly taken from processes.
- Circular Wait: A cycle exists in the resource allocation graph.
2. Deadlock Detection (Slide 39)
Single Resource per Type
Resource Allocation Graphs:
- Nodes = Processes (P) and Resources (R).
- Edges: Request edge (P → R) or Assignment edge (R → P).
- Deadlock exists iff the graph contains a cycle that cannot be resolved.
Example: Slide 39 includes a system where processes A, D, F, etc., form cycles (e.g., A→S→D→U→A).
Multiple Resources per Type
Matrix-based Algorithm:
- Current Allocation Matrix (C): Tracks resources assigned to processes.
- Request Matrix (R): Tracks outstanding resource requests.
- Available Vector (A): Lists free resources.
Steps:
- Find an unmarked process Pi where its row in R ≤ A.
- Add Pi’s allocated resources to A and mark Pi.
- Repeat until no more processes can be marked.
- Unmarked processes at termination are deadlocked.
3. Deadlock Avoidance: Banker’s Algorithm (Slide 40)
Purpose
Ensure the system remains in a safe state (i.e., processes can complete in some order without deadlock).
Key Components
- Max: Matrix of maximum resource claims per process.
- Allocated: Current resource assignments.
- Need = Max – Allocated.
- Available: Free resources.
Algorithm Steps
- Verify if Need ≤ Available for any process.
- Assume the process completes, release its resources, update Available.
- Repeat until all processes are marked as safe.
- Safe state if all processes can complete; unsafe otherwise.
Example: Slide 40 illustrates resource trajectories and iterative checks for safety.
4. Deadlock Prevention (Slide 41)
Strategies to break one of the four necessary conditions
Mutual Exclusion
- Allow resource sharing where possible (e.g., read-only files).
- Impossible for inherently exclusive resources (e.g., printers).
Hold and Wait
- Require processes to request all resources upfront (pre-allocation).
- Disadvantage: Low resource utilization.
No Preemption
- Allow OS to revoke resources (e.g., forcibly reclaim memory).
- Example: Rollback a process holding a resource to a previous savepoint.
Circular Wait
- Impose a total ordering of resource types (e.g., R1 < R2 < R3).
- Require processes to request resources in ascending order.
Exam Focus Areas
- Analyze resource allocation graphs to identify deadlocks (Slide 39).
- Apply the Banker’s Algorithm to determine if a state is safe (Slide 40).
- Contrast prevention vs. avoidance (e.g., breaking specific conditions vs. maintaining safe states).
- Classic problems: Cyclic resource dependencies in multi-process systems.
Key References
- Slide 38: Deadlock definition and models.
- Slide 39: Detection methods for single and multiple resources.
- Slide 40: Banker’s Algorithm implementation.
- Slide 41: Prevention strategies (breaking mutual exclusion, hold-and-wait, etc.).
Adapted from Tanenbaum & Bos, Modern Operating Systems (4th ed.).
Virtualization
1. Virtualization Overview
Definition: Virtualization allows multiple isolated virtual machines (VMs) or containers to run on a single physical host, abstracting hardware resources.
Purpose:
- Improve resource utilization.
- Isolation between guest environments for security.
- Flexibility in running diverse operating systems/applications.
2. Hypervisors (Virtual Machine Monitors)
Slide 50/51 and 《50.Virtualization.pdf》 emphasize hypervisors, which manage resource allocation between physical hardware and VMs:
Type 1 (Bare-Metal)
- Runs directly on physical hardware.
- Examples: VMware ESXi, Microsoft Hyper-V, Xen.
- Advantages: Higher efficiency/resource isolation, minimal overhead, ideal for data centers.
Type 2 (Hosted)
- Runs atop a host OS (e.g., Windows/macOS).
- Examples: VMware Workstation, VirtualBox.
- Advantages: Easier setup for development/testing but with higher latency.
VMware ESX (Type 1) is highlighted for:
- Resource Isolation: Ensures VMs do not interfere with each other.
- Efficiency: Direct hardware access reduces overhead.
3. Key Requirements for Hypervisors
From 《50.Virtualization.pdf》:
- Fidelity: Guest software (OS/apps) behaves identically to running on native hardware.
- Performance: Near-native execution speed for most instructions.
- Safety: Hypervisor retains full control of hardware; guests operate in restricted mode.
- Isolation: Faults/crashes in one VM do not affect others.
4. Virtualization Techniques
Memory Virtualization
- Address Translation: Hypervisor maps guest virtual addresses to physical addresses using shadow page tables or hardware-assisted extensions (Intel EPT, AMD RVI).
- Ballooning: Dynamically reclaims unused memory from VMs to optimize allocation.
I/O Virtualization
- Device Passthrough: Direct hardware access for critical performance (e.g., GPUs).
- Paravirtualization: Guests use hypervisor-aware drivers (e.g., virtio in Linux) to reduce overhead.
- Software Emulation: Hypervisor emulates devices (e.g., virtual NICs/disks), but introduces latency.
5. Containerization
While not explicitly covered in slides 50/51, 《50.Virtualization.pdf》 contrasts containers with VMs:
Key Differences
- Containers share the host OS kernel; VMs run full OS stacks.
- Containers are lightweight, faster to start, but offer weaker isolation.
Use Cases
- Microservices, DevOps pipelines, resource-efficient application deployment.
6. Challenges in Virtualization
From exam focus areas:
- Overhead: CPU/memory costs for hypervisor operation.
- Complexity: Managing resource allocation conflicts (e.g., CPU time, disk I/O).
- Security: Hypervisor vulnerabilities (e.g., “VM escape”) and side-channel attacks between VMs.
Exam Focus
- Compare Type 1 vs. Type 2 hypervisors in terms of use cases, performance, and isolation.
- Analyze how memory virtualization handles address translation and optimizes usage.
- Contrast VM-based virtualization with containers (referenced in notes but not slides).
Programming Questions
1. Programming Questions in Past Papers
Topics are drawn from C programming fundamentals and OS-specific concepts, requiring careful handling of memory, concurrency, and system interactions. Key formats include:
A. Pointer Manipulation
Past Exam Focus (e.g., 2023 Q3):
Example Task: Swap values using a function like swapx(int *p1, int *p2).
Common Pitfalls:
- Forgetting to dereference pointers in assignments (e.g., *p1 = *p2 instead of p1 = p2).
- Misusing pointer arithmetic (e.g., ptr++ instead of (*ptr)++).
Key Solutions:
- Always validate pointers for NULL before dereferencing.
- Use diagrams to visualize pointer relationships (e.g., arrays, structs).
B. Dynamic Memory Management
Slide References: 46, 47 (e.g., 202425_CS2850_w1_slides).
Typical Tasks:
Heap Allocation:
1 |
|
Exam Focus:
- Check malloc’s return value to handle out-of-memory errors.
- Use sizeof() for structs (e.g., malloc(n * sizeof(struct Node))).
Memory Leaks/Corruption:
Example Q: “Explain why failing to free s after copyString(input, s); is dangerous.”
Answer: Accumulated leaks degrade performance; corrupted memory risks crashes.
C. String Operations
Slide Reference: 48 (e.g., 2023 Q3).
Frequent Tasks:
String Truncation/Splicing:
- Always reserve space for \0 (e.g., i < SIZE-1 in loops).
- Example Error: strcpy(dest, src) without bounds checking → buffer overflow.
String Merging:
Exam Q: “Why must you null-terminate a string before merging?”
Answer: Functions like strcat rely on \0 to determine termination.
D. OS-Specific Coding
Examples from Slides:
- Process Creation: Use fork() and wait() for multiprocessing (Slides 43–44).
- Thread Safety: Implement mutual exclusion using semaphores or mutexes (referenced in IPC slides).
Sample Code:
1 |
|
2. Exam Strategies for Programming Questions
- Pseudocode First: Outline logic with comments (e.g., loop conditions, error checks).
Edge-Case Testing:
- Null/empty strings (e.g., input = “”).
- Maximum buffer sizes (e.g., SIZE = 1024).
Memory Validation:
- Always initialize variables (e.g., char c = ‘\0’;).
- Use valgrind-like mental checks for leaks/corruption.
3. Common Pitfalls and Fixes
Error Type | Example Mistake | Correction |
---|---|---|
Missing Null Terminator | dest[SIZE] = … → overflow | Use dest[SIZE-1] = ‘\0’; |
Pointer Misuse | int *p = x; instead of *p=x; | Dereference properly: *p = 5; |
Undefined Behavior | Accessing freed memory | Set pointers to NULL after free(). |
4. Key Slides for Revision
- Dynamic Memory: 46 (malloc/free), 47 (string pitfalls).
- Concurrency: 15 (semaphores), 43–44 (process/thread syscalls).
- OS Utilities: 48 (file I/O in C), 49–51 (system libraries).
By mastering these concepts and practicing past paper questions, you can confidently tackle programming tasks in the exam.
Always reference lecture slides in explanations (e.g., “As per Slide 47, strcpy without bounds checking…”).