Experiment 3
Implement First Come First Serve CPU Scheduling Algorithm
#include<iostream>
using namespace std;
int main(){
int n,at[20],bt[20],ct[20],tat[20],wt[20];
float avgtat=0,avgwt=0;
cout<<"Enter the number of process: ";
cin>>n;
cout << "Enter the details:";
for(int i=0;i<n;i++)
{
cout<<"\n\nP["<<i+1<<"]:\n";
cout<<"Arrival Time: \t\t";
cin>>at[i];
cout<<"\nBrust Time: \t\t";
cin>>bt[i];
}
ct[0]=at[0]+bt[0];
tat[0]=ct[0]-at[0];
wt[0]=tat[0]-bt[0];
for(int i=1;i<n;i++){
ct[i]=ct[i-1]+bt[i];
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
}
cout<<"\nProcess_ID\t\t"<<"Arrival Time\t\t"<<"Burst Time\t\
t"<<"Completion Time\t\t"<<"Turn Around Time\t"<<"Waiting Time"<<"\n\
n";
for (int i = 0; i < n; i++) {
cout << "P["<<i+1<<"]" << "\t\t\t" << at[i]
<< "\t\t\t" << bt[i] << "\t\t\t" << ct[i]<<"\t\t\t" <<
tat[i]<<"\t\t\t" << wt[i] <<endl;
avgtat+=tat[i];
avgwt+=wt[i];
}
avgtat/=n;
avgwt/=n;
cout<<"Average Turn Around Time: \t"<<avgtat<<endl;
cout<<"Average Waiting Time: \t\t"<<avgwt<<endl;
Experiment 4
Implement Shortest Job First CPU Scheduling algorithm
#include<iostream>
using namespace std;
int main(){
int n,at[20],bt[20],ct[20],tat[20],wt[20],temp=0, pid[20];
float avgtat=0,avgwt=0;
cout<<"Enter the number of process: ";
cin>>n;
cout << "Enter the details:";
for(int i=0;i<n;i++)
{
cout<<"\nProcess ID: \t\t";
cin>>pid[i];
cout<<"\nArrival Time: \t\t";
cin>>at[i];
cout<<"\nBrust Time: \t\t";
cin>>bt[i];
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(bt[i]>bt[j]) //checking the brust time
{
temp=at[i];
at[i]=at[j]; //sorting the arrival time
at[j]=temp;
temp=bt[i];
bt[i]=bt[j]; //sorting the brust time
bt[j]=temp;
temp=pid[i];
pid[i]=pid[j]; //sorting the process id
pid[j]=temp;
}
}
}
int min,d,t=0;
min=at[0];
for(int i=0;i<n;i++)
{
if(min>at[i])
{
min=at[i];
d=i;
}
}
t=min;
ct[d]=t+bt[d];
t=ct[d];
for(int i=0;i<n;i++)
{
if(at[i]!=min)
{
ct[i]=bt[i]+t;
t=ct[i];
}
}
for(int i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
avgtat=avgtat+tat[i];
wt[i]=tat[i]-bt[i];
avgwt=avgwt+wt[i];
}
avgtat=avgtat/n;
avgwt=avgwt/n;
cout<<"\nProcess_ID\t\t"<<"Arrival Time\t\t"<<"Burst Time\t\
t"<<"Completion Time\t\t"<<"Turn Around Time\t"<<"Waiting Time"<<"\n\
n";
for (int i = 0; i < n; i++) {
cout << pid[i] << "\t\t\t" << at[i]
<< "\t\t\t" << bt[i] << "\t\t\t" << ct[i]<<"\t\t\t" <<
tat[i]<<"\t\t\t" << wt[i] <<endl;
avgtat+=tat[i];
avgwt+=wt[i];
}
avgtat/=n;
avgwt/=n;
cout<<"Average Turn Around Time: \t"<<avgtat<<endl;
cout<<"Average Waiting Time: \t\t"<<avgwt<<endl;
}
Experiment 5
Implement Shortest Remaining time CPU Scheduling algorithm
#include<iostream>
#include<algorithm>
#include<limits.h>
using namespace std;
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnaroundTime;
int waitingTime;
};
bool compareArrivalTime(const Process& a, const Process& b) {
return a.arrivalTime < b.arrivalTime;
}
int main() {
int n;
float avgTurnaroundTime = 0, avgWaitingTime = 0;
cout << "Enter the number of processes: ";
cin >> n;
Process processes[n];
cout << "Enter process details:\n";
for(int i = 0; i < n; i++) {
cout << "\nProcess " << i+1 << ":\n";
processes[i].id = i+1;
cout << "Arrival Time: ";
cin >> processes[i].arrivalTime;
cout << "Burst Time: ";
cin >> processes[i].burstTime;
processes[i].remainingTime = processes[i].burstTime;
}
sort(processes, processes + n, compareArrivalTime);
int currentTime = 0, completedProcesses = 0;
cout << "\nExecution Order: ";
while (completedProcesses < n) {
int shortestRemainingTimeIndex = -1;
int shortestRemainingTime = INT_MAX;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime &&
processes[i].remainingTime < shortestRemainingTime &&
processes[i].remainingTime > 0) {
shortestRemainingTime = processes[i].remainingTime;
shortestRemainingTimeIndex = i;
}
}
if (shortestRemainingTimeIndex == -1) {
currentTime++;
continue;
}
processes[shortestRemainingTimeIndex].remainingTime--;
currentTime++;
if (processes[shortestRemainingTimeIndex].remainingTime == 0) {
completedProcesses++;
processes[shortestRemainingTimeIndex].completionTime =
currentTime;
processes[shortestRemainingTimeIndex].turnaroundTime =
processes[shortestRemainingTimeIndex].completionTime -
processes[shortestRemainingTimeIndex].arrivalTime;
processes[shortestRemainingTimeIndex].waitingTime =
processes[shortestRemainingTimeIndex].turnaroundTime -
processes[shortestRemainingTimeIndex].burstTime;
cout << "P" << processes[shortestRemainingTimeIndex].id <<
" ";
}
}
cout << "\n\nProcess\tArrival Time\tBurst Time\tCompletion Time\
tTurnaround Time\tWaiting Time\n";
for (int i = 0; i < n; i++) {
cout << processes[i].id << "\t" << processes[i].arrivalTime <<
"\t\t" << processes[i].burstTime << "\t\t" <<
processes[i].completionTime << "\t\t" << processes[i].turnaroundTime <<
"\t\t" << processes[i].waitingTime << endl;
avgTurnaroundTime += processes[i].turnaroundTime;
avgWaitingTime += processes[i].waitingTime;
}
avgTurnaroundTime /= n;
avgWaitingTime /= n;
cout << "\nAverage Turnaround Time: " << avgTurnaroundTime << endl;
cout << "Average Waiting Time: " << avgWaitingTime << endl;
return 0;
}
Experiment 6
Implement Round Robin CPU Scheduling algorithm
#include <iostream>
#include <vector>
#include <queue>
#include <numeric> // Include numeric for accumulate
using namespace std;
struct Process {
int id; // Process ID
int arrivalTime; // Arrival Time
int burstTime; // Burst Time
int remainingBurstTime; // Remaining Burst Time
};
// Function to execute Round Robin CPU Scheduling algorithm
void roundRobinScheduling(vector<Process>& processes, int quantum) {
int n = processes.size(); // Number of processes
vector<int> completionTime(n); // Completion Time
vector<int> turnaroundTime(n); // Turnaround Time
vector<int> waitingTime(n); // Waiting Time
// Custom comparator for priority queue
auto compare = [](const Process& p1, const Process& p2) {
return p1.arrivalTime > p2.arrivalTime || (p1.arrivalTime ==
p2.arrivalTime && p1.id > p2.id);
};
priority_queue<Process, vector<Process>, decltype(compare)>
readyQueue(compare); // Min heap of Processes
int currentTime = 0;
int processed = 0;
int idx = 0; // Index to keep track of the current process
while (processed < n) {
// Push processes to the ready queue based on arrival time
while (idx < n && processes[idx].arrivalTime <= currentTime) {
readyQueue.push(processes[idx]); // Push process
idx++;
}
if (!readyQueue.empty()) {
Process currentProcess = readyQueue.top(); // Retrieve the
process with the earliest arrival time
readyQueue.pop();
int executionTime = min(quantum,
currentProcess.remainingBurstTime);
currentTime += executionTime;
currentProcess.remainingBurstTime -= executionTime;
if (currentProcess.remainingBurstTime <= 0) {
completionTime[currentProcess.id] = currentTime;
turnaroundTime[currentProcess.id] = currentTime -
currentProcess.arrivalTime;
waitingTime[currentProcess.id] =
turnaroundTime[currentProcess.id] - currentProcess.burstTime;
processed++;
} else {
while (idx < n && processes[idx].arrivalTime <=
currentTime) {
readyQueue.push(processes[idx]); // Push any new
processes that arrive during this quantum
idx++;
}
readyQueue.push(currentProcess); // Push back the
current process if it still has remaining burst time
}
} else {
currentTime = processes[idx].arrivalTime;
}
}
// Display results
cout << "Process_ID\t\tArrival Time\tBurst Time\tCompletion Time\
tTurnaround Time\tWaiting Time\n\n";
for (int i = 0; i < n; i++) {
cout << "P[" << processes[i].id + 1 << "]\t\t\t" <<
processes[i].arrivalTime << "\t\t\t" << processes[i].burstTime
<< "\t\t\t" << completionTime[i] << "\t\t\t" <<
turnaroundTime[i] << "\t\t\t" << waitingTime[i] << endl;
}
// Calculate average turnaround time and average waiting time
float totalTurnaroundTime = accumulate(turnaroundTime.begin(),
turnaroundTime.end(), 0); // Use accumulate from numeric header
float totalWaitingTime = accumulate(waitingTime.begin(),
waitingTime.end(), 0);
float averageTurnaroundTime = totalTurnaroundTime / n;
float averageWaitingTime = totalWaitingTime / n;
cout << "\nAverage Turnaround Time: " << averageTurnaroundTime <<
endl;
cout << "Average Waiting Time: " << averageWaitingTime << endl;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
cout << "Enter the details:\n";
for (int i = 0; i < n; i++) {
cout << "\n\nP[" << i + 1 << "]:\n";
cout << "Arrival Time: ";
cin >> processes[i].arrivalTime;
cout << "Burst Time: ";
cin >> processes[i].burstTime;
processes[i].remainingBurstTime = processes[i].burstTime;
processes[i].id = i;
}
int quantum;
cout << "Enter the time quantum for Round Robin: ";
cin >> quantum;
roundRobinScheduling(processes, quantum);
return 0;
}
Experiment 7
Banker’s
#include <iostream>
#include <vector>
using namespace std;
const int NUMBER_OF_PROCESSES = 5;
const int NUMBER_OF_RESOURCES = 3;
vector<int> available(NUMBER_OF_RESOURCES);
vector<vector<int>> maximum(NUMBER_OF_PROCESSES,
vector<int>(NUMBER_OF_RESOURCES));
vector<vector<int>> allocation(NUMBER_OF_PROCESSES,
vector<int>(NUMBER_OF_RESOURCES));
vector<vector<int>> need(NUMBER_OF_PROCESSES,
vector<int>(NUMBER_OF_RESOURCES));
bool is_safe_state(vector<int>& available, vector<vector<int>>& need,
vector<vector<int>>& allocation) {
vector<bool> finish(NUMBER_OF_PROCESSES, false);
vector<int> work = available;
int count = 0;
while (count < NUMBER_OF_PROCESSES) {
bool found = false;
for (int i = 0; i < NUMBER_OF_PROCESSES; ++i) {
if (!finish[i]) {
bool can_allocate = true;
for (int j = 0; j < NUMBER_OF_RESOURCES; ++j) {
if (need[i][j] > work[j]) {
can_allocate = false;
break;
}
}
if (can_allocate) {
for (int j = 0; j < NUMBER_OF_RESOURCES; ++j) {
work[j] += allocation[i][j];
}
finish[i] = true;
found = true;
++count;
}
}
}
if (!found) {
return false; // System is in an unsafe state
}
}
return true; // System is in a safe state
}
bool request_resources(int pid, vector<int>& request) {
// Check if request is within need
for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) {
if (request[i] > need[pid][i]) {
return false; // Request exceeds need
}
}
// Check if request is within available
for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) {
if (request[i] > available[i]) {
return false; // Request exceeds available
}
}
// Simulate resource allocation
for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
// Check if system is still in safe state after allocation
if (is_safe_state(available, need, allocation)) {
return true; // Resource request granted
} else {
// Revert allocation
for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
return false; // Resource request denied
}
}
int main() {
// Initialize available resources
available = {3, 3, 2};
// Initialize maximum resources for each process
maximum = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
// Initialize allocation for each process
allocation = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
// Calculate need for each process
for (int i = 0; i < NUMBER_OF_PROCESSES; ++i) {
for (int j = 0; j < NUMBER_OF_RESOURCES; ++j) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
// Perform resource request and print results
vector<int> request = {1, 0, 2};
int pid = 1; // Process making the request
if (request_resources(pid, request)) {
cout << "Request granted." << endl;
} else {
cout << "Request denied." << endl;
}
return 0;
}
Implement SFJ
Implement SRTF
round robin
priority scheduling
Banker's algorithm