0% found this document useful (0 votes)
4 views

Assignment 1

The document contains a C++ program for implementing the Highest Response Ratio Next (HRRN) scheduling algorithm for process management. It prompts the user to input the number of processes along with their arrival and burst times, calculates waiting and turnaround times, and outputs the execution order and average times. The program utilizes a struct to manage process attributes and employs sorting and iteration to determine the scheduling order based on response ratios.
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)
4 views

Assignment 1

The document contains a C++ program for implementing the Highest Response Ratio Next (HRRN) scheduling algorithm for process management. It prompts the user to input the number of processes along with their arrival and burst times, calculates waiting and turnaround times, and outputs the execution order and average times. The program utilizes a struct to manage process attributes and employs sorting and iteration to determine the scheduling order based on response ratios.
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/ 5

Name – Sakshi chauhan

SAP ID – 590018354
Batch – B3

Lab Assignment

HRRN (Highest Response Ratio Next) Scheduling

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

struct Process {
int id, arrival_time, burst_time, waiting_time, turnaround_time,
completion_time;
float response_ratio;
};

bool compareArrival(Process a, Process b) {


return a.arrival_time < b.arrival_time;
}
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;

vector<Process> processes(n);

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


processes[i].id = i;
cout << "Enter arrival time and burst time for process P" << i << ": ";
cin >> processes[i].arrival_time >> processes[i].burst_time;
}

sort(processes.begin(), processes.end(), compareArrival);

int current_time = 0, completed = 0;


float total_waiting_time = 0, total_turnaround_time = 0;
vector<int> execution_order;

while (completed < n) {


int idx = -1;
float max_response_ratio = -1;

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


if (processes[i].arrival_time <= current_time &&
processes[i].completion_time == 0) {
float waiting_time = current_time - processes[i].arrival_time;
float response_ratio = (waiting_time + processes[i].burst_time) /
(float)processes[i].burst_time;

if (response_ratio > max_response_ratio) {


max_response_ratio = response_ratio;
idx = i;
}
}
}

if (idx == -1) {
current_time++;
} else {
current_time += processes[idx].burst_time;
processes[idx].completion_time = current_time;
processes[idx].turnaround_time = processes[idx].completion_time -
processes[idx].arrival_time;
processes[idx].waiting_time = processes[idx].turnaround_time -
processes[idx].burst_time;

total_waiting_time += processes[idx].waiting_time;
total_turnaround_time += processes[idx].turnaround_time;
execution_order.push_back(processes[idx].id);
completed++;
}
}

cout << "\nExecution Order: ";


for (int id : execution_order) {
cout << "P" << id << " ";
}

cout << "\n\nProcess\tAT\tBT\tCT\tTAT\tWT" << endl;


for (const auto &p : processes) {
cout << "P" << p.id << "\t" << p.arrival_time << "\t" << p.burst_time <<
"\t" << p.completion_time
<< "\t" << p.turnaround_time << "\t" << p.waiting_time << endl;
}

cout << fixed << setprecision(2);


cout << "\nAverage Waiting Time: " << (total_waiting_time / n) << endl;
cout << "Average Turnaround Time: " << (total_turnaround_time / n) <<
endl;

return 0;
}

You might also like