Write program to implement Round Robin scheduling algorithm in Operating System -KML Tutorial

Aim :-
Write program to implement Round Robin scheduling algorithm.
Program :-
1.    #include<iostream>
2.    using namespace std;
3.    void findWaitingTime(int processes[], int n,
4.                int bt[], int wt[], int quantum)
5.    {
6.         int rem_bt[n];
7.        for (int i = 0 ; i < n ; i++)
8.            rem_bt[i] = bt[i];
9.        int t = 0;
10.        while (1)
11.        {
12.            bool done = true;
13.            for (int i = 0 ; i < n; i++)
14.            {
15.                if (rem_bt[i] > 0)
16.                {
17.                    done = false;
18.                    if (rem_bt[i] > quantum)
19.                    {
20.                        t += quantum;
21.                        rem_bt[i] -= quantum;
22.                    }
23.                    else
24.                    {
25.                        t = t + rem_bt[i];
26.                        wt[i] = t - bt[i];
27.                        rem_bt[i] = 0;
28.                    }
29.                }
30.            }
31.            if (done == true)
32.            break;
33.        }
34.    }
35.    void findTurnAroundTime(int processes[], int n,
36.                            int bt[], int wt[], int tat[])
37.    {
38.        for (int i = 0; i < n ; i++)
39.            tat[i] = bt[i] + wt[i];
40.    }
41.    void findavgTime(int processes[], int n, int bt[],
42.                                        int quantum)
43.    {
44.        int wt[n], tat[n], total_wt = 0, total_tat = 0;
45.        findWaitingTime(processes, n, bt, wt, quantum);
46.        findTurnAroundTime(processes, n, bt, wt, tat);
47.        cout << "PN\t "<< " \tBT "
48.            << "  WT " << " \tTAT\n";
49.        for (int i=0; i<n; i++){
50.            total_wt = total_wt + wt[i];
51.            total_tat = total_tat + tat[i];
52.            cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
53.                << wt[i] <<"\t\t " << tat[i] <<endl;
54.        }
55.        cout << "Average waiting time = "
56.            << (float)total_wt / (float)n;
57.        cout << "\nAverage turn around time = "
58.            << (float)total_tat / (float)n;
59.    }
60.    int main(){
61.        int processes[] = { 1, 2, 3};
62.        int n = sizeof processes / sizeof processes[0];
63.        int burst_time[] = {10, 5, 8};
64.        int quantum = 2;
65.        findavgTime(processes, n, burst_time, quantum);
66.        return 0;
67.    }
 

Outpute:-
    PN                BT           WT         TAT
     1                  10            13             23
     2                   5             10             15
     3                   8             13             21
Average waiting time = 12
Average turn around time = 19.6667
Process returned 0 (0x0)   execution time : 37.708 s

            ***Thank You***

Comments

Post a Comment