OneCompiler

chit sjf

1636

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

// Process structure to hold process details
struct Process {
int id; // Process ID
int burstTime; // Total burst time required by the process
int arrivalTime; // Arrival time of the process
int remainingTime; // Remaining burst time (used for preemption)
int startTime; // Time at which process starts execution
int completionTime; // Time at which process finishes execution
int waitingTime; // Time process has been waiting
int turnAroundTime; // Total time spent by process from arrival to completion
};

// Comparison function to sort processes by arrival time
bool compareArrivalTime(const Process &p1, const Process &p2) {
return p1.arrivalTime < p2.arrivalTime;
}

// Comparison function to sort by remaining burst time (for preemption)
bool compareRemainingTime(const Process &p1, const Process &p2) {
return p1.remainingTime < p2.remainingTime;
}

// Function to implement SRTF Scheduling
void SRTF(vector<Process> &processes, int n) {
int currentTime = 0; // Time tracker
int completed = 0; // Counter for completed processes
vector<bool> isCompleted(n, false); // Track completion status

// While not all processes are completed
while (completed < n) {
    // Find the process with the smallest remaining time that has arrived
    Process *currentProcess = nullptr;

    // Check for processes that have arrived by current time
    for (int i = 0; i < n; i++) {
        if (!isCompleted[i] && processes[i].arrivalTime <= currentTime) {
            if (currentProcess == nullptr || processes[i].remainingTime < currentProcess->remainingTime) {
                currentProcess = &processes[i];
            }
        }
    }

    // If we found a process to run, execute it
    if (currentProcess != nullptr) {
        // Decrement the remaining time of the process
        currentProcess->remainingTime--;
        if (currentProcess->remainingTime == 0) {
            // Process has completed execution
            currentProcess->completionTime = currentTime + 1;
            currentProcess->turnAroundTime = currentProcess->completionTime - currentProcess->arrivalTime;
            currentProcess->waitingTime = currentProcess->turnAroundTime - currentProcess->burstTime;
            isCompleted[currentProcess->id - 1] = true;
            completed++;
        }
    }

    currentTime++; // Increment time
}

// Calculate and print the results
cout << "Process ID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\tCompletion Time\n";
int totalWaitingTime = 0, totalTurnAroundTime = 0;
for (int i = 0; i < n; i++) {
    cout << processes[i].id << "\t\t" << processes[i].burstTime << "\t\t"
         << processes[i].arrivalTime << "\t\t" << processes[i].waitingTime << "\t\t"
         << processes[i].turnAroundTime << "\t\t" << processes[i].completionTime << endl;
    totalWaitingTime += processes[i].waitingTime;
    totalTurnAroundTime += processes[i].turnAroundTime;
}

// Calculate average waiting time and turnaround time
cout << "\nAverage Waiting Time: " << (float)totalWaitingTime / n << endl;
cout << "Average Turnaround Time: " << (float)totalTurnAroundTime / n << endl;

}

int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;

vector<Process> processes(n);

// Input process details
for (int i = 0; i < n; i++) {
    processes[i].id = i + 1;
    cout << "Enter burst time and arrival time for process " << i + 1 << ": ";
    cin >> processes[i].burstTime >> processes[i].arrivalTime;
    processes[i].remainingTime = processes[i].burstTime;
}

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

// Perform SRTF scheduling
SRTF(processes, n);

return 0;

}