0
0
Operating-systemsComparisonBeginner · 4 min read

FCFS vs SJF vs Round Robin: Key Differences and When to Use Each

FCFS schedules processes in the order they arrive, SJF picks the shortest job next, and Round Robin cycles through processes giving each a fixed time slice. FCFS is simple but can cause long waits, SJF minimizes average wait time but needs job length knowledge, and Round Robin ensures fairness with time-sharing.
⚖️

Quick Comparison

Here is a quick table comparing FCFS, SJF, and Round Robin scheduling algorithms based on key factors.

FactorFCFSSJFRound Robin
Scheduling OrderFirst come, first servedShortest job firstFixed time slices in rotation
PreemptiveNoCan be preemptive or non-preemptiveYes
Average Waiting TimeCan be highUsually lowestModerate
FairnessLow (long jobs delay others)Can starve long jobsHigh (time sharing)
ComplexitySimpleNeeds job length infoModerate
Use CaseBatch systemsWhen job lengths knownTime-sharing systems
⚖️

Key Differences

FCFS (First-Come, First-Served) runs processes in the exact order they arrive without interruption. It is easy to implement but can cause long waiting times if a long process arrives first, leading to the "convoy effect".

SJF (Shortest Job First) selects the process with the smallest execution time next. This reduces average waiting time but requires knowing or estimating job lengths beforehand. It can be preemptive (Shortest Remaining Time First) or non-preemptive. However, it risks starving longer jobs if short jobs keep arriving.

Round Robin assigns each process a fixed time slice (quantum) and cycles through them repeatedly. This ensures all processes get CPU time fairly and is ideal for time-sharing systems. It is preemptive and balances responsiveness and throughput but may increase context switching overhead.

⚖️

Code Comparison

Below is a simple Python example demonstrating FCFS scheduling for processes with arrival and burst times.

python
def fcfs_scheduling(processes):
    start_time = 0
    waiting_times = []
    for pid, arrival, burst in processes:
        if start_time < arrival:
            start_time = arrival
        waiting = start_time - arrival
        waiting_times.append((pid, waiting))
        start_time += burst
    return waiting_times

processes = [(1, 0, 5), (2, 1, 3), (3, 2, 8), (4, 3, 6)]
waiting = fcfs_scheduling(processes)
for pid, wait in waiting:
    print(f"Process {pid} waiting time: {wait}")
Output
Process 1 waiting time: 0 Process 2 waiting time: 4 Process 3 waiting time: 6 Process 4 waiting time: 14
↔️

Round Robin Equivalent

This Python code shows a simple Round Robin scheduler using a fixed time quantum.

python
from collections import deque

def round_robin(processes, quantum):
    queue = deque(processes)
    time = 0
    waiting_times = {pid: 0 for pid, _, _ in processes}
    remaining = {pid: burst for pid, _, burst in processes}
    last_time = {pid: 0 for pid, _, _ in processes}

    while queue:
        pid, arrival, burst = queue.popleft()
        if time < arrival:
            time = arrival
        exec_time = min(quantum, remaining[pid])
        waiting_times[pid] += time - last_time[pid]
        time += exec_time
        remaining[pid] -= exec_time
        last_time[pid] = time
        if remaining[pid] > 0:
            queue.append((pid, arrival, burst))
    return waiting_times

processes = [(1, 0, 5), (2, 1, 3), (3, 2, 8), (4, 3, 6)]
waiting = round_robin(processes, quantum=2)
for pid in sorted(waiting):
    print(f"Process {pid} waiting time: {waiting[pid]}")
Output
Process 1 waiting time: 7 Process 2 waiting time: 4 Process 3 waiting time: 13 Process 4 waiting time: 11
🎯

When to Use Which

Choose FCFS when simplicity is key and process arrival order is fair, such as in batch processing without strict timing needs.

Choose SJF when you can estimate job lengths accurately and want to minimize average waiting time, but be cautious of starving longer tasks.

Choose Round Robin for interactive or time-sharing systems where fairness and responsiveness matter, balancing CPU time among all processes with fixed time slices.

Key Takeaways

FCFS is simple but can cause long waits for short jobs behind long ones.
SJF minimizes average waiting time but needs job length knowledge and risks starvation.
Round Robin ensures fairness with time slices, ideal for time-sharing systems.
Use FCFS for simple batch jobs, SJF for known job lengths, and Round Robin for interactive multitasking.
Understanding each algorithm’s trade-offs helps pick the best fit for your system’s needs.