errr
dfddunknown
c_cpp
a year ago
1.9 kB
4
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 = 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() {
// Example usage:
int processes[] = {0, 1, 2}; // Process IDs
int burst_time[] = {10, 5, 8}; // Burst times
int quantum = 3; // Time quantum
int n = sizeof(processes) / sizeof(processes[0]);
findAvgTime(processes, n, burst_time, quantum);
return 0;
}Editor is loading...
Leave a Comment