CPU scheduling algorithms
Computer Science · Computer architecture & systems
Question 1 of 10
0Processes: P1 (burst 1 ms), P2 (burst 5 ms), P3 (burst 5 ms). Scheduling uses First Come First Served (FCFS). All processes arrive at time 0 and are queued in the order P1, P2, …. What is the waiting time of P2 (turnaround time − burst time), in ms?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
CPU scheduling algorithms explained
CPU scheduling decides which ready process runs next on the processor. Several processes often want the CPU at once, so the operating system keeps them in a ready queue and picks one by a rule called a scheduling algorithm. The burst time is how long a process needs the CPU. Two measures are used to judge an algorithm: the waiting time, the total time a process spends in the ready queue, and the turnaround time, the time from arrival to completion.
First come, first served (FCFS) runs processes in the order they arrive. It is simple but a long job can delay many short ones. Shortest job first (SJF) runs the process with the shortest burst time, which lowers average waiting time but needs burst times to be known in advance. Round robin gives each process a fixed time slice, called a time quantum; a process that is not finished goes to the back of the queue. Round robin shares the CPU fairly, but a poor choice of quantum adds overhead from switching between processes.
Priority scheduling runs the process with the highest priority first and can cause some processes to wait indefinitely unless aging is used.
For each process, turnaround time equals completion time minus arrival time, and waiting time equals turnaround time minus burst time.
Worked example
Three processes arrive at time 0 with burst times P1 = 5, P2 = 3 and P3 = 8, scheduled FCFS in the order P1, P2, P3.
- P1 runs from 0 to 5, so its waiting time is 0.
- P2 runs from 5 to 8, so its waiting time is 5. P3 runs from 8 to 16, so its waiting time is 8.
- The average waiting time is time units.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
Processes: P1 (burst 7 ms), P2 (burst 6 ms), P3 (burst 9 ms). Scheduling uses First Come First Served (FCFS). All processes arrive at time 0 and are queued in the order P1, P2, …. At what time (in ms) does P2 finish?
Medium
Processes: P1 (burst 4 ms), P2 (burst 8 ms), P3 (burst 4 ms), P4 (burst 8 ms), P5 (burst 3 ms). Scheduling uses Shortest Job First (SJF, non-preemptive). Ties are broken in favour of the lower process number. All processes arrive at time 0. What is the average waiting time (turnaround time − burst time)? Give your answer in ms to 2 decimal places.
Hard
Processes: P1 (arrival 0 ms, burst 6 ms), P2 (arrival 0 ms, burst 9 ms), P3 (arrival 6 ms, burst 3 ms), P4 (arrival 4 ms, burst 2 ms). Scheduling uses Shortest Job First (SJF, non-preemptive). Ties are broken in favour of the lower process number. If the CPU is idle it waits for the next arrival. What is the turnaround time of P4 (completion time − arrival time), in ms?