Write a C++ code to implement CPU scheduling algorithm Shortest Job First(SJF)

Code:

#include<iostream>
using namespace std;
int VarMatrix[10][6];
 
void varswapping(int *ac, int *bc)
{
    int vartemp = *ac;
    *ac = *bc;
    *bc = vartemp;
}
 
void arrangeArrival(int num1, int VarMatrix[][6])
{
    for(int i=0; i<num1; i++)
    {
        for(int j=0; j<num1-i-1; j++)
        {
            if(VarMatrix[j][1] > VarMatrix[j+1][1])
            {
                for(int k=0; k<5; k++)
                {
                    varswapping(VarMatrix[j][k], VarMatrix[j+1][k]);
                }
            }
        }
    }
}
 
void cT(int num1, int VarMatrix[][6])
{
    int vartemp, val1;
    VarMatrix[0][3] = VarMatrix[0][1] + VarMatrix[0][2];
    VarMatrix[0][5] = VarMatrix[0][3] - VarMatrix[0][1];
    VarMatrix[0][4] = VarMatrix[0][5] - VarMatrix[0][2];
     
    for(int i=1; i<num1; i++)
    {
        vartemp = VarMatrix[i-1][3];
        int low1 = VarMatrix[i][2];
        for(int j=i; j<num1; j++)
        {
            if(vartemp >= VarMatrix[j][1] && low1 >= VarMatrix[j][2])
            {
                low1 = VarMatrix[j][2];
                val1 = j;
            }
        }
        VarMatrix[val][3] = vartemp + VarMatrix[val][2];
        VarMatrix[val][5] = VarMatrix[val][3] - VarMatrix[val][1];
        VarMatrix[val][4] = VarMatrix[val][5] - VarMatrix[val][2];
        for(int k=0; k<6; k++)
        {
            varswapping(VarMatrix[val][k], VarMatrix[i][k]);
        }
    }
}
 
int main()
{
    int num, vartemp;
     
    cout<<"number of Processing: ";
    cin>>num1;
     
    cout<<".Enter the process ID";
    for(int i=0; i<num1; i++)
    {
        cout<<"Processing "<<i+1<<"...\n";
        cout<<"Processing Id: ";
        cin>>VarMatrix[i][0];
        cout<<"Arrival Time: ";
        cin>>VarMatrix[i][1];
        cout<<"Burst Time: ";
        cin>>VarMatrix[i][2];
    }
     
    cout<<"Before Arrange...\n";
    cout<<"Processing ID\tArrival Time\tBurst Time\n";
    for(int i=0; i<num; i++)
    {
        cout<<VarMatrix[i][0]<<"\t\t"<<VarMatrix[i][1]<<"\t\t"<<VarMatrix[i][2]<<"\n";
    }
     
    arrangeArrival(num, VarMatrix);
    completionTime(num, VarMatrix);
    cout<<"Final Answer...\n";
    cout<<"Processing ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";
    for(int i=0; i<num; i++)
    {
        cout<<VarMatrix[i][0]<<"\t\t"<<VarMatrix[i][1]<<"\t\t"<<VarMatrix[i][2]<<"\t\t"<<VarMatrix[i][4]<<"\t\t"<<VarMatrix[i][5]<<"\n";
    }
}

Post a Comment

0 Comments