121k views
1 vote
Write a c program for round robin. By finding the completion time, average turnaround time and burst time.

Try to run the program with different quantum number 1,2,3,5,6,7,8 without key-in 15 arrival time and burst time.

The program should save the 15 arrival and burst time.(not key-in once run program)

This is the arrival time and burst time for 15 jobs lists.

Arrival time------------burst time

1 --------4

2---------5

2---------6

4----------9

7----------12

8----------16

9------------23

4------------19

5-------------26

8--------30

9-------16

11-------35

14-------49

21---------45

24--------60

User Sean Hill
by
7.8k points

1 Answer

2 votes

Here's a C program that implements the Round Robin scheduling algorithm and calculates the completion time, average turnaround time, and burst time. The program uses the predefined arrival times and burst times for 15 jobs, and it allows the user to run the program with different quantum numbers without entering the arrival and burst times each time.

```c

#include <stdio.h>

#define MAX_JOBS 15

struct Job {

int arrivalTime;

int burstTime;

int remainingTime;

};

void roundRobinScheduling(struct Job jobs[], int numJobs, int quantum) {

int completionTime[MAX_JOBS];

int waitingTime[MAX_JOBS];

int turnaroundTime[MAX_JOBS];

int totalWaitingTime = 0;

int totalTurnaroundTime = 0;

int time = 0;

int i;

for (i = 0; i < numJobs; i++) {

jobs[i].remainingTime = jobs[i].burstTime;

}

while (1) {

int allJobsCompleted = 1;

for (i = 0; i < numJobs; i++) {

if (jobs[i].remainingTime > 0) {

allJobsCompleted = 0;

if (jobs[i].remainingTime > quantum) {

time += quantum;

jobs[i].remainingTime -= quantum;

} else {

time += jobs[i].remainingTime;

waitingTime[i] = time - jobs[i].burstTime - jobs[i].arrivalTime;

turnaroundTime[i] = time - jobs[i].arrivalTime;

totalWaitingTime += waitingTime[i];

totalTurnaroundTime += turnaroundTime[i];

jobs[i].remainingTime = 0;

completionTime[i] = time;

printf("Job %d completed. Completion Time: %d, Waiting Time: %d, Turnaround Time: %d\\",

i + 1, completionTime[i], waitingTime[i], turnaroundTime[i]);

}

}

}

if (allJobsCompleted) {

break;

}

}

double averageWaitingTime = (double)totalWaitingTime / numJobs;

double averageTurnaroundTime = (double)totalTurnaroundTime / numJobs;

printf("Average Waiting Time: %.2lf\\", averageWaitingTime);

printf("Average Turnaround Time: %.2lf\\", averageTurnaroundTime);

}

int main() {

struct Job jobs[MAX_JOBS] = {

{1, 4}, {2, 5}, {2, 6}, {4, 9}, {7, 12},

{8, 16}, {9, 23}, {4, 19}, {5, 26}, {8, 30},

{9, 16}, {11, 35}, {14, 49}, {21, 45}, {24, 60}

};

int numJobs = sizeof(jobs) / sizeof(jobs[0]);

int quantumNumbers[] = {1, 2, 3, 5, 6, 7, 8};

int numQuantumNumbers = sizeof(quantumNumbers) / sizeof(quantumNumbers[0]);

for (int i = 0; i < numQuantumNumbers; i++) {

int quantum = quantumNumbers[i];

printf("\\Round Robin Scheduling with Quantum Number: %d\\", quantum);

roundRobinScheduling(jobs, numJobs, quantum);

}

return 0;

}

```

In this program, the `jobs` array contains the predefined arrival times and burst times for 15 jobs. The program prompts the user to run the program with different quantum numbers: 1, 2, 3, 5, 6, 7, and 8. The completion time, waiting time, and turnaround time are calculated for each quantum number. Finally, the program displays the average waiting time and average turnaround time for each run.

Please note that the program assumes that the arrival times and burst times are predefined and stored in the `jobs` array. You can modify the code to change the values or read them from a file if needed.

User Choeger
by
8.7k points