CPU Scheduling Algorithms in C: Round Robin, FCFS and SJF

A round robin scheduling program in C gives each process a fixed CPU time slice called a time quantum, cycling through all processes in order so no single process monopolises the CPU. It is one of three core scheduling algorithms — alongside FCFS and SJF — covered in every Operating Systems lab course.

  • FCFS is the simplest: processes execute in arrival order, no preemption.
  • SJF picks the shortest burst time next, cutting average waiting time significantly.
  • Round Robin adds a time quantum so no single process starves the CPU.
  • Waiting time and turnaround time are the two core metrics you calculate for every algorithm.
  • The degree of multiprogramming tells you how many processes the OS keeps in memory simultaneously.

FCFS Scheduling: First Come First Serve Program in C

FCFS is the easiest algorithm to implement. Processes are sorted by arrival time and executed one after another without interruption. There is no priority, no preemption, just a simple queue.

Here is the logic you would code in a first come first serve scheduling program in C:

  1. Read n processes with their arrival time and burst time.
  2. Sort processes by arrival time (ascending).
  3. Set completion_time[0] = arrival_time[0] + burst_time[0].
  4. For each subsequent process, completion_time[i] = max(completion_time[i-1], arrival_time[i]) + burst_time[i].
  5. Calculate turnaround time = completion time – arrival time.
  6. Calculate waiting time = turnaround time – burst time.

The biggest weakness of FCFS is the convoy effect. If a long process arrives first, every shorter process behind it waits unnecessarily. According to Silberschatz, Galvin and Gagne’s Operating System Concepts (10th edition), FCFS can produce average waiting times several times higher than Round Robin under mixed workloads — a finding that directly explains why interactive systems abandoned FCFS decades ago.

FCFS Worked Example

Process Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
P1 0 5 5 5 0
P2 1 3 8 7 4
P3 2 8 16 14 6
P4 3 2 18 15 13
Average 10.25 5.75

P4 has a burst time of just 2 units but waits 13 units because P3 arrived before it. That is the convoy effect in action. This exact example appears regularly in GATE CS papers and in OS lab viva questions at universities including Anna University, VTU and Mumbai University, where FCFS is typically the first algorithm students implement.

SJF Scheduling Program in C and How It Differs from FCFS

SJF (Shortest Job First) selects the process with the smallest burst time from the ready queue. It can be non-preemptive (runs to completion once started) or preemptive (also called Shortest Remaining Time First, SRTF). Non-preemptive SJF is what most university labs ask you to implement first.

The core difference from FCFS: SJF actively reorders the ready queue by burst time, not by arrival time. This minimises average waiting time. According to Silberschatz, Galvin and Gagne’s Operating System Concepts (10th edition), SJF is provably optimal for minimising average waiting time among all non-preemptive scheduling algorithms — making it the theoretical benchmark every other algorithm is measured against.

SJF Scheduling Program in C: Logic

  1. Read n processes with arrival and burst times.
  2. At each scheduling decision point, pick the process with the smallest burst time among those that have already arrived.
  3. Mark it complete, record completion time.
  4. Repeat until all processes finish.
  5. Compute turnaround time and waiting time as with FCFS.

The main practical problem with the SJF scheduling program in C is that the OS cannot always know a process’s burst time in advance. That is why Round Robin is preferred in real interactive systems. The SJF implementation you write in lab assumes burst times are known, which is a deliberate simplification for learning purposes.

If you are serious about systems programming and want to move beyond lab assignments, check out the top skills every college student must learn before graduation to see how OS concepts fit into a broader technical skill set.

Round Robin Scheduling Program in C: Code Logic and Time Quantum

Round Robin is the algorithm that makes time-sharing systems work. Every process gets a fixed CPU slice called the time quantum (typically 10 to 100 milliseconds in real systems). Once the quantum expires, the running process goes back to the end of the ready queue and the next process gets its turn. Writing a correct round robin scheduling program in C requires a circular queue and careful tracking of remaining burst time for each process.

Round Robin Scheduling Program in C: Step-by-Step Logic

  1. Read n processes and a time quantum q.
  2. Initialise remaining_time[i] = burst_time[i] for all processes.
  3. Use a loop: pick the next process in queue order.
  4. If remaining_time[i] > q, run it for q units, subtract q from remaining time, move it to the back of the ready queue.
  5. If remaining_time[i] <= q, run it to completion, record completion time.
  6. Track current time throughout. When a process finishes, turnaround_time = completion_time - arrival_time and waiting_time = turnaround_time - burst_time.

Choosing the Right Time Quantum

Too small a quantum means constant context switching overhead. Too large and the round robin scheduling program in C behaves like FCFS. Research by Tanenbaum in Modern Operating Systems (4th edition) shows that a time quantum of 20 to 50 milliseconds works well for most interactive workloads, balancing responsiveness against context switch cost. For your C lab program, a quantum of 2 or 4 units on small datasets is standard practice.

Round Robin Worked Example (Time Quantum = 2)

Process Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
P1 0 5 13 13 8
P2 1 3 10 9 6
P3 2 8 18 16 8
P4 3 2 8 5 3
Average 10.75 6.25

Notice that P4’s waiting time drops from 13 units in FCFS to just 3 units with the round robin scheduling program in C. That is the fairness guarantee this algorithm provides. No process gets completely starved, even if turnaround time for longer processes increases slightly.

Degree of Multiprogramming and Why It Matters

The degree of multiprogramming refers to the number of processes kept in memory at the same time. A higher degree means the CPU spends less time idle because there is always a ready process to run. Round Robin directly supports a higher degree of multiprogramming because it never lets one process monopolise the CPU through a long CPU burst.

According to NASSCOM’s India Tech Talent Report 2024, Operating Systems and systems programming skills rank among the top 10 technical competencies sought by Indian IT employers, with over 1.5 lakh students appearing for GATE CS annually (IIT data). OS scheduling concepts including degree of multiprogramming appear in GATE CS every year, and students at IITs, NITs and state engineering universities are expected to calculate waiting and turnaround times for all three algorithms from memory.

Understanding these concepts also helps when you are thinking about career options in 2026 for students and professionals in systems programming, cloud infrastructure and embedded development, all of which require solid OS fundamentals.

Algorithm Comparison at a Glance

Feature FCFS SJF (Non-Preemptive) Round Robin
Preemptive? No No Yes
Starvation? No Yes (long processes) No
Optimal waiting time? No Yes (minimum avg) No
Response time Poor Moderate Best
Context switching overhead None None High if quantum is small
Implementation complexity Low Medium Medium
Best use case Batch systems Batch / known workloads Interactive / time-sharing

If you are preparing for university exams, GATE, or technical interviews at companies like TCS, Infosys or Wipro, you will need to write all three algorithms from scratch and calculate metrics by hand. Practise with at least five different process sets before your exam.

For students who want to go further with systems programming or move into tech roles, the entry-level jobs available for teens and fresh graduates page covers which roles actually use these skills on the job. If you are still deciding on a direction, the guide on how to choose the right career path after graduation in 2026 is worth reading before you commit to a specialisation.

Platforms listed on free online learning platforms for students in 2025 offer OS courses where you can practise these programs in a browser-based compiler. That is a practical option if your college lab hours are limited. You can also explore online courses for high-salary jobs in 2025 to see how OS and systems skills compare against other technical tracks.

Once you have all three algorithms working, extend your round robin scheduling program in C to handle processes with different arrival times and variable time quanta. Then implement preemptive SJF (SRTF) and compare its output with your Round Robin results on the same dataset. That comparison exercise alone will deepen your understanding of CPU scheduling more than re-reading notes ever will.

For structured guidance and industry-recognised credentials, explore certification courses at 3University covering Cybersecurity, AI, Blockchain and Web3 — built for students and working professionals who want practical, job-ready skills. The OS fundamentals you have just covered are the foundation that makes advanced systems and security work make sense.

Frequently Asked Questions

What is round robin scheduling in an operating system?

Round Robin scheduling assigns a fixed time slice called a time quantum to each process in a circular order. When a process’s quantum expires, it returns to the end of the ready queue and the next process runs. It prevents starvation, supports a high degree of multiprogramming, and is the standard CPU scheduling algorithm for interactive and time-sharing operating systems.

How do you write an FCFS scheduling program in C?

Sort processes by arrival time, then execute them sequentially. Track the current time and set each process’s completion time as the maximum of the current time and its arrival time, plus its burst time. Calculate turnaround time as completion minus arrival, and waiting time as turnaround minus burst time. A first come first serve scheduling program in C needs no complex data structures, making it the easiest of the three to implement.

How is SJF different from FCFS?

FCFS runs processes in arrival order regardless of how long they take. SJF picks the process with the smallest burst time from the ready queue at each decision point. This produces a lower average waiting time than FCFS. The trade-off is potential starvation: a long process can wait indefinitely if shorter processes keep arriving, something FCFS never causes.

What is the best time quantum for a round robin scheduling program in C?

There is no single best value. Tanenbaum’s research in Modern Operating Systems suggests 20 to 50 milliseconds for real interactive systems. In lab programs with small datasets, a quantum of 2 to 4 time units is standard. A quantum that is too small increases context switching overhead; a quantum that is too large makes Round Robin behave like FCFS, defeating its purpose.

Which CPU scheduling algorithm has the minimum average waiting time?

SJF (Shortest Job First) in its non-preemptive form is provably optimal for minimising average waiting time among non-preemptive algorithms, as established in Silberschatz, Galvin and Gagne’s Operating System Concepts. Preemptive SJF (SRTF) achieves the minimum average waiting time overall. Round Robin trades some waiting time efficiency for fairness and better response time in interactive systems.

How do you calculate waiting and turnaround time?

Turnaround time equals completion time minus arrival time. Waiting time equals turnaround time minus burst time. These two formulas apply to FCFS, SJF and Round Robin equally. Average waiting time is the sum of all individual waiting times divided by the number of processes. This metric is the primary benchmark for comparing CPU scheduling algorithm performance in OS textbooks and GATE exam problems.

What is the degree of multiprogramming in an OS?

The degree of multiprogramming is the number of processes loaded into main memory simultaneously. A higher degree keeps the CPU busier because there is always a ready process to schedule. Round Robin and preemptive algorithms support a higher degree than FCFS because they do not let one process block the CPU for a long CPU burst. It is a key concept in OS design and GATE CS preparation across Indian engineering universities.

Last updated: September 2026. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles