Write the program to simulate Round Robin (RR) scheduling. The arrival time and first CPU-burst for different n number of processes should be input to the algorithm. Also give the time quantum as input. Assume the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The output should give Gantt chart, turnaround time and waiting time for each process. Also find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Structure to represent a process
typedef struct Process {
int pid; // Process ID
int arrival; // Arrival time
int burst; // CPU burst time
int remaining; // Remaining burst time
int waiting; // Waiting time
int turnaround; // Turnaround time
} Process;
// Function to swap two processes
void swap(Process *a, Process *b) {
Process temp = *a;
*a = *b;
*b = temp;
}
// Function to perform Round Robin (RR) scheduling
void roundRobinScheduling(Process *processes, int n, int timeQuantum) {
// Sort processes based on arrival time
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival > processes[j + 1].arrival) {
swap(&processes[j], &processes[j + 1]);
}
}
}
// Initialize variables
int currentTime = 0;
int totalWaiting = 0;
int totalTurnaround = 0;
int completed = 0;
printf("\nGantt Chart:\n");
printf("--------------------------------------------------\n");
printf("Time |");
for (int i = 0; i < n; i++) {
printf(" P%d |", processes[i].pid);
}
printf("\n--------------------------------------------------\n");
// Perform scheduling
while (completed < n) {
for (int i = 0; i < n; i++) {
if (processes[i].remaining > 0) {
// Execute the process for the time quantum or until it completes
int timeToExecute = (processes[i].remaining < timeQuantum) ? processes[i].remaining : timeQuantum;
processes[i].remaining -= timeToExecute;
currentTime += timeToExecute;
// Print Gantt chart
printf("%3d |", currentTime);
// Check if the process has completed
if (processes[i].remaining == 0) {
// Calculate turnaround time
processes[i].turnaround = currentTime - processes[i].arrival;
totalTurnaround += processes[i].turnaround;
// Calculate waiting time
processes[i].waiting = processes[i].turnaround - processes[i].burst;
totalWaiting += processes[i].waiting;
completed++;
}
}
}
}
// Print turnaround time and waiting time for each process
printf("\nProcess | Turnaround Time | Waiting Time\n");
printf("----------------------------------------\n");
for (int i = 0; i < n; i++) {
printf(" P%d %5d %5d\n", processes[i].pid, processes[i].turnaround, processes[i].waiting);
}
// Print average turnaround time and waiting time
printf("\nAverage Turnaround Time: %.2f\n", (float)totalTurnaround / n);
printf("Average Waiting Time: %.2f\n", (float)totalWaiting / n);
}
int main() {
srand(time(NULL));
int n, timeQuantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &timeQuantum);
// Initialize an array of processes
Process processes[n];
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
printf("Enter arrival time and burst time for P%d: ", i + 1);
scanf("%d %d", &processes[i].arrival, &processes[i].burst);
processes[i].remaining = processes[i].burst;
}
// Perform Round Robin scheduling
roundRobinScheduling(processes, n, timeQuantum);
return 0;
}
Comments
Post a Comment