Process Scheduling
If there is only One CPU, but you have multiple programs that need to be executed, the CPU have to do them concurrently. So there should be a way to manage which process runs at which time. This is done by process scheduling
two main types of schedulers
- Long term scheduler This loads the program from disk to RAM (ready-queue). This determines total no. of processes running concurrently.
- Short term schedulers These determine which Process takes the CPU at what time. It takes processes from ready queue and execute them using a defined criteria or algorithm
Pre-emptive VS Non Pre-emptive Scheduling
Preemptive means you do something to prevent a thing from happening before it actually happens. Ex: An software engineer notices the rapid rise of a new programming framework. Even though their current company doesn't use it yet, they decide to spend their weekends learning it so they are completely prepared if their current skills become obsolete.
Non Pre-emptive
Once Process runs in CPU, it keeps running until it terminates itself or gives up voluntarily (for IO operations etc) These are pretty easy to implement and simple, but long non preemptive process can block short set of processes
Pre-emptive
OS can stop a process from running before it decides to stop itself. This is done by time slice interrupt or high priority levels.
Scheduling algorithms
| Algorithm | Type | How It Works | Key Tradeoff |
|---|---|---|---|
| First-Come, First-Served (FCFS) | Non-Preemptive | Executes processes strictly in order of arrival. | Simple, but long jobs can stall short jobs (convoy effect). |
| Shortest Job First (SJF) | Non-Preemptive | Executes the process with the shortest estimated execution time. | Minimizes average waiting time, but long processes may starve. |
| Shortest Remaining Time First (SRTF) | Preemptive | Preemptive version of SJF; switches if a new job arrives with a shorter remaining time. | Optimal average response time, but high overhead from context switching. |
| Round Robin (RR) | Preemptive | Gives each process a fixed unit of time (time quantum) in a cyclic queue. | Fair and responsive; performance depends heavily on time quantum size. |
| Priority Scheduling | Both | Assigns processes a priority score; CPU goes to the highest priority task. | Ensures critical tasks run first; risk of starvation (mitigated by aging). |
| Multilevel Feedback Queue (MLFQ) | Preemptive | Uses multiple priority queues, moving processes between them based on CPU usage. | Highly adaptive to varied workloads; complex to design and tune. |
Performance Metrics
these define how efficiently the CPU does tasks. different OSs use these to optimize the scheduling.
- CPU Utilization The percentage of time the CPU is actively performing useful work (executing instructions) rather than sitting idle.
- **Goal:** **Maximize** (keep b/w $40\% - 90\%$)2. Throughput The total number of processes completed per unit of time (e.g., 50 processes/second). - Goal: Maximize to process as much work as possible.
-
Turnaround Time ()
The total time interval from the moment a process is submitted until it completely finishes execution. It accounts for time spent waiting in memory queues, ready queues, executing on the CPU, and doing I/O.
- **Goal:** **Minimize**
4. Waiting Time ()
The cumulative time a process spends waiting in the ready queue to be granted CPU execution time.- CPU Burst time is the time it used the CPU
- **Goal:** **Minimize**.
5. Response Time ()
The time elapsed between a process entering the ready queue and the CPU generating its _first_ execution response.- **Goal:** **Minimize** (critical for interactive applications).
6. Fairness
Ensuring that every process gets a reasonable share of CPU time and no single process is indefinitely delayed or starved.
