CPU Scheduling
Many programs may need the CPU at the same time, but each CPU core can run only one process at a time. A scheduling algorithm decides which process should run next. Enter when each process arrives and how much CPU time it needs. Then compare how seven scheduling algorithms share CPU time. The Gantt chart shows the execution timeline, and each decision comes with a simple explanation.
Interactive workspace
Shortest Remaining Time First
SRTF works like SJF, but it can interrupt a running process. If a new process arrives and has less remaining work, it gets the CPU instead.
Enter one number per process, separated by spaces. The first number represents process A, the second represents process B, and so on.
Examples
Gantt chart
context switch preemption (a running process was paused)
CPU is
Clock
0
end of the latest part
Context switches
0
Preemptions
0
CPU busy
0%
so far
Ready queue (next to run on the left)
Why this process? (Shortest Remaining Time First)
The scheduler explains each choice here, using the real numbers at that moment.
Step-by-step explanation
0 of 5 steps
Press Next or Play to see which process runs next and why.
Process statistics
| Process | Arrival | Burst | Finish | Turnaround | Waiting |
|---|---|---|---|---|---|
| A | 0 | 8 | 17 | 17 | 9 |
| B | 1 | 4 | 5 | 4 | 0 |
| C | 2 | 9 | 26 | 24 | 15 |
| D | 3 | 5 | 10 | 7 | 2 |
| Average | 13.00 | 6.50 | |||
Average waiting time
6.50
Average turnaround time
13.00
Total time
26
until the last process ends
What do these words mean?
- Arrival time: when a process joins the queue.
- Burst time: how much CPU time the process needs in total.
- Finish time: when the process is completely done.
- Turnaround time: finish time minus arrival time. The total time from joining to finishing.
- Waiting time: turnaround time minus burst time. The time spent waiting instead of running.
- Preemptive: the CPU can pause a running process to give the turn to another one.
- Context switch: the CPU stops one process and starts another.
- Time quantum: in Round Robin, the longest a process may run before the next process gets a turn.
- Response ratio: in HRRN, (waiting time + burst time) / burst time. It grows the longer a process waits.
- Starvation: when a process waits for a very long time because others keep getting chosen first.
Design diagram: how this demo decides
Click any box to see what it does and why it is built that way.
Start
Start the clock at time 0
- What it does
- The simulator keeps one number, the current time, and moves it forward as processes run.
- Where it lives
- simulateSchedule() in src/lib/scheduling.ts
- Why it is built this way
- Jumping straight to the next event, instead of moving one unit at a time, keeps it fast and exact.
Under the hood
From input to screen
The experiment follows this process. Select any box to see what it does and which function or component is behind it.
Pipeline: input, parsing, simulation, steps, visualization
Click any box to see what it does and why it is built that way.
Start
Input
- What it does
- You enter arrival times and burst times. You can also enter priorities or a time quantum when the selected algorithm needs them. Then you choose one of the seven algorithms.
- Where it lives
- SchedulingDemo.tsx (arrival, burst, priority, quantumText, algo)
- Why it is built this way
- Only the values you enter are stored. The process information is created from those values whenever the simulation runs.
Technical notes
How CPU Scheduling works
How the algorithm works
The simulator keeps track of the current time. It checks which processes have arrived, selects one using the chosen rule, runs it and records the result in the Gantt chart. This continues until all processes finish.
Non-preemptive algorithms (FCFS, SJF, non-preemptive Priority and HRRN) let a process continue until it finishes. Preemptive algorithms (SRTF and preemptive Priority) can choose another process when a new one arrives. Round Robin gives each process a turn of up to one time quantum.
Important steps
- 01Start the clock at time 0.
- 02Check which processes have arrived.
- 03If no process is ready, keep the CPU idle until the next process arrives.
- 04Choose the next process using the selected scheduling algorithm.
- 05Run the process until it finishes, gets interrupted or reaches its time quantum.
- 06Save that part of the Gantt chart with an explanation.
- 07Calculate turnaround and waiting times for all processes.
Relevant variables
- n
- Number of processes, up to 12.
- arrival, burst
- When a process arrives and how much CPU time it needs.
- priority
- A lower number means a higher priority.
- remaining
- CPU time still needed by a process. Used by SRTF and Round Robin.
- quantum
- Maximum time for one Round Robin turn, from 1 to 20.
- turnaround
- Finish time minus arrival time.
- waiting
- Turnaround time minus burst time.
Time and space complexity
| Algorithm | Time | Space |
|---|---|---|
| FCFS, SJF, Priority, HRRN | O(n² log n) | O(n) |
| SRTF, Priority (preemptive) | O(n² log n) | O(n) |
| Round Robin | O(n·R) | O(n + R) |
n is the number of processes. Each selection rebuilds and sorts the ready list. There are at most about 2n selections, which gives O(n² log n) for the basic algorithms. For Round Robin, R is the number of turns, roughly the total burst time divided by the time quantum. Each turn checks the arrival list.
Example input and output
Input
- Arrival: 0 1 2 3
- Burst: 8 4 9 5
- Algorithm: SRTF
Output
- Gantt chart: A 0–1, B 1–5, D 5–10, A 10–17, C 17–26
- Average waiting time: 6.50
- Average turnaround time: 13.00
- For comparison: FCFS gives 8.75 and SJF gives 7.75 average waiting time.
Edge cases
- CPU idle time: If no process has arrived or all available processes have finished, the CPU is shown as idle.
- Ties: The process that arrived first is selected. If arrival times are equal, input order is used. A running process is not interrupted just because another process has the same selection value.
- Invalid input: A burst time of 0, different list lengths, a quantum outside 1 to 20 or more than 12 processes are rejected.
- Round Robin: If the quantum is larger than every process's burst time, the result is the same as FCFS.
- One process: It runs as a single block in the Gantt chart.
Where the code lives
- Enginesrc/lib/scheduling.ts
- Workspacesrc/components/SchedulingDemo.tsx
- Checkstests/scheduling.test.ts