Implementieren des Round-Robin-Scheduling-Algorithmus

C++C++Beginner
Jetzt üben

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

In diesem Lab werden wir lernen, wie man den Round Robin Scheduling Algorithmus (Rundrobin-Scheduling-Algorithmus) in C++ implementiert. Der Round Robin Scheduling Algorithmus ist ein präemptiver Algorithmus, bei dem ein Prozess für ein festes Zeitintervall, das als Zeitquantum bekannt ist, ausgeführt wird. Wenn der Prozess seine Ausführung innerhalb des Zeitquants abgeschlossen hat, wird er beendet. Andernfalls wird er an das Ende der Warteschlange verschoben.

Erstellen einer neuen C++-Datei

Zuerst erstellen Sie eine neue C++-Datei im Verzeichnis ~/project. Sie können sie round_robin.cpp nennen.

$ cd ~/project
$ touch round_robin.cpp

Einbinden der erforderlichen Bibliotheken

Binden Sie die erforderlichen Bibliotheken in die Datei round_robin.cpp ein.

#include <iostream>
using namespace std;

Definieren der main()-Funktion

Definieren Sie die main()-Funktion und initialisieren Sie die Prozess-IDs (Prozessidentifikatoren), die Burst-Zeiten (Ausführungszeiten), das Zeitquantum und die Anzahl der Prozesse.

int main()
{
    // Process IDs
    int processes[] = { 1, 2, 3, 4 };

    // Burst time of all processes
    int burst_time[] = { 5, 9, 6, 8 };

    // Time quantum
    int quantum = 2;

    // Number of processes
    int n = sizeof processes / sizeof processes[0];

    return 0;
}

Implementieren der Funktion zur Berechnung der Wartezeit

Die Funktion findWaitingTime() wird verwendet, um die Wartezeit für alle Prozesse zu berechnen. Diese Funktion ist so definiert, dass sie die Prozesse im Round-Robin-Verfahren durchläuft.

void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{
    // Make a copy of burst times bt[] to store remaining
    // burst times.
    int rem_bt[n];
    for (int i = 0; i < n; i++)
        rem_bt[i] = bt[i];

    int t = 0; // Current time

    // Keep traversing processes in round-robin manner
    // until all of them are not done.
    while (1) {
        bool done = true;

        // Traverse all processes one by one repeatedly
        for (int i = 0; i < n; i++) {
            // If burst time of a process is greater than 0
            // then only need to process further
            if (rem_bt[i] > 0) {
                done = false; // There is a pending process

                /* If remaining burst time is greater than
                quantum then decrease the time slice
                from remaining burst time */
                if (rem_bt[i] > quantum) {
                    // Increase the value of t i.e. shows
                    // how much time a process has been processed
                    t += quantum;

                    // Decrease the burst_time of current process
                    // by quantum
                    rem_bt[i] -= quantum;
                }
                /* If remaining burst time is smaller than
                or equal to quantum then the remaining
                burst time for this process is 0.*/
                else {
                    // Increase the value of t i.e. shows
                    // how much time a process has been processed
                    t += rem_bt[i];

                    // Waiting time is current time minus time
                    // used by this process
                    wt[i] = t - bt[i];

                    // As the process gets fully executed
                    // make its remaining burst time = 0
                    rem_bt[i] = 0;
                }
            }
        }

        // If all processes are done
        if (done == true)
            break;
    }
}

Implementieren der Funktion zur Berechnung der Antwortzeit

Die Funktion findTurnAroundTime() wird verwendet, um die Antwortzeit (Turnaround-Zeit) für alle Prozesse zu berechnen.

void findTurnAroundTime(int processes[], int n,
                         int bt[], int wt[], int tat[])
{
    // calculating turnaround time by adding
    // bt[i] + wt[i]
    for (int i = 0; i < n; i++)
        tat[i] = bt[i] + wt[i];
}

Implementieren der Funktion zur Berechnung der durchschnittlichen Zeit

Die Funktion findavgTime() wird verwendet, um die durchschnittliche Wartezeit und die durchschnittliche Antwortzeit (Turnaround-Zeit) für alle Prozesse zu berechnen.

void findavgTime(int processes[], int n, int bt[],
                                                 int quantum)
{
    int wt[n], tat[n], total_wt = 0, total_tat = 0;

    // Function to find waiting time of all processes
    findWaitingTime(processes, n, bt, wt, quantum);

    // Function to find turn around time for all processes
    findTurnAroundTime(processes, n, bt, wt, tat);

    // Display processes along with all details
    cout << "Processes "
         << " Burst time "
         << " Waiting time "
         << " Turn around time\n";

    // Calculate total waiting time and total turn
    // around time
    for (int i = 0; i < n; i++) {
        total_wt = total_wt + wt[i];
        total_tat = total_tat + tat[i];
        cout << " " << i + 1 << "\t\t" << bt[i] << "\t "
             << wt[i] << "\t\t " << tat[i] << endl;
    }

    cout << "Average waiting time = "
         << (float)total_wt / (float)n;
    cout << "\nAverage turn around time = "
         << (float)total_tat / (float)n;
}

Aufrufen der findavgTime()-Funktion

Rufen Sie die findavgTime()-Funktion auf, um die durchschnittliche Wartezeit und die durchschnittliche Antwortzeit (Turnaround-Zeit) für die Prozesse zu berechnen.

int main()
{
    int processes[] = { 1, 2, 3, 4 };

    // Burst time of all processes
    int burst_time[] = { 5, 9, 6, 8 };

    // Time quantum
    int quantum = 2;

    // Number of processes
    int n = sizeof processes / sizeof processes[0];

    // Function to find average time
    findavgTime(processes, n, burst_time, quantum);

    return 0;
}

Um den Code in einem Terminal auszuführen, führen Sie den folgenden Befehl im Verzeichnis ~/project aus.

$ g++ round_robin.cpp -o round_robin &&./round_robin

Zusammenfassung

In diesem Lab haben wir gelernt, wie man den Round-Robin-Scheduling-Algorithmus (Rotierendes Verfahren zur Prozessplanung) in C++ implementiert. Dieser Algorithmus wird verwendet, um Prozesse in einem Betriebssystem für ein Zeitintervall, das als Zeitquantum (Time Quantum) bekannt ist, zu planen. Wir haben auch gesehen, wie man die durchschnittliche Wartezeit und die durchschnittliche Antwortzeit (Turnaround-Zeit) für eine Gruppe von Prozessen berechnet.