I have created a Round Robin Algorithm with C. The output I get for 5 processes with the same arrival time is correct but incorrect for different arrival times. Below is an example,
RRB.C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
typedef struct Process {
int burst_time;
int arrival_time;
int remaining_time;
double wait_time;
double turnaround_time;
double response_time;
} Process;
Process *processes;
// Function to check if input is a valid non-negative integer
int check_is_int(char input[10]) {
char *endptr;
int num = strtol(input, &endptr, 10);
if ((!isspace(*endptr) && *endptr != 0) || num < 0) {
return -1; // Return -1 for invalid input
}
return num; // Return the integer if input is valid
}
// Function to calculate the response time of each process
void calc_response_time(int i) {
int prev_process_cpu_time = processes[i - 1].burst_time - processes[i - 1].remaining_time;
processes[i].response_time = processes[i - 1].response_time + prev_process_cpu_time;
}
// Function to simulate the execution of a process by the CPU
void execute_process(int i, int time_slice, double *utilised_cpu_time, int *num_processes_completed, double *total_wait_time, double *total_turnaround_time, double *total_response_time) {
if (processes[i].remaining_time > time_slice) {
*utilised_cpu_time += time_slice;
processes[i].remaining_time -= time_slice;
} else if (processes[i].remaining_time <= time_slice) {
*utilised_cpu_time += processes[i].remaining_time;
processes[i].remaining_time = 0;
processes[i].turnaround_time = *utilised_cpu_time;
processes[i].wait_time = *utilised_cpu_time - processes[i].burst_time;
*total_wait_time += processes[i].wait_time;
*total_turnaround_time += processes[i].turnaround_time;
*total_response_time += processes[i].response_time;
(*num_processes_completed)++;
}
}
// Function to print header for process timings
void print_header() {
printf("PROCESS\t\tBurst Times\t\tArrival Times\t\tWaiting Times\t\tTurnaround Times\t\tResponse Times\n");
}
// Function to print timings for an individual process
void print_process_timings(int i, int burst_time, int arrival_time, double wait_time, double turnaround_time, double response_time) {
printf("P%d\t\t\t%d\t\t\t\t%d\t\t\t\t%.0f\t\t\t\t%.0f\t\t\t\t%.0f\n", i + 1, burst_time, arrival_time, wait_time, turnaround_time, response_time);
}
int main(void) {
char input[12];
bool first_run = true;
int num_processes_completed = 0;
double utilised_cpu_time = 0;
double total_wait_time = 0;
double total_turnaround_time = 0;
double total_response_time = 0;
// Print introduction
printf("CPU scheduling method: Round Robin (RR)\n\n");
// Prompt user to enter the number of processes
printf("Enter the number of processes: ");
fgets(input, sizeof(input), stdin);
int num_processes = check_is_int(input); // Check if input is valid
printf("\n");
if (num_processes == -1) { // Check if input is invalid
printf("Please input a valid non-negative integer!\n");
return 1;
}
processes = (Process *)malloc(num_processes * sizeof(Process)); // Allocate memory for processes
if (processes == NULL) { // Check if memory allocation failed
printf("Memory allocation failed!\n");
return 1;
}
// Input burst times and arrival times for each process
for (int i = 0; i < num_processes; i++) {
printf("Enter Burst Time for process %d: ", i + 1);
fgets(input, sizeof(input), stdin);
int burst_time = check_is_int(input); // Check if input is valid
printf("Enter Arrival Time for process %d: ", i + 1);
fgets(input, sizeof(input), stdin);
int arrival_time = check_is_int(input); // Check if input is valid
printf("\n");
if (burst_time < 0 || arrival_time < 0) { // Check if input is invalid
printf("Please input valid non-negative integers!\n");
free(processes);
return 1;
} else {
// Initialize a new process instance
Process p;
p.response_time = 0;
p.arrival_time = arrival_time;
p.turnaround_time = 0;
p.wait_time = 0;
p.burst_time = burst_time;
p.remaining_time = burst_time;
processes[i] = p;
}
}
// Input the size of time slice
printf("Enter the size of time slice: ");
fgets(input, sizeof(input), stdin);
printf("\n");
int time_slice = check_is_int(input); // Check if input is valid
if (time_slice == -1) { // Check if input is invalid
printf("Please input a valid non-negative integer!\n");
free(processes);
return 1;
}
print_header(); // Print header for process timings
// Execute processes and calculate timings
for (int i = 0; i < num_processes; i++) {
if (processes[i].remaining_time != 0) {
execute_process(i, time_slice, &utilised_cpu_time, &num_processes_completed, &total_wait_time, &total_turnaround_time, &total_response_time);
}
if (i > 0 && first_run) {
calc_response_time(i);
if (i == num_processes - 1) {
first_run = false;
}
}
if (i == num_processes - 1 && num_processes_completed < num_processes) {
i = -1; // Reset loop if there are outstanding processes
} else if (num_processes_completed == num_processes) {
break; // Exit loop if all processes are completed
}
}
// Print timings for each process
for (int i = 0; i < num_processes; i++) {
print_process_timings(i, processes[i].burst_time, processes[i].arrival_time, processes[i].wait_time, processes[i].turnaround_time, processes[i].response_time);
}
printf("\n");
// Print average waiting time, turnaround time, and response time
printf("Average Waiting Time: %.1f\n", total_wait_time / (double)num_processes);
printf("Average Turnaround Time: %.1f\n", total_turnaround_time / (double)num_processes);
printf("Average Response Time: %.1f\n", total_response_time / (double)num_processes);
free(processes); // Free allocated memory for processes
return 0;
}
Output for Different Arrival Time
CPU scheduling method: Round Robin (RR)
Enter the number of processes: 5
Enter Burst Time for process 1: 45
Enter Arrival Time for process 1: 0
Enter Burst Time for process 2: 90
Enter Arrival Time for process 2: 5
Enter Burst Time for process 3: 70
Enter Arrival Time for process 3: 8
Enter Burst Time for process 4: 38
Enter Arrival Time for process 4: 15
Enter Burst Time for process 5: 55
Enter Arrival Time for process 5: 20
Enter the size of time slice: 10
PROCESS Burst Times Arrival Times Waiting Times Turnaround Times Response Times
P1 45 0 158 203 0
P2 90 5 208 298 10
P3 70 8 208 278 20
P4 38 15 150 188 30
P5 55 20 203 258 40
Average Waiting Time: 185.4
Average Turnaround Time: 245.0
Average Response Time: 20.0
However, according to https://process-scheduling-solver.boonsuen.com/, the average waiting time and average turnaround time are 173.2 and 232.8 respectively, which differs from my output. I've tried to tweak my code but am still unable to figure out why my average waiting time and average turnaround time. Please help to correct my code if there's any discrepancy. Thanks.
This is a bit screwy. I'm not sure I have a definitive answer for this.
The TL;DR is that I think the website may be off (more on that later).
However, there are issues with your code ...
Dhad an arrival time of T50 instead of T15double--intis fine.utilised_cpu_time,total_turnaround_time, etc). The code can be simplified by putting these in astructand just passing around a single pointer to thestruct.processes[i].whateverinstead of defining a pointer (e.g.)Process *p = &processes[i];and then usingp->whatever.i = -1;in the middle to start over. This a bit hacky. The clean(er) way is to have nested loops.Here is input to the website:
Here is the website's Gantt chart:
The issue: [I could be completely wrong about this, but] At time T30, process D is runnable (and so is E), so the sequence should start out with:
But, the gantt chart's sequence is:
In other words, the website did not think that
Dwas runnable.So, maybe, the website's notion of "arrival time" is different (or it has a bug of some sort?).
Here is the website's final result
This doesn't match up too well with either the results of your program (or your program as restructured by me).
Based on all the issues I mentioned above, I had to restructure your code [quite] a bit.
calc_response_timeIn the code above, I've used
cppconditionals to denote old vs. new code:Note: this can be cleaned up by running the file through
unifdef -kHere is the program input file I used:
Here is the program output: