Dulranga's Notes
Semester 3Operating Systems

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

  1. Long term scheduler This loads the program from disk to RAM (ready-queue). This determines total no. of processes running concurrently.
  2. 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

AlgorithmTypeHow It WorksKey Tradeoff
First-Come, First-Served (FCFS)Non-PreemptiveExecutes processes strictly in order of arrival.Simple, but long jobs can stall short jobs (convoy effect).
Shortest Job First (SJF)Non-PreemptiveExecutes the process with the shortest estimated execution time.Minimizes average waiting time, but long processes may starve.
Shortest Remaining Time First (SRTF)PreemptivePreemptive 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)PreemptiveGives each process a fixed unit of time (time quantum) in a cyclic queue.Fair and responsive; performance depends heavily on time quantum size.
Priority SchedulingBothAssigns 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)PreemptiveUses 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.

  1. CPU Utilization The percentage of time the CPU is actively performing useful work (executing instructions) rather than sitting idle.
CPU Utilization=CPU Active TimeTotal Time×100\text{CPU Utilization} = \frac{\text{CPU Active Time}}{\text{Total Time}} \times 100
- **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.

  1. Turnaround Time (TturnaroundT_{\text{turnaround}})

    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.

Tturnaround=Tcompletion−TarrivalT_{\text{turnaround}} = T_{\text{completion}} - T_{\text{arrival}}
- **Goal:** **Minimize**
    

4. Waiting Time (WtimeW_{\text{time}})

The cumulative time a process spends waiting in the ready queue to be granted CPU execution time.
Wtime=Tturnaround−CPU Burst TimeW_{\text{time}} = T_{\text{turnaround}} - \text{CPU Burst Time}
- CPU Burst time is the time it used the CPU
- **Goal:** **Minimize**.
    

5. Response Time (RtimeR_{\text{time}})

The time elapsed between a process entering the ready queue and the CPU generating its _first_ execution response.
Rtime=Tfirst execution−TarrivalR_{\text{time}} = T_{\text{first execution}} - T_{\text{arrival}}
- **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. process_scheduling.png

On this page