OS Lab Assignment - 03: Name: Sachin Mahendra Gaikwad Roll No: 207941 Reg No: MC20151

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

OS Lab Assignment – 03

Name : Sachin Mahendra Gaikwad


Roll No : 207941
Reg No : MC20151

Implementation of Shortest Remaining Time first (SRTF)


#include<bits/stdc++.h>
using namespace std;
#define mx 100

int main()
{
int n, at[mx], bt[mx], st[mx], ct[mx], tat[mx], wt[mx], rt[mx];
cout << "Enter total number of process : ";
cin >> n;

int p[n];
for(int i=0; i<n; i++)
p[i] = i+1;

cout << "Enter Arrival time of the process : ";


for(int i=0; i<n; i++)
cin >> at[i];

int burst_remaining[100];
cout << "Enter Burst time of the process : ";
for(int i=0; i<n; i++)
{
cin >> bt[i];
burst_remaining[i] = bt[i];
}

int total_turnaround_time = 0;
int total_waiting_time = 0;
int total_response_time = 0;

int is_completed[100]{0};
int current_time = 0;
int completed = 0;

while(completed != n)
{
int idx = -1;
int min = INT_MAX;
for(int i=0; i<n; i++)
{
if(at[i] <= current_time && is_completed[i] == 0)
{
if(burst_remaining[i] < min)
{
min = burst_remaining[i];
idx = i;
}
if(burst_remaining[i] == min)
{
if(at[i] < at[idx])
{
min = burst_remaining[i];
idx = i;
}
}
}
}

if(idx != -1)
{
if(burst_remaining[idx] == bt[idx])
{
st[idx] = current_time;
}
burst_remaining[idx] -= 1;
current_time++;

if(burst_remaining[idx] == 0)
{
ct[idx] = current_time;
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];
rt[idx] = st[idx] - at[idx];

total_turnaround_time += tat[idx];
total_waiting_time += wt[idx];
total_response_time += rt[idx];

is_completed[idx] = 1;
completed++;
}
}
else
current_time++;
}
float avg_turnaround_time = (float) total_turnaround_time / n;
float avg_waiting_time = (float) total_waiting_time / n;
float avg_response_time = (float) total_response_time / n;

cout << endl << "#P\t" << "AT\t" << "BT\t" << "ST\t" << "CT\t" << "TAT\t"
<< "WT\t" << "RT\t" << "\n" << endl;

for(int i=0; i<n; i++)


{
cout << p[i] << "\t" << at[i] << "\t" << bt[i] << "\t" << st[i] << "\t
" << ct[i] << "\t" << tat[i] << "\t" << wt[i] << "\t" << rt[i] << "\t" << "\n"
<< endl;
}

cout << "Average Turnaround Time = " << avg_turnaround_time << endl;
cout << "Average Waiting Time = " << avg_waiting_time << endl;
cout << "Average Response Time = " << avg_response_time << endl;

return 0;
}

Output :
Implementation of Priority Scheduling Algorithm (Non - Preemptive)
#include<bits/stdc++.h>
using namespace std;
#define mx 100
int main()
{
int n, at[mx], bt[mx], pr[mx], st[mx], ct[mx], tat[mx], wt[mx], rt[mx];
cout << "Enter total number of process : ";
cin >> n;

int p[n];
for(int i = 0; i < n; i++)
p[i] = i+1;

cout << "Enter Arrival time of the process : ";


for(int i = 0; i < n; i++)
cin >> at[i];

cout << "Enter Burst time of the process : ";


for(int i = 0; i < n; i++)
cin >> bt[i];

cout << "Enter Priority of the process : ";


for(int i = 0; i < n; i++)
cin >> pr[i];

int total_turnaround_time = 0;
int total_waiting_time = 0;
int total_response_time = 0;

int is_completed[100] = {0};


int current_time = 0;
int completed = 0;

while(completed != n)
{
int idx = -1;
int max = -1;
for(int i = 0; i < n; i++)
{
if(at[i] <= current_time && is_completed[i] == 0)
{
if(pr[i] > max)
{
max = pr[i];
idx = i;
}
if(pr[i] == max)
{
if(at[i] < at[idx])
{
max = pr[i];
idx = i;
}
}
}
}
if(idx != -1)
{
st[idx] = current_time;
ct[idx] = st[idx] + bt[idx];
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];
rt[idx] = st[idx] - at[idx];

total_turnaround_time += tat[idx];
total_waiting_time += wt[idx];
total_response_time += rt[idx];

is_completed[idx] = 1;
completed++;
current_time = ct[idx];
}
else
current_time++;
}

float avg_turnaround_time = (float) total_turnaround_time / n;


float avg_waiting_time = (float) total_waiting_time / n;
float avg_response_time = (float) total_response_time / n;

cout << endl << endl;

cout << "#P\t" << "AT\t" << "BT\t" << "PRI\t" << "ST\t" << "CT\t" << "TAT\
t" << "WT\t" << "RT\t" << "\n" << endl;

for(int i = 0; i < n; i++) {


cout << p[i] << "\t" << at[i] << "\t" << bt[i] << "\t" << pr[i] << "\t
" << st[i] << "\t" << ct[i] << "\t" << tat[i] << "\t" << wt[i] << "\t" << rt[I
] << "\t" << "\n" << endl;
}

cout << "Average Turnaround Time = " << avg_turnaround_time << endl;
cout << "Average Waiting Time = " << avg_waiting_time << endl;
cout << "Average Response Time = " << avg_response_time << endl;
return 0;
}

Output :
Implementation of Priority Scheduling Algorithm (Preemptive)
#include<bits/stdc++.h>
using namespace std;
#define mx 100

int main()
{
int n, at[mx], bt[mx], pr[mx], st[mx], ct[mx], tat[mx], wt[mx], rt[mx];
cout << "Enter total number of process : ";
cin >> n;

int p[n];
for(int i = 0; i < n; i++)
p[i] = i+1;

cout << "Enter Arrival time of the process : ";


for(int i = 0; i < n; i++)
cin >> at[i];

int burst_remaining[100];
cout << "Enter Burst time of the process : ";
for(int i = 0; i < n; i++)
{
cin >> bt[i];
burst_remaining[i] = bt[i];
}

cout << "Enter Priority of the process : ";


for(int i = 0; i < n; i++)
cin >> pr[i];

int total_turnaround_time = 0;
int total_waiting_time = 0;
int total_response_time = 0;

int is_completed[100]={0};
int current_time = 0;
int completed = 0;

while(completed != n)
{
int idx = -1;
int max = -1;
for(int i = 0; i < n; i++)
{
if(at[i] <= current_time && is_completed[i] == 0)
{
if(pr[i] > max)
{
max = pr[i];
idx = i;
}
if(pr[i] == max)
{
if(at[i] < at[idx])
{
max = pr[i];
idx = i;
}
}
}
}

if(idx != -1)
{
if(burst_remaining[idx] == bt[idx])
st[idx] = current_time;
burst_remaining[idx] -= 1;
current_time++;

if(burst_remaining[idx] == 0)
{
ct[idx] = current_time;
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];
rt[idx] = st[idx] - at[idx];

total_turnaround_time += tat[idx];
total_waiting_time += wt[idx];
total_response_time += rt[idx];

is_completed[idx] = 1;
completed++;
}
}
else
current_time++;
}

float avg_turnaround_time = (float) total_turnaround_time / n;


float avg_waiting_time = (float) total_waiting_time / n;
float avg_response_time = (float) total_response_time / n;

cout << endl << endl;


cout << "#P\t" << "AT\t" << "BT\t" << "PRI\t" << "ST\t" << "CT\t" << "TAT\
t" << "WT\t" << "RT\t" << "\n" << endl;

for(int i = 0; i < n; i++)


cout << p[i] << "\t" << at[i] << "\t" << bt[i] << "\t" << pr[i] << "\t
" << st[i] << "\t" << ct[i] << "\t" << tat[i] << "\t" << wt[i] << "\t" << rt[i
] << "\t" << "\n" << endl;

cout << "Average Turnaround Time = " << avg_turnaround_time << endl;
cout << "Average Waiting Time = " << avg_waiting_time << endl;
cout << "Average Response Time = " << avg_response_time << endl;

return 0;
}

Output :

You might also like