0% found this document useful (0 votes)
11 views2 pages

P 10

The document contains a C++ program that implements a priority scheduling algorithm for processes. It defines a 'Process' structure and includes functions to calculate waiting time and turnaround time for each process based on their arrival time, burst time, and priority. The program prompts the user to input process details and then displays the results in a formatted table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

P 10

The document contains a C++ program that implements a priority scheduling algorithm for processes. It defines a 'Process' structure and includes functions to calculate waiting time and turnaround time for each process based on their arrival time, burst time, and priority. The program prompts the user to input process details and then displays the results in a formatted table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>

using namespace std;

struct Process {
int id;
int arrivalTime;
int burstTime;
int priority;
};

bool compareByPriority(const Process& p1, const Process& p2) {


return p1.priority > p2.priority || (p1.priority == p2.priority && p1.arrivalTime
< p2.arrivalTime);
}

void calculateWaitingTime(vector<Process>& processes) {


int currentTime = 0;
for (int i = 0; i < processes.size(); ++i) {
// Find the first ready process (arrival time <= current time) with highest
priority
int nextProcess = -1;
for (int j = 0; j < processes.size(); ++j) {
if (processes[j].arrivalTime <= currentTime && (nextProcess == -1 ||
compareByPriority(processes[j], processes[nextProcess]))) {
nextProcess = j;
}
}
if (nextProcess == -1) {
currentTime = processes[i].arrivalTime;
continue;
}
for (int j = 0; j < processes.size(); ++j) {
if (j != nextProcess) {
processes[j].waitingTime += processes[nextProcess].burstTime;
}
}
currentTime += processes[nextProcess].burstTime;
}
}

void calculateTurnaroundTime(vector<Process>& processes) {


for (auto& process : processes) {
process.turnaroundTime = process.burstTime + process.waitingTime;
}
}

void printResults(const vector<Process>& processes) {


cout << "Process | Arrival Time | Burst Time | Priority | Waiting Time |
Turnaround Time" << endl;
cout <<
"-------|--------------|-----------|----------|-------------|------------------" <<
endl;
for (const auto& process : processes) {
cout << process.id << " | " << process.arrivalTime << " | " <<
process.burstTime << " | " << process.priority << " | " <<
process.waitingTime << " | " << process.turnaroundTime << endl;
}
}

int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;

vector<Process> processes(n);

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


cout << "Process " << (i + 1) << ":" << endl;
cout << "ID: "; cin >> processes[i].id;
cout << "Arrival Time: "; cin >> processes[i].arrivalTime;
cout << "Burst Time: "; cin >> processes[i].burstTime;
cout << "Priority: "; cin >> processes[i].priority;
}

calculateWaitingTime(processes);
calculateTurnaroundTime(processes);
printResults(processes);

return 0;
}

You might also like