Cover Image for Round Robin Program in C with Output
112 views

Round Robin Program in C with Output

Round Robin scheduling is a preemptive scheduling algorithm commonly used in operating systems. It allocates a fixed time slice (quantum) to each process in a circular order, allowing each process to execute for a specified time before moving to the next process. Here’s a simple C program that demonstrates Round Robin scheduling with a few sample processes:

C
#include <stdio.h>
#include <stdbool.h>

// Structure to represent a process
struct Process {
    int pid;     // Process ID
    int burst;   // Burst time
    int remainingBurst; // Remaining burst time
};

// Function to perform Round Robin scheduling
void roundRobin(struct Process processes[], int n, int quantum) {
    int currentTime = 0;
    int completed = 0;

    while (completed < n) {
        for (int i = 0; i < n; i++) {
            if (processes[i].remainingBurst > 0) {
                // Execute the process for the quantum or its remaining burst time, whichever is smaller
                int executionTime = (processes[i].remainingBurst < quantum) ? processes[i].remainingBurst : quantum;
                currentTime += executionTime;
                processes[i].remainingBurst -= executionTime;

                printf("Process %d executed for %d units. Current time: %d\n", processes[i].pid, executionTime, currentTime);

                if (processes[i].remainingBurst == 0) {
                    completed++;
                }
            }
        }
    }
}

int main() {
    int n, quantum;

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    printf("Enter the time quantum: ");
    scanf("%d", &quantum);

    struct Process processes[n];

    // Initialize processes
    for (int i = 0; i < n; i++) {
        processes[i].pid = i + 1;
        printf("Enter burst time for process %d: ", i + 1);
        scanf("%d", &processes[i].burst);
        processes[i].remainingBurst = processes[i].burst;
    }

    printf("\nRound Robin Scheduling:\n");
    roundRobin(processes, n, quantum);

    return 0;
}

In this program:

  • We define a struct Process to represent a process with a process ID (pid), burst time (burst), and remaining burst time (remainingBurst).
  • The roundRobin function simulates the Round Robin scheduling algorithm. It iterates through the processes, allowing each process to execute for a specified quantum or until it completes.
  • In the main function, we input the number of processes, time quantum, and burst times for each process.
  • The program then performs Round Robin scheduling and prints the execution details for each process.

This program demonstrates Round Robin scheduling with a small set of sample processes. You can adjust the number of processes and their burst times to see how the algorithm works for different scenarios.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS