Quantcast
Channel: Active questions tagged operating-system+scheduling - Stack Overflow
Viewing all articles
Browse latest Browse all 209

Round Robin Implementation with Different Arrival Time

$
0
0

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 integerint 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 processvoid 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 CPUvoid 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 timingsvoid 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 processvoid 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: 5Enter Burst Time for process 1: 45Enter Arrival Time for process 1: 0Enter Burst Time for process 2: 90Enter Arrival Time for process 2: 5Enter Burst Time for process 3: 70Enter Arrival Time for process 3: 8Enter Burst Time for process 4: 38Enter Arrival Time for process 4: 15Enter Burst Time for process 5: 55Enter Arrival Time for process 5: 20Enter the size of time slice: 10PROCESS                 Burst Times                     Arrival Times                   Waiting Times                   Turnaround Times                        Response TimesP1                      45                              0                               158                             203                             0P2                      90                              5                               208                             298                             10P3                      70                              8                               208                             278                             20P4                      38                              15                              150                             188                             30P5                      55                              20                              203                             258                             40Average Waiting Time: 185.4Average Turnaround Time: 245.0Average 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.


Viewing all articles
Browse latest Browse all 209

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>