Experiment 3
Experiment 3
#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];
}
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;
Experiment 5
#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;
};
int main() {
int n;
float avgTurnaroundTime = 0, avgWaitingTime = 0;
Process processes[n];
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;
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
struct Process {
int id; // Process ID
int arrivalTime; // Arrival Time
int burstTime; // Burst Time
int remainingBurstTime; // Remaining Burst Time
};
int currentTime = 0;
int processed = 0;
int idx = 0; // Index to keep track of the current process
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;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
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>
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
}
int main() {
// Initialize available resources
available = {3, 3, 2};
return 0;
}
Implement SFJ
Implement SRTF
round robin
priority scheduling
Banker's algorithm