Header Ads Widget

Responsive Advertisement

Latest

6/recent/ticker-posts

Program for FCFS , SJF , ROUND ROBIN Scheduling Algorithm

C++ program to implement CPU scheduling algorithms

 

Program to implement FCFS Scheduling Algorithm(C++)

 
#include<iostream>
using namespace std;
int main()
{
         int n,bt[10], wt[10], tat[10], avgwt=0, avgtat=0, i , j;
         cout<<"Enter total no of processes(Max 10):\n";
         cin>>n;
         cout<<"Enter the process burst time:\n";
         for(i=0 ; i<n ; i++)
         {
            cout<<"P["<<i+1<<"]:";
             cin>>bt[i];
          }
           wt[0]=0;
           for(i=0 ; i<n ; i++)
          { 
            wt[i]=0;
             for(j=0 ; j<i ; j++)
              wt[i]+=bt[j];
           }
            cout<<"\nProcess\t\tBurst Time\t\tWaiting Time\t\tTurn Arround Time\n";
            for(i=0 ; i<n ; i++)
            {
            tat[i]=bt[i]+wt[i];
            avgwt+=wt[i];
            avgtat+=tat[i];
            cout<<"P["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]<<"\n";
            }
            avgwt/=i;
            avgtat/=i;
            cout<<"\n\n Average Waiting Time:"<<avgwt;
            cout<<"\n\n Average Turn Arround Time:"<<avgtat;
            return 0;
}
 
OUTPUT:

Output for FCFS Algorithm
Output For FCFS CPU Scheduling Algorithm
         

Program to implement SJF Scheduling Algorithm(C++)

 
#include<iostream.h>
int main()
{
    int p[10] , burst[10] , wait[10] , turnaround[10] , i , j , n , total=0 , pos , temp ; 
    float avgwait , avgturn ;
    cout<<"Enter no of process:";
    cin>>n ;
 
    cout<<"\nEnter Burst Time:\n";
    for(i=0 ; i<n ; i++)
    {
      cout<<"p"<<i+1<<":";
      cin>>burst[i];
      p[i]=i+1;
    }
    //Sorting by selection sort
    for(i=0 ; i<n ; i++)
    {
      pos=i;
      for(j=i+1 ; j<n ; j++)
      {
         if(burst[j]<burst[pos])
          pos=j;
       }
     temp=burst[i];
     burst[i]=burst[pos];
     burst[pos]=temp;

     temp=p[i];
     p[i]=p[pos];
     p[pos]=temp;
     }
     wait[0]=0;
     //calculation
     for(i=1 ; i<n ; i++)
     {
     wait[i]=0;
        for(j=0 ; j<i ; j++)
           wait[i]+=burst[j];
           total+=wait[i];
     }
     avgwait=(float)total/n;
     total=0;
     cout<<\nProcess\tBurst Time\twaiting time\tTurnArround Time"
     for(i=0 ; i<n ; i++)
     {
      turnaround[i]=burst[i]+wait[i];
      total+=turnaround[i];
      cout<<"\np"<<p[i]<<"\t\t"<<burst[i]<<"\t\t"<<wait[i]<<"\t\t\t"<<turnaround[i];
      }
     avgturn=(float)total/n;
     cout<<"\nAverage Waiting Time="<<avgwait;
     cout<<"\nAvearge Turn Arround="<<avgturn;
     return 0;
    }
 
OUTPUT:
 
Output for SJF Algorithm
Output For SJF CPU Scheduling Algorithm

     

 

 

 

 

 

 

 

 

 

Program to implement Rounds Robin Scheduling Algorithm(C++)

 
#include<iostream.h>

int main()
{

  int count,j,n,time,remain,flag=0,time_quantum;
  int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
  cout<<"Enter Total Process:\t ";
  cin>>n;
  remain=n;
  for(count=0;count<n;count++)
  {
    cout<<"Enter Arrival Time and Burst Time for Process Process Number"<<count+1<<":";
    cin>>at[count];
    cin>>bt[count];
    rt[count]=bt[count];
  }
  cout<<"Enter Time Quantum:\t";
  cin>>time_quantum;
  cout<<"\n\nProcess\t|Turnaround Time|Waiting Time\n\n";
  for(time=0,count=0;remain!=0;)
  {
    if(rt[count]<=time_quantum && rt[count]>0)
    {
      time+=rt[count];
      rt[count]=0;
      flag=1;
    }
    else if(rt[count]>0)
    {
      rt[count]-=time_quantum;
      time+=time_quantum;
    }
    if(rt[count]==0 && flag==1)
    {
      remain--;
 cout<<"P["<<count+1<<"]"<<"\t\t"<<time-at[count]<<"\t\t"<<time-at[count]- bt[count]<<"\n";
      wait_time+=time-at[count]-bt[count];
      turnaround_time+=time-at[count];
      flag=0;
    }
    if(count==n-1)
      count=0;
    else if(at[count+1]<=time)
      count++;
    else
      count=0;
  }
  cout<<"\nAverage Waiting Time="<<wait_time*1.0/n<<"\n";
  cout<<"Avg Turnaround Time = "<<turnaround_time*1.0/n<<"\n";
  
  return 0;
}
 
OUTPUT:

Output for Round Robin algorithm
Output for Round Robin CPU Scheduling algorithm



Post a Comment

2 Comments