Write the simulation program for Round Robin scheduling for given time quantum. The arrival time and first CPU-burst of different jobs should be input to the system. Accept no. of Processes, arrival time and burst time. The output should give the Gantt chart, turnaround time and waiting time for each process. Also display the average turnaround time and average waiting time.
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 100
// Structure to hold process details
typedef struct {
int id; // Process ID
int arrivalTime; // Arrival Time
int burstTime; // Burst Time
int remainingTime; // Remaining Time
int waitingTime; // Waiting Time
int turnaroundTime; // Turnaround Time
} Process;
// Function to simulate Round Robin Scheduling
void roundRobinScheduling(Process processes[], int n, int quantum) {
int time = 0; // Current time
int remainingProcesses = n;
int queue[MAX_PROCESSES];
int front = 0, rear = 0;
// Initialize queue with processes that have arrived
for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;
}
printf("Gantt Chart:\n");
printf("Time\t");
// Main loop for Round Robin Scheduling
while (remainingProcesses > 0) {
int i;
for (i = 0; i < n; i++) {
if (processes[i].remainingTime > 0) {
// Process not completed yet
if (processes[i].arrivalTime <= time) {
int execTime = (processes[i].remainingTime > quantum) ? quantum : processes[i].remainingTime;
processes[i].remainingTime -= execTime;
time += execTime;
// Print the Gantt Chart
printf("%d-%d\t", time - execTime, time);
// Update waiting time for remaining processes
for (int j = 0; j < n; j++) {
if (processes[j].remainingTime > 0 && processes[j].arrivalTime <= time) {
processes[j].waitingTime += execTime;
}
}
// If process is completed
if (processes[i].remainingTime == 0) {
processes[i].turnaroundTime = time - processes[i].arrivalTime;
remainingProcesses--;
}
}
}
}
}
printf("\n");
// Print results
printf("Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
float totalWaitingTime = 0;
float totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id,
processes[i].arrivalTime,
processes[i].burstTime,
processes[i].waitingTime,
processes[i].turnaroundTime);
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}
// Calculate and display averages
printf("Average Waiting Time: %.2f\n", totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / n);
}
int main() {
int n, quantum;
Process processes[MAX_PROCESSES];
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
processes[i].id = i + 1;
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
// Run Round Robin Scheduling
roundRobinScheduling(processes, n, quantum);
return 0;
}
0 Comments