Cpu Scheduling Codes
Cpu Scheduling Codes
1] FCFS/FIFO:
#include <iostream>
#include <vector>
using namespace std;
struct Process {
int id; // Process ID
int AT; // Arrival Time
int BT; // Burst Time
int CT; // Completion Time
int TAT; // Turnaround Time
int WT; // Waiting Time
};
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
cout << endl;
vector<Process> processes(n);
// Display results
cout << "\nP | AT | BT | CT | TAT | WT |\n";
for (int i = 0; i < n; i++) {
cout << " " << processes[i].id << " | " << processes[i].AT
<< " | "
<< processes[i].BT << " | " << processes[i].CT << " | "
<< processes[i].TAT << " | " << processes[i].WT << "
|\n";
}
cout << "\nAverage Turnaround Time (TAT): " << avgTAT <<
endl;
cout << "Average Waiting Time (WT): " << avgWT << endl;
return 0;
}
2] SJF
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
int n;
int currentTime = 0;
vector<bool> isCompleted(n, false); // Track completed
processes
// Select the process with the smallest burst time that has
arrived and not yet completed
for (int j = 0; j < n; j++) {
if (!isCompleted[j] && processes[j].arrivalTime <=
currentTime && processes[j].burstTime < minBurstTime) {
minBurstTime = processes[j].burstTime;
idx = j;
}
}
if (idx == -1) {
// No process has arrived yet; advance current time to
the next process's arrival time
for (int j = 0; j < n; j++) {
if (!isCompleted[j]) {
currentTime = processes[j].arrivalTime;
break;
}
}
i--; // Revisit this iteration to find the next process
} else {
// Process found
isCompleted[idx] = true;
currentTime += processes[idx].burstTime;
processes[idx].completionTime = currentTime;
processes[idx].turnaroundTime =
processes[idx].completionTime - processes[idx].arrivalTime;
processes[idx].waitingTime =
processes[idx].turnaroundTime - processes[idx].burstTime;
}
}
avgTAT /= n;
avgWT /= n;
cout << "\nAverage Turnaround Time: " << avgTAT << endl;
cout << "Average Waiting Time: " << avgWT << endl;
return 0;
}
3] RR scheduling:
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,quantum,completed=0,total_time=0;
double avg_tat=0,avg_wt=0;
cout<<"Enter the number of processes: ";
cin>>n;
cout<<"Enter the time qunatum: ";
cin>>quantum;
int at[n],bt[n],rem_bt[n],ct[n],tat[n],wt[n];
for(int i=0;i<n;i++){
cout<<"Arrival time for process p"<<i+1<<":";
cin>>at[i];
cout<<"Burst time for process p"<<i+1<<":";
cin>>bt[i];
rem_bt[i]=bt[i];
}
int time=0;
bool done[n] ={false};
//logic
while(completed<n){
bool all_idle=true;
for(int i=0;i<n;i++){
if(rem_bt[i]>0 && at[i]<=time){
all_idle=false;
if(rem_bt[i]>quantum){
time+=quantum;
rem_bt[i]-=quantum;
}
else{
time+=rem_bt[i];
rem_bt[i]=0;
ct[i]=time;
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
avg_tat+=tat[i];
avg_wt+=wt[i];
completed++;
done[i]=true;
}
}
}
if(all_idle){
time++;
}
}
avg_tat/=n;
avg_wt/=n;
cout<<"\nProcess | AT | BT | CT | TAT | WT |\n";
cout<<fixed<<setprecision(2);
for(int i=0;i<n;i++){
cout<<"P "<<i+1<<" \t"<<at[i]<<"\t\t"<<bt[i]<<"\t\
t"<<ct[i]<<"\t\t"<<tat[i]<<"\t\t"<<wt[i]<<endl;