Berechnung der durchschnittlichen Zeiten und Anzeige der endgültigen Ergebnisse
In diesem letzten Schritt werden wir die durchschnittliche Wartezeit und die durchschnittliche Durchlaufzeit für alle Prozesse berechnen. Diese Durchschnittswerte sind wichtige Metriken zur Bewertung der Effizienz eines Scheduling-Algorithmus.
Ändern wir unsere fcfs.cpp
-Datei, um diese Berechnungen hinzuzufügen und unser Programm abzuschließen:
#include <iostream>
#include <iomanip> // For formatted output
using namespace std;
int main() {
cout << "FCFS Scheduling Algorithm Implementation" << endl;
cout << "----------------------------------------" << endl;
// Variables declaration
int n; // Number of processes
int burst_time[20]; // Array to store burst time of each process
int waiting_time[20]; // Array to store waiting time of each process
int turnaround_time[20]; // Array to store turnaround time of each process
int total_waiting_time = 0;
int total_turnaround_time = 0;
float avg_waiting_time = 0.0;
float avg_turnaround_time = 0.0;
// Get the number of processes from the user
cout << "\nEnter the number of processes (maximum 20): ";
cin >> n;
// Input validation
if (n <= 0 || n > 20) {
cout << "Invalid number of processes. Please enter a value between 1 and 20." << endl;
return 1;
}
// Get burst time for each process
cout << "\nEnter the Burst Time for each process:" << endl;
for (int i = 0; i < n; i++) {
cout << "Process P" << i + 1 << ": ";
cin >> burst_time[i];
// Input validation for burst time
if (burst_time[i] <= 0) {
cout << "Burst time must be a positive integer. Please restart the program." << endl;
return 1;
}
}
// Calculate waiting time for each process
waiting_time[0] = 0; // First process has 0 waiting time
for (int i = 1; i < n; i++) {
waiting_time[i] = 0;
// Add burst times of all previous processes
for (int j = 0; j < i; j++) {
waiting_time[i] += burst_time[j];
}
}
// Calculate turnaround time for each process and total times
cout << "\nProcess\tBurst Time\tWaiting Time\tTurnaround Time" << endl;
cout << "----------------------------------------------------" << endl;
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
cout << "P" << i + 1 << "\t" << burst_time[i] << "\t\t"
<< waiting_time[i] << "\t\t" << turnaround_time[i] << endl;
}
// Calculate average waiting time and average turnaround time
avg_waiting_time = (float)total_waiting_time / n;
avg_turnaround_time = (float)total_turnaround_time / n;
// Display the results with two decimal places
cout << fixed << setprecision(2);
cout << "\nAverage Waiting Time: " << avg_waiting_time << " time units" << endl;
cout << "Average Turnaround Time: " << avg_turnaround_time << " time units" << endl;
// Visual representation of the FCFS schedule (Gantt chart in text)
cout << "\nGantt Chart:" << endl;
cout << " ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < burst_time[i]; j++) {
cout << "--";
}
}
cout << endl << "|";
for (int i = 0; i < n; i++) {
for (int j = 0; j < burst_time[i] - 1; j++) {
cout << " ";
}
cout << "P" << i + 1;
for (int j = 0; j < burst_time[i] - 1; j++) {
cout << " ";
}
cout << "|";
}
cout << endl << " ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < burst_time[i]; j++) {
cout << "--";
}
}
cout << endl;
cout << "0";
int current_time = 0;
for (int i = 0; i < n; i++) {
current_time += burst_time[i];
for (int j = 0; j < burst_time[i] * 2 - 1; j++) {
cout << " ";
}
if (current_time < 10) cout << " ";
cout << current_time;
}
cout << endl;
return 0;
}
Lassen Sie uns verstehen, was wir hinzugefügt haben:
- Zwei neue Variablen:
avg_waiting_time
und avg_turnaround_time
, um die durchschnittlichen Zeiten zu speichern.
- Berechnungen für die durchschnittlichen Zeiten:
- Durchschnittliche Wartezeit = Gesamtwartezeit / Anzahl der Prozesse
- Durchschnittliche Durchlaufzeit = Gesamtdurchlaufzeit / Anzahl der Prozesse
- Formatierte Ausgabe, um die durchschnittlichen Zeiten mit zwei Dezimalstellen anzuzeigen.
- Ein einfaches textbasiertes Gantt-Diagramm, um das FCFS-Scheduling zu visualisieren:
- Jeder Prozess wird durch seine ID (P1, P2, etc.) repräsentiert.
- Die Breite jedes Prozessblocks ist proportional zu seiner Burst-Zeit.
- Zeitmarkierungen werden unten angezeigt.
Kompilieren und führen wir unser finales Programm aus:
g++ fcfs.cpp -o fcfs
./fcfs
Versuchen Sie, die gleichen Daten wie zuvor einzugeben:
3
5
9
4
Sie sollten eine Ausgabe ähnlich der folgenden sehen:
FCFS Scheduling Algorithm Implementation
----------------------------------------
Enter the number of processes (maximum 20): 3
Enter the Burst Time for each process:
Process P1: 5
Process P2: 9
Process P3: 4
Process Burst Time Waiting Time Turnaround Time
----------------------------------------------------
P1 5 0 5
P2 9 5 14
P3 4 14 18
Average Waiting Time: 6.33 time units
Average Turnaround Time: 12.33 time units
Gantt Chart:
--------------------
| P1 | P2 | P3 |
--------------------
0 5 14 18
Betrachten wir die Ergebnisse:
- Durchschnittliche Wartezeit: (0 + 5 + 14) / 3 = 6,33 Zeiteinheiten
- Durchschnittliche Durchlaufzeit: (5 + 14 + 18) / 3 = 12,33 Zeiteinheiten
Das Gantt-Diagramm zeigt visuell, wie die Prozesse im Laufe der Zeit geplant werden:
- P1 läuft von Zeit 0 bis 5.
- P2 läuft von Zeit 5 bis 14.
- P3 läuft von Zeit 14 bis 18.
Dies schließt unsere Implementierung des FCFS-Scheduling-Algorithmus ab. Sie können mit verschiedenen Anzahlen von Prozessen und Burst-Zeiten experimentieren, um zu sehen, wie sich dies auf die durchschnittliche Wartezeit und Durchlaufzeit auswirkt.