Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Process {
    char name;
    int arrival_time;
    int burst_time;
    int completion_time;
};

bool compareArrivalTime(const Process& p1, const Process& p2) {
    return p1.arrival_time < p2.arrival_time;
}

bool compareBurstTime(const Process& p1, const Process& p2) {
    return p1.burst_time < p2.burst_time;
}

int main() {
    vector<Process> processes = {
        {'P1', 0, 3, 0},
        {'P2', 1, 2, 0},
        {'P3', 2, 1, 0},
        {'P4', 3, 4, 0},
        {'P5', 4, 5, 0},
        {'P6', 17, 2, 0}
    };

    // Sort processes by arrival time
    sort(processes.begin(), processes.end(), compareArrivalTime);

    int current_time = 0;
    for (Process& p : processes) {
        if (p.arrival_time > current_time) {
            current_time = p.arrival_time;
        }
        p.completion_time = current_time + p.burst_time;
        current_time = p.completion_time;
    }

    // Sort processes by burst time for SJF
    sort(processes.begin(), processes.end(), compareBurstTime);

    // Print sorted processes based on SJF
    cout << "Sorted Processes based on SJF:" << endl;
    for (const Process& p : processes) {
        cout << "Process " << p.name << " (AT: " << p.arrival_time << ", BT: " << p.burst_time << ")" << endl;
    }

    // Create Gantt chart
    cout << "Gantt Chart:" << endl;
    cout << "|";
    current_time = 0;
    for (const Process& p : processes) {
        cout << "  " << p.name << "  |";
        current_time = p.completion_time;
    }
    cout << endl;

    return 0;
}