Skip to content

Instantly share code, notes, and snippets.

@TanmayChakrabarty
Last active September 16, 2019 06:14
Show Gist options
  • Save TanmayChakrabarty/458e123a24d535a86c3d75ce0edd854a to your computer and use it in GitHub Desktop.
Save TanmayChakrabarty/458e123a24d535a86c3d75ce0edd854a to your computer and use it in GitHub Desktop.
C++ Program (Source Code) for Priority Scheduling CPU Scheduling Algorithm
/*
Compatible with C++, C++14, C++17
Tested at https://www.onlinegdb.com/online_c++_compiler
*/
#include <iostream>
using namespace std;
int main(){
int i,j,k,p,s=0, got=0, idle=0, temp_burst, temp_row, pre_process_row, done=0;
float sum=0;
cout<<"Please enter the number of proces : ";
cin>>p;
int a[p][5];
int b[p][5];
cout<<"\n\nProcess\tArrival\tBurst\tPriority\n-------\t-------\t-----\t--------\n";
for(i=0;i<p;i++){
for(j=0;j<5;j++){
if(j!=3){
cin>>a[i][j];
}
}
a[i][3]=a[i][2];
}
cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
i=a[0][1];
while(done!=p){
got=0;
k=0;
while(k<p){
if(a[k][1]<=i){
if(a[k][2]!=0){
got=1;
temp_burst=a[k][4];
temp_row=k;
idle=0;
break;
}
else
k++;
}
else{
if(idle==0)
printf("%5d-----------\n |Idle |\n",i);
idle=1;
break;
}
}
if(got!=0){
k=0;
while(a[k][1]<=i && k<p){
if(a[k][2]!=0){
if(temp_burst>a[k][4]){
temp_burst=a[k][4];
temp_row=k;
}
}
k++;
}
a[temp_row][2]-=1;
if(i==a[0][1])
printf("%5d-----------\n |p-%-4d|\n",i,a[temp_row][0]);
else{
if(pre_process_row!=temp_row)
printf("%5d-----------\n |p-%-4d|\n",i,a[temp_row][0]);
}
pre_process_row=temp_row;
if(a[temp_row][2]==0){
done++;
b[s][0]=a[temp_row][0];
b[s][1]=a[temp_row][1];
b[s][2]=i;
b[s][3]=a[temp_row][3];
b[s][4]=((i-a[temp_row][1])-a[temp_row][3])+1;
sum+=((i-a[temp_row][1])-a[temp_row][3])+1;
s++;
}
}
i++;
}
printf("%5d-----------\n",i);
cout<<endl<<endl;
cout<<"Table of processes with completion record as they were completed\n\n";
cout<<"\n\nProcess\tArrival\tFinish\tTotal\tWait\n------------------------------------\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i][3]<<"\t"<<b[i][4]<<"\n";
cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
cout<<"__________________________________________________________\nProgrammed by : Tanmay Chakrabarty, onlineclassnotes.com.\n\n";
//system("pause");
return 0;
}
@TanmayChakrabarty
Copy link
Author

@nabbbilll
Copy link

can you explain the function for each variable in the program for me to understand it better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment