CPU scheduling Algos

Posting a few basic c++ programs I wrote to simulate CPU scheduling algorithms.

First Come First Serve

As the name suggest, we select process according to their arrival time.

#include <stdio.h>
int main(){  
  int no_of_process = 3;
  int burst_times[] = {3,2,4};
  int wait_time = 0;
  int turn_around_time = 0;

  printf("PID \t\t Burst Time \t\t Wait Time \t\t Turn Around Time\n");

  for(int i=0;i < no_of_process;i++){
    turn_around_time = wait_time + burst_times[i];
    printf("%d \t\t %d \t\t\t %d \t\t\t%d  \n",i,burst_times[i],wait_time,turn_around_time );
    wait_time+=burst_times[i];
  }
}
Output
PID          Burst Time          Wait Time       Turn Around Time
0          3           0          3  
1          2           3          5  
2          4           5          9  
Priority Scheduling

We select the process according to their priority.

#include<stdio.h>
#include<algorithm>
int main(){  
  int no_of_process = 4;
  int priorities []={5,2,1,3};
  //1,2,5,3
  int burst_times[]= {3,4,1,2};
  int min,min_index;
  int wait_time=0;
  //  Selection sort
  int turnaround_time=0;
  for(int i=0;i<no_of_process;i++)
  {
    min = priorities[i];
    min_index = i;
    for(int j=i+1;j<no_of_process;j++){
      if (priorities[j] < min){
        min_index = j;
        min = priorities[j];
      }
    }
    // Swap min_index with i
    int temp = priorities[i];
    //printf("%d debug\n",min );
    priorities[i] = priorities[min_index];
    priorities[min_index] = temp;

    temp = burst_times[i];
    burst_times[i] = burst_times[min_index];
    burst_times[min_index] = temp;
  }
  printf("PID \t\t Priotity \t\t Burst Time \t\t Wait time \t\t Turn Around time \n" );
  for(int i=0;i<no_of_process;i++){
    turnaround_time = wait_time + burst_times[i];
    printf("%d \t\t %d \t\t\t %d \t\t\t %d\t\t\t %d\n",i,priorities[i],burst_times[i],wait_time,turnaround_time);
    wait_time+=burst_times[i];
  }
}
Shortest Job First

We select the process according to their Burst Time. (Speediest process first)

#include <stdio.h>
#include<algorithm>
using namespace std;  
int main(){  
  int no_of_process = 4;
  int burst_times[] = {4,2,1,5};
  int wait_time = 0;
  int turn_around_time = 0;

  sort(burst_times,burst_times+4);
  printf("PID \t\t Burst Time \t\t Wait Time \t\t Turn Around time \n" );
  for(int i=0;i<no_of_process;i++){
    turn_around_time = wait_time + burst_times[i];
    printf("%d \t\t %d \t\t %d \t\t %d \n",i,burst_times[i],wait_time, turn_around_time );
    wait_time +=burst_times[i];
  }
}
Round Robin

We execute each process for a period of quantum time in a circular way.

#include<stdio.h>
int main(){  
  int burst_times []={1,4,3};
  int rt[]= {1,3,4};
  int quantum = 2;
  int time=0;
  int remain = 3;
  int i =0;
  printf("PID \t\tTurn Around Time \t\t Wait time \n" );
  while (remain >0){
    if (burst_times[i] <= quantum && burst_times[i] > 0){
      burst_times[i] = 0;
      time+=rt[i];
    }
    else if(burst_times[i] > 0){
      burst_times[i]-=quantum;
      time+=quantum;
    }
    if (burst_times[i] == 0){
      remain--;
      printf("%d \t\t %d \t\t\t\t %d \n",rt[i],time,time-rt[i]);
      burst_times[i]=-1;
    }
    if(i==2){
      i=0;
    }
    else{
      i+=1;
    }

  }
}