Operating System Notes
Operating System Notes
Main tasks:
2. Computer-System Organization
• A computer system has one or more CPUs, device controllers, and memory.
• All devices connected via common buses.
• Components:
1. CPU (Central Processing Unit): Executes instructions.
2. Memory: Stores data and instructions.
3. Device Controllers: Each controls a specific I/O device.
4. Buses: Used for communication between components.
• Each device controller has a local buffer and communicates via interrupts.
3. Computer-System Architecture
Main Services:
• A system call is the way programs request services from the OS.
• Acts as interface between the process and the OS.
• Implemented via traps or software interrupts.
Categories:
7. System Programs
Steps:
Types of Structures:
Process Concept
Process Scheduling
• The OS handles scheduling using a scheduler that selects which process should
run next from the ready queue.
• Schedulers:
o Long-term scheduler: selects processes from job pool (controls degree
of multiprogramming)
o Short-term scheduler: selects from ready queue (executes frequently)
o Medium-term scheduler (optional): suspends/resumes processes to
improve performance
Operations on Processes
• Producer-Consumer Problem:
o Producer adds items to a buffer.
o Consumer takes items from the buffer.
o If buffer is full, producer waits. If empty, consumer waits.
o Solved using bounded buffer with synchronization tools like
semaphores or mutexes.
Basic Concepts
• CPU Scheduling is the process of choosing which process gets CPU time.
• The dispatcher gives CPU control to selected process.
• Important when multiple processes are ready but only one CPU is available.
Scheduling Criteria
Scheduling Algorithms
1. FCFS (First-Come-First-Serve):
o Non-preemptive
o Simple but not efficient for short tasks
2. SJF (Shortest Job First):
o Non-preemptive or preemptive
o Best average waiting time
o Needs prior knowledge of burst time
3. Priority Scheduling:
o Each process assigned a priority
o Higher priority runs first
o Risk of starvation (solved using aging)
4. Round Robin (RR):
o Preemptive
o Each process gets a fixed time slice (quantum)
o Good for time-sharing systems
5. Multilevel Queue Scheduling:
o Different queues for different process types
o E.g., system, interactive, batch
o Each queue has its own scheduling algorithm
6. Multilevel Feedback Queue:
o Processes can move between queues
o Based on history and behavior
Thread Scheduling
• Evaluate using:
o Deterministic modeling (using set formulas)
o Queueing models (statistical analysis)
o Simulations
o Implementation and real-world measurement
• Goals: reduce waiting time, maximize CPU use, balance fairness
UNIT 3 – SYNCHRONIZATION
Background
• In modern OS, many processes execute concurrently (at the same time or
interleaved).
• When multiple processes share data/resources (e.g., variables, files), race
conditions can occur if not synchronized.
• A race condition happens when multiple processes access and manipulate
shared data simultaneously, and the final outcome depends on the order of
execution.
• To avoid this, OS must ensure mutual exclusion and synchronization.
• The critical section is the part of a process where it accesses shared resources
(like variables or files).
• Problem: ensure only one process is in its critical section at any time.
• Conditions to solve it:
1. Mutual Exclusion – Only one process in the critical section.
2. Progress – If no process is in the critical section, and some want to enter,
one must be allowed.
3. Bounded Waiting – Limit the number of times other processes can
enter their critical sections before a waiting process is allowed.
Peterson’s Solution
DEADLOCKS
System Model
• A system has multiple resources (CPU cycles, memory, I/O devices).
• Processes request and release resources.
• A deadlock occurs when each process holds one resource and waits to acquire
another, forming a cycle.
• Resources can be:
o Preemptable (can be taken away like CPU)
o Non-preemptable (cannot be taken away like printers)
Deadlock Characterization
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
1. Process Termination:
o Kill all deadlocked processes.
o Or terminate one by one until deadlock breaks.
2. Resource Preemption:
o Take a resource away from a process.
o Requires rollback of that process.
o Need to avoid starvation.
UNIT 4 – MEMORY MANAGEMENT
Background
Swapping
Segmentation
• Logical memory is divided into segments based on types (e.g., code, data,
stack).
• Each segment has its own base and limit (starting address and size).
• Addresses are of the form ⟨segment number, offset⟩.
• Advantages:
o Supports modularity and protection.
o Segments can grow independently.
• Problems:
o External fragmentation possible.
o Requires complex hardware (segment tables).
Paging
Background
• Virtual memory lets a process execute even if not fully loaded in physical
memory.
• Stores large programs by keeping only necessary parts in memory.
• Uses demand paging to load pages when required.
• Helps with:
o Running larger programs.
o Multiprogramming (more processes in memory).
o Reducing I/O by loading pages only when needed.
Demand Paging
Copy-on-Write (COW)
Page Replacement
• When a page fault occurs and memory is full, OS must replace a page.
• Algorithms:
1. FIFO (First-In-First-Out) – Oldest page replaced.
2. LRU (Least Recently Used) – Page not used for longest time.
3. Optimal – Replaces page not needed for longest time in future
(theoretical).
4. Clock – Circular pointer like second-chance algorithm.
• Goal: Reduce page faults.
Allocation of Frames
Thrashing
File Concepts
Access Methods
File Sharing
Protection
• Requires:
o Structures for directories and metadata.
o Allocation techniques to manage space.
o Data structures like file table, inode, bitmap.
• Goal: fast access and reliable storage.
Directory Implementation
Allocation Methods
1. Contiguous allocation:
o File blocks are stored together.
o Fast access.
o Issues: external fragmentation and size prediction.
2. Linked allocation:
o Each block points to next.
o No fragmentation but slow access (no random access).
3. Indexed allocation:
o Uses an index block with pointers to all blocks.
o Supports direct access and efficient for large files.
Disk Structure
Disk Scheduling
• Includes:
o Formatting (low-level, logical).
o Partitioning (dividing disk into sections).
o Boot block (to load OS).
o Bad block management (mark unusable blocks).
• Uses tools like defragmentation to improve performance.
• OS handles mounting and unmounting of file systems.