Simple Simulation of FCFS and Round Robin Scheduling in OS with Code

By Vandu
May 7, 2025

Follow us on


Learn how to simulate basic process scheduling algorithms like First Come First Serve (FCFS) and Round Robin in Operating Systems with step-by-step code and easy explanations.

Simple Simulation of FCFS and Round Robin Scheduling in OS with Code

🖥️ Write a Simple Program to Simulate Process Scheduling (FCFS and Round Robin)

Process scheduling is a crucial part of an operating system. It decides which process will use the CPU next when multiple processes are ready to run. In this article, we will simulate First Come First Serve (FCFS) and Round Robin (RR) scheduling algorithms using C++. Each step is explained in very simple language.


📌 What is Process Scheduling?

When multiple processes are ready to execute, the operating system needs a way to decide which one goes first. This is called process scheduling. It improves CPU utilization and provides fairness to all processes.


🔹 First Come First Serve (FCFS) Scheduling

👉 Concept:

In FCFS, the process that arrives first is executed first. It works like a queue in a bank – whoever comes first gets served first.


🧠 How FCFS Works (Step-by-Step):

  1. Input: Number of processes, arrival time, and burst time for each.

  2. Sort: Sort the processes by arrival time.

  3. Calculate: Start time, completion time, turnaround time, and waiting time.

  4. Output: Display the scheduling results.


💻 FCFS Scheduling Program in C++:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Process {
    int pid, arrivalTime, burstTime, startTime, completionTime, turnaroundTime, waitingTime;
};

bool compareArrival(Process a, Process b) {
    return a.arrivalTime < b.arrivalTime;
}

int main() {
    int n;
    cout << "Enter number of processes: ";
    cin >> n;
    vector<Process> p(n);

    // Input
    for (int i = 0; i < n; i++) {
        p[i].pid = i + 1;
        cout << "Enter Arrival Time and Burst Time for Process " << p[i].pid << ": ";
        cin >> p[i].arrivalTime >> p[i].burstTime;
    }

    // Sort by Arrival Time
    sort(p.begin(), p.end(), compareArrival);

    int currentTime = 0;
    for (int i = 0; i < n; i++) {
        if (currentTime < p[i].arrivalTime)
            currentTime = p[i].arrivalTime;

        p[i].startTime = currentTime;
        p[i].completionTime = currentTime + p[i].burstTime;
        p[i].turnaroundTime = p[i].completionTime - p[i].arrivalTime;
        p[i].waitingTime = p[i].startTime - p[i].arrivalTime;
        currentTime = p[i].completionTime;
    }

    // Output
    cout << "\nPID\tAT\tBT\tST\tCT\tTAT\tWT\n";
    for (auto pr : p) {
        cout << pr.pid << "\t" << pr.arrivalTime << "\t" << pr.burstTime << "\t"
             << pr.startTime << "\t" << pr.completionTime << "\t"
             << pr.turnaroundTime << "\t" << pr.waitingTime << "\n";
    }

    return 0;
}

🧾 Output Example:

Enter number of processes: 3
Enter Arrival Time and Burst Time for Process 1: 0 5
Enter Arrival Time and Burst Time for Process 2: 1 3
Enter Arrival Time and Burst Time for Process 3: 2 8

PID	AT	BT	ST	CT	TAT	WT
1	0	5	0	5	5	0
2	1	3	5	8	7	4
3	2	8	8	16	14	6

🔹 Round Robin Scheduling

👉 Concept:

In Round Robin, each process gets a fixed time slot (quantum). If it doesn’t finish in that time, it goes back in the queue.


🧠 How Round Robin Works (Step-by-Step):

  1. Input: Arrival time, burst time, and time quantum.

  2. Use Queue: A queue is used to manage process execution.

  3. Rotate: Each process gets executed for time quantum. If not done, it goes back.

  4. Calculate: Completion, turnaround, and waiting times.


💻 Round Robin Scheduling Program in C++:

#include <iostream>
#include <queue>
using namespace std;

struct Process {
    int pid, arrivalTime, burstTime, remainingTime, completionTime;
};

int main() {
    int n, quantum;
    cout << "Enter number of processes: ";
    cin >> n;
    cout << "Enter Time Quantum: ";
    cin >> quantum;

    Process p[n];
    queue<int> q;
    bool inQueue[n] = {false};

    for (int i = 0; i < n; i++) {
        p[i].pid = i + 1;
        cout << "Enter Arrival and Burst Time for Process " << p[i].pid << ": ";
        cin >> p[i].arrivalTime >> p[i].burstTime;
        p[i].remainingTime = p[i].burstTime;
    }

    int time = 0, completed = 0;
    q.push(0);
    inQueue[0] = true;

    while (completed < n) {
        int i = q.front(); q.pop();
        if (p[i].remainingTime > 0) {
            int execTime = min(quantum, p[i].remainingTime);
            time += execTime;
            p[i].remainingTime -= execTime;

            // Add new arrivals to the queue
            for (int j = 0; j < n; j++) {
                if (!inQueue[j] && p[j].arrivalTime <= time && p[j].remainingTime > 0) {
                    q.push(j);
                    inQueue[j] = true;
                }
            }

            // Requeue if not done
            if (p[i].remainingTime > 0) {
                q.push(i);
            } else {
                p[i].completionTime = time;
                completed++;
            }

            if (q.empty()) {
                for (int j = 0; j < n; j++) {
                    if (!inQueue[j] && p[j].remainingTime > 0) {
                        q.push(j);
                        inQueue[j] = true;
                        break;
                    }
                }
            }
        }
    }

    // Calculate Turnaround Time and Waiting Time
    cout << "\nPID\tAT\tBT\tCT\tTAT\tWT\n";
    for (int i = 0; i < n; i++) {
        int tat = p[i].completionTime - p[i].arrivalTime;
        int wt = tat - p[i].burstTime;
        cout << p[i].pid << "\t" << p[i].arrivalTime << "\t" << p[i].burstTime << "\t"
             << p[i].completionTime << "\t" << tat << "\t" << wt << "\n";
    }

    return 0;
}

🧾 Output Example:

Enter number of processes: 3
Enter Time Quantum: 2
Enter Arrival and Burst Time for Process 1: 0 5
Enter Arrival and Burst Time for Process 2: 1 3
Enter Arrival and Burst Time for Process 3: 2 6

PID	AT	BT	CT	TAT	WT
1	0	5	13	13	8
2	1	3	9	8	5
3	2	6	15	13	7

🧠 Final Thoughts

  • FCFS is simple but can cause longer wait times.

  • Round Robin gives better response time and fairness.

  • Understanding these algorithms helps you grasp how OS handles multitasking.


© 2025 Pay18News. All rights reserved.