Submission for Previous Labs and Assignment for Scheduling 

your assignment is to implement the Shortest Remaining Time & Round Robin (RR) Scheduling using c++ deadline is 16/01/2021

Definition of Process Scheduler:

A Process Scheduler schedules different processes to be assigned to the CPU based on scheduling algorithms.

Algorithms for Scheduling:


There are many popular process scheduling algorithms that we use in many applications, but I have mentioned and implement some basic and very effective here:

Ø  First-Come, First-Served (FCFS) Scheduling

Ø  Shortest-Job-Next (SJN) Scheduling

Ø  Shortest Remaining Time

Ø  Round Robin (RR) Scheduling

 

non-preemptive algorithms are designed so that once a process enters the running state, it cannot be preempted until it completes its allotted time.

Preemptive scheduling is based on priority where a scheduler may preempt a low priority running process anytime when a high priority process enters a ready state.

First Come First Serve (FCFS)

  • Jobs are executed on a first-come, first-serve basis.
  • It is a non-preemptive, pre-emptive scheduling algorithm.
  • Easy to understand and implement.
  • Its implementation is based on the FIFO queue.
  • Poor in performance as the average wait time is high.

 

Shortest Job Next (SJN)

Ø  This is also known as the shortest job first, or SJF

Ø  This is a non-preemptive, pre-emptive scheduling algorithm.

Ø  Best approach to minimize the waiting time.

Ø  Easy to implement in Batch systems where required CPU time is known in advance.

Ø  Impossible to implement in interactive systems where the required CPU time is not known.

Ø  The processer should know in advance how much time the process will take.


Implementation of FCFS & SJN Algorithms in C++:

 // C++ program to implement Shortest Job first with Arrival Time

#include<iostream>

using namespace std;

int mat[10][6];

 

void swap(int *a, int *b)

{

       int temp = *a;

       *a = *b;

       *b = temp;

}

 

void arrangeArrival(int num, int mat[][6])

{

       for(int i=0; i<num; i++)

       {

              for(int j=0; j<num-i-1; j++)

              {

                     if(mat[j][1] > mat[j+1][1])

                     {

                           for(int k=0; k<5; k++)

                           {

                                  swap(mat[j][k], mat[j+1][k]);

                           }

                     }

              }

       }

}

 

void completionTime(int num, int mat[][6])

{

       int temp, val;

       mat[0][3] = mat[0][1] + mat[0][2];

       mat[0][5] = mat[0][3] - mat[0][1];

       mat[0][4] = mat[0][5] - mat[0][2];

      

       for(int i=1; i<num; i++)

       {

              temp = mat[i-1][3];

              int low = mat[i][2];

              for(int j=i; j<num; j++)

              {

                     if(temp >= mat[j][1] && low >= mat[j][2])

                     {

                           low = mat[j][2];

                           val = j;

                     }

              }

              mat[val][3] = temp + mat[val][2];

              mat[val][5] = mat[val][3] - mat[val][1];

              mat[val][4] = mat[val][5] - mat[val][2];

              for(int k=0; k<6; k++)

              {

                     swap(mat[val][k], mat[i][k]);

              }

       }

}

 

int main()

{

       int num, temp;

      

       cout<<"Enter number of Process: ";

       cin>>num;

      

       cout<<"...Enter the process ID...\n";

       for(int i=0; i<num; i++)

       {

              cout<<"...Process "<<i+1<<"...\n";

              cout<<"Enter Process Id: ";

              cin>>mat[i][0];

              cout<<"Enter Arrival Time: ";

              cin>>mat[i][1];

              cout<<"Enter Burst Time: ";

              cin>>mat[i][2];

       }

      

       cout<<"Before Arrange...\n";

       cout<<"Process ID\tArrival Time\tBurst Time\n";

       for(int i=0; i<num; i++)

       {

              cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";

       }

      

       arrangeArrival(num, mat);

       completionTime(num, mat);

       cout<<"Final Result...\n";

       cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";

       for(int i=0; i<num; i++)

       {

          cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i][4]<<"\t\t"<<mat[i][5]<<"\n";

       }

       system("pause");

       return 0;

 

}

 

// C++ program for implementation of FCFS 

// scheduling

#include<iostream>

using namespace std;

 

// Function to find the waiting time for all 

// processes

void findWaitingTime(int processes[], int n, 

                          int bt[], int wt[])

{

    // waiting time for first process is 0

    wt[0] = 0;

 

    // calculating waiting time

    for (int  i = 1; i < n ; i++ )

        wt[i] =  bt[i-1] + wt[i-1] ;

}

 

// Function to calculate turn around time

void findTurnAroundTime( int processes[], int n,  int bt[], int wt[], int tat[])

{

    // calculating turnaround time by adding

    // bt[i] + wt[i]

    for (int  i = 0; i < n ; i++)

        tat[i] = bt[i] + wt[i];

}

 

//Function to calculate average time

void findavgTime( int processes[], int n, int bt[])

{

    int wt[3], tat[3], total_wt = 0, total_tat = 0;

 

    //Function to find waiting time of all processes

    findWaitingTime(processes, n, bt, wt);

 

    //Function to find turn around time for all processes

    findTurnAroundTime(processes, n, bt, wt, tat);

 

    //Display processes along with all details

    cout << "Processes  "<< " Burst time  "

         << " Waiting time  " << " Turn around time\n";

 

    // Calculate total waiting time and total turn 

    // around time

    for (int  i=0; i<n; i++)

    {

        total_wt = total_wt + wt[i];

        total_tat = total_tat + tat[i];

        cout << "   " << i+1 << "\t\t" << bt[i] <<"\t    "

            << wt[i] <<"\t\t  " << tat[i] <<endl;

    }

 

    cout << "Average waiting time = "

         << (float)total_wt / (float)n;

    cout << "\nAverage turn around time = "

         << (float)total_tat / (float)n;

}

 

// Driver code

int main()

{

    //process id's

    int processes[] = { 1, 2, 3};

    int n = sizeof processes / sizeof processes[0];

 

    //Burst time of all processes

    int  burst_time[] = {10, 5, 8};

 

    findavgTime(processes, 3,  burst_time);

       cout<<endl;

       system("pause");

    return 0;

      

}

 

Submission of FCFS Algorithm:

Click here for submission 

Submission of shortest job first algorithm:


Submission of Assignment  on Shortest Remaining Time & Round Robin (RR) Scheduling Algorithm:



Post a Comment

Previous Post Next Post