Showing posts with label Sheduling. Show all posts
Showing posts with label Sheduling. Show all posts

SJF Sheduling

SJF Sheduling

#include<bits/stdc++.h>


using namespace std;
struct process
{
    int pid;
    int at;
    int bt;
    int wt;
    bool flag;


};
bool cmp(process a, process b)
{
    return a.bt<b.bt;
}
bool cmp2(process a, process b)
{
    return a.at<b.at;
}
int main()
{
    queue<int>q;
    freopen ("sjfs.txt","r",stdin);
    //freopen ("fcfsout.txt","w",stdout);
    int n;
    vector<process>v;
    cin >> n;
    for (int i = 0; i<n; i++)
    {
        process temp;
        temp.pid = i;
        temp.flag = false;
        cin >> temp.at >> temp.bt;
        v.push_back(temp);

    }
    int time = 0;
    int idl = 0;
    int cou = 0, x = 0;
    sort(v.begin(), v.end(), cmp);

    vector<process>ans;
    while (cou < n)
    {
        vector<process>tem;
        for (int i = 0; i<n; i++)
        {
            if (v[i].at <= time&&!v[i].flag)
            {
                //cout<<v[i]<<endl;
                v[i].flag = true;
                tem.push_back(v[i]);
                break;


            }

        }


        if (tem.size() == 0)
        {
            idl++;
            time++;

        }
        else
        {
            tem[0].wt = time - tem[0].at;
            time += tem[0].bt;
            ans.push_back(tem[0]);
            cou++;

        }

    }
    double totalWt=0;
    for (int i = 0; i<ans.size(); i++)
    {
        totalWt+=ans[i].wt;


    }
    cout<<"\nProcess ID == Arival Time == Wait Time\n\n";
    for(int i=0;i<n;i++)
    {
        cout<<ans[i].pid<<"\t\t"<<ans[i].at<<"\t\t"<<ans[i].wt<<endl;
    }
    cout<<"\nAvg wait Time= "<<totalWt/(double)n<<endl;
    cout<<"Cpu Utilizatimon= "<<(double)((double)(time-idl)/(double)time)*100<<"\%"<<endl;

    //cout<<time<<endl;

    return 0;
}

FCFS Sheduling

FCFS Sheduling
#include<bits/stdc++.h>
using namespace std;
struct process
{
    int pid;
    int at;
    int bt;
    int wt;


};
bool cmp(process a,process b)
{
    return a.at<b.at;
}
int main()
{
    queue<int>q;
    freopen ("fcfs.txt","r",stdin);
    //freopen ("fcfsout.txt","w",stdout);
    int n;
    vector<process>v;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        process temp;
        temp.pid=i;
        cin>>temp.at>>temp.bt;
        v.push_back(temp);
    }
    sort(v.begin(),v.end(),cmp);
    int time = 0, idl = 0, x = 0;
    for (int i = 0; i<v.size(); i++)
    {
        q.push(i);

    }

    while (!q.empty())
    {
        int k = q.front();

        if (time >= v[k].at)
        {
            v[k].wt = time - v[k].at;
            //cout << v[k].wt << endl;
            time += v[k].bt;
            q.pop();
        }
        else {
            time++;
            idl++;
        }
    }

    double twt=0;
    for(int i=0;i<v.size();i++)
    {

        twt+=v[i].wt;
    }
    double cpuUtil=(double)((time-idl)/(double)time)*100.0;



    cout<<"Process ID-----Arrival Time---Duration-----Waiting Time\n";
    for(int i=0;i<v.size();i++)
    {
        cout<<v[i].pid<<"\t\t "<<v[i].at<<" \t\t"<<v[i].bt<<"\t\t"<<v[i].wt<<endl;
    }

    cout<<"Avarage Waiting Time = "<<(float)((float)twt/(float)n)<<endl;

    cout<<"CPU utilization = "<<cpuUtil<<"\%"<<endl;


    return 0;
}