Skip to content

Latest commit

 

History

History
143 lines (138 loc) · 3.17 KB

extra.md

File metadata and controls

143 lines (138 loc) · 3.17 KB
Robin round CPU scheduling algorithm C++ code
#include <bits/stdc++.h>
using namespace std;

struct process
{
    int p_id;
    int at;
    int bt;
    int wt;
    int tat;
    int rem_bt;
};
bool compareAT(const process &a, const process &b)
{
    return a.at < b.at;
}
bool compareSerial(const process &a, const process &b)
{
    return a.p_id < b.p_id;
}
int main()
{
    int n, cur_process;
    cout << "\n\n\nEnter total number of processes: ";
    cin >> n;
    vector<process> ps(n);
    for (int i = 0; i < n; i++)
    {
        cout << "Enter arival time of P" << i + 1 << ": ";
        cin >> ps[i].at;
        ps[i].p_id = i;
    }cout<<endl;
    for(int i=0; i<n; i++)
    {
        cout << "Enter burst time of P" << i + 1 << ": ";
        cin >> ps[i].bt;
        ps[i].rem_bt = ps[i].bt;

    }
    int time_quantum;
    cout << "Enter time quantum: ";
    cin >> time_quantum;
    sort(ps.begin(), ps.end(),compareAT);
    queue<int> q;
    q.push(0);
    vector<bool> visited(n, false);
    visited[0] = 1;
    int cur_t = 0, completed = 0;
    int total_tat = 0, total_wt = 0;
    vector<pair<int, int>> gantt_chart;
    while (completed != n)
    {
        int cur = q.front();
        q.pop();
        if (cur_t < ps[cur].at)
        {
            cur_t = ps[cur].at;
        }
        gantt_chart.push_back({ps[cur].p_id, cur_t});
        if (ps[cur].rem_bt > time_quantum)
        {
            ps[cur].rem_bt -= time_quantum;
            cur_t += time_quantum;
        }
        else
        {
            cur_t += ps[cur].rem_bt;
            ps[cur].rem_bt = 0;
            completed++;

            ps[cur].tat = cur_t - ps[cur].at;
            ps[cur].wt = ps[cur].tat - ps[cur].bt;
            total_tat += ps[cur].tat;
            total_wt += ps[cur].wt;
        }
        for (int i = 1; i < n; i++)
        {
            if (cur_t >= ps[i].at and
                ps[i].rem_bt == ps[i].bt and visited[i] == false)
            {
                visited[i] = true;
                q.push(i);
            }
        }
        if (ps[cur].rem_bt > 0)
        {
            q.push(cur);
        }
        if (q.empty())
        {
            for (int i = 1; i < n; i++)
            {
                if (visited[i] == false)
                {
                    q.push(i);
                    visited[i] = 1;
                    break;
                }
            }
        }
    }
    cout << "\n\nGantt chart: " << endl;
    for (auto p : gantt_chart)
    {
        cout << "(P" << p.first + 1 << "," << p.second << "),  ";
    }
    cout << "End at " << cur_t << endl;
    sort(ps.begin(), ps.end(),compareSerial);
    cout << "   P\t   At\t   Bt\t   Wt\t   Tat\t" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << "   P" << ps[i].p_id + 1 << "\t   " << ps[i].at << "\t   " << ps[i].bt
             << "\t   " << ps[i].wt << "\t   " << ps[i].tat << "\t" << endl;
    }
    cout << fixed << setprecision(2);
    cout << "\nAverage waiting time: " << (total_wt * 1.0) / n << endl;
    cout << "\nAverage turn around time: " << (total_tat * 1.0) / n << endl
         << endl;
    return 0;
}

Input

5
0
5
1
6
8
8
2
7
3
3
3