fef
unknown
c_cpp
a year ago
2.3 kB
5
Indexable
#include <iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
int rem_bt[n]; // Remaining burst time for each process
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
while (true) {
bool done = true;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false; // There is a pending process
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else { // If remaining time is less than or equal to quantum
t += rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0; // Process is completed
}
}
}
if (done)
break;
}
}
void findTurnaroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
void findAvgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnaroundTime(processes, n, bt, wt, tat);
cout << "PN\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
cout << "P" << processes[i] << "\t" << bt[i] << "\t" << wt[i] << "\t" << tat[i] << endl;
}
cout << "Average waiting time: " << (float)total_wt / n << endl;
cout << "Average turnaround time: " << (float)total_tat / n << endl;
}
int main() {
int n;
// User input for number of processes
cout << "Enter the number of processes: ";
cin >> n;
int processes[n], burst_time[n];
// User input for process IDs and burst times
cout << "Enter the burst times for each process:\n";
for (int i = 0; i < n; i++) {
processes[i] = i; // Assign process IDs as 0, 1, ..., n-1
cout << "Burst time for Process " << processes[i] << ": ";
cin >> burst_time[i];
}
int quantum;
// User input for time quantum
cout << "Enter the time quantum: ";
cin >> quantum;
findAvgTime(processes, n, burst_time, quantum);
return 0;
}Editor is loading...
Leave a Comment