Primary Goal of the OS: Acts as an intermediary between the user and the hardware.
Main Goals:
Execute user programs and facilitate problem-solving.
Enhance system convenience.
Efficiently manage hardware resources.
Functions of OS:
Resource Allocator: Manages hardware resources, resolves conflicts.
Control Program: Manages program execution to prevent errors/misuse.
Components of a Computer System:
Hardware: CPU, memory, and I/O devices.
Operating System: Controls and coordinates hardware use.
Application Programs: Define resource usage (e.g. compilers, games).
Users: Individuals, machines, or systems that use the computer.
The Kernel: Core of the OS, always running.
Components:
Other programs are either system programs (bundled with OS) or application programs.
Traps/Exceptions: Software-generated interrupts due to:
Errors (e.g., division by zero).
User requests for OS services (system calls).
Key Points:
Handled by the OS via Interrupt Service Routines (ISRs).
The address of the interrupted instruction must be saved.
Benefits of Multithreading:
Responsiveness: Keeps applications responsive when parts are blocked.
Resource Sharing: Threads share memory, making communication efficient.
Economy: Lower overhead for context switching and cheaper than process creation.
Scalability: Threads can exploit multiple processors, running in parallel.
User Threads vs. Kernel Threads:
User Threads: Managed by user-level libraries, faster, and can block other user threads.
Kernel Threads: Managed by the OS kernel, slower due to system calls, but allow true parallel execution.
Blocking Behavior:
For user threads: If one blocks, they all block.
For kernel threads: Blocking of one does not affect others.
Multithreading Models:
Many-to-One: Multiple user threads to a single kernel thread.
One-to-One: One user thread per kernel thread; allows true parallelism.
Many-to-Many: Many user threads to many kernel threads—flexible.
Two-Level Model: Variation allowing user threads to bind to specific kernel threads.
Types of Scheduling:
First-Come, First-Served (FCFS): Simple but leads to convoy effect.
Shortest Job First (SJF): Minimizes average waiting time, can be preemptive (SRTF).
Round Robin (RR): Time-sharing, each process gets a fixed time slice.
Priority Scheduling: Allocates CPU to highest priority; may cause starvation.
Multilevel Queue Scheduling: Queues based on process type/priority.
Multilevel Feedback Queue: Adapts processes between queues; more flexible.
Real-Time Scheduling: Manages tasks based on deadlines.
Dispatch Latency: Time taken to stop/start processes, including context switching and jumping to the right location.
Convoy Effect: Occurs when short processes wait behind long ones in FCFS scheduling; degrades system efficiency.
Aging: Technique to prevent starvation by increasing the priority of waiting processes over time.
Techniques for Critical Section Problem:
Peterson’s Solution: Two processes, using flag[] and turn for mutual exclusion.
Hardware Support: Atomic instructions like test-and-set and compare-and-swap.
Mutex Locks: Require acquire()/release() for critical section entry/exit.
Semaphores: Binary (mutual exclusion) and counting versions for resources.
Monitors: High-level abstraction with automatic mutual exclusion.
Mutual Exclusion: Only one process may execute in its critical section at a time to prevent race conditions.
Three Requirements:
Mutual Exclusion
Progress
Bounded Waiting
Test-and-Set Function:
boolean test_and_set(boolean *target) {
boolean rv = *target;
*target = TRUE;
return rv;
}
Binary Semaphore: Takes values 0 and 1, behaves like a mutex lock for mutual exclusion.
Disadvantages of Busy Waiting: Wastes CPU cycles and reduces system performance, especially with long critical sections or multiple processes.