0% found this document useful (0 votes)
40 views3 pages

SRTF Algo

The document contains C++ code to implement the shortest remaining time first (SRTF) CPU scheduling algorithm. It takes user input for the number of processes and their arrival times and burst times. It then calculates each process's start time, completion time, turnaround time, waiting time, and response time. It outputs these values for each process and calculates the average turnaround time, waiting time, and response time.

Uploaded by

Abdullah Zeeshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

SRTF Algo

The document contains C++ code to implement the shortest remaining time first (SRTF) CPU scheduling algorithm. It takes user input for the number of processes and their arrival times and burst times. It then calculates each process's start time, completion time, turnaround time, waiting time, and response time. It outputs these values for each process and calculates the average turnaround time, waiting time, and response time.

Uploaded by

Abdullah Zeeshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Shortest remaining time first Haris Ejaz 14th March 14, 2022

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

struct process {
int pid;
int arrival_time;
int burst_time;
int start_time;
int completion_time;
int turnaround_time;
int waiting_time;
int response_time;
};

int main() {

int n;
struct process p[100];
float avg_turnaround_time; int total_turnaround_time = 0;
float avg_waiting_time; int total_waiting_time = 0;
float avg_response_time; int total_response_time = 0;

int is_completed[100];
/*
Don't start to panic after looking at the line of code below. All it
is doing is initializing
the 'is_completed' array to 0. This is because we need to be to know
which process has been completed
which we will then change to 1 from 0.
*/
memset(is_completed,0,sizeof(is_completed));

cout << setprecision(2) << fixed;


/*
Don't start to panic after looking at the line of code below. All it
is doing is bounding the system
float variable to 2 decimal places. Just memorize it. It's fine even
if we don't do this.
*/

//Taking input from the user for the number of processes.


cout<<"Enter the number of processes: ";
cin>>n;

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


cout<<"Enter arrival time of process "<<i+1<<": ";
cin>>p[i].arrival_time;
cout<<"Enter burst time of process "<<i+1<<": ";
cin>>p[i].burst_time;
p[i].pid = i+1;
cout<<endl;
Shortest remaining time first Haris Ejaz 14th March 14, 2022

int current_time = 0; // total ongoing time of the system. For all


processes.
int completed = 0; // this variable will be used for iterating through
the processes.
int prev = 0; /* this variable is used in conjunction with the
current_time variable.
When current_time moves forward. This will stay one
step behind.
*/

while(completed != n) { // iterating through the processes for main


algorithm
int idx = -1;
int mn = 10000000;
for(int i = 0; i < n; i++) { // this for-loop is used for iterating
through the processes and finding out which process has the least burst time
among all the processes in the ready queue.
if(p[i].arrival_time <= current_time && is_completed[i] == 0) {
// if(p[i].arrival_time <= current_time) this line is because
we only consider the process that are already in the ready queue. simple
logic
if(p[i].burst_time < mn) {
mn = p[i].burst_time;
idx = i;
}
if(p[i].burst_time == mn) {
if(p[i].arrival_time < p[idx].arrival_time) {
mn = p[i].burst_time;
idx = i;
}
}
}
}
if(idx != -1) {
p[idx].start_time = current_time;
p[idx].completion_time = p[idx].start_time + p[idx].burst_time;
p[idx].turnaround_time = p[idx].completion_time -
p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
p[idx].response_time = p[idx].start_time - p[idx].arrival_time;

total_turnaround_time += p[idx].turnaround_time;
total_waiting_time += p[idx].waiting_time;
total_response_time += p[idx].response_time;

is_completed[idx] = 1;
completed++;
current_time = p[idx].completion_time;
prev = current_time;
}
else {
current_time++;
}

}
Shortest remaining time first Haris Ejaz 14th March 14, 2022

avg_turnaround_time = (float) total_turnaround_time / n;


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

cout<<endl<<endl;

cout<<"#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].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\t"<<p[i].st
art_time<<"\t"<<p[i].completion_time<<"\t"<<p[i].turnaround_time<<"\t"<<p[i].
waiting_time<<"\t"<<p[i].response_time<<"\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;

You might also like