0% found this document useful (0 votes)
57 views2 pages

SJF

This C++ code implements the shortest job first (SJF) scheduling algorithm. It takes as input a vector of processes with their arrival times and burst times. It sorts the processes by arrival time and then iteratively selects the process with the shortest remaining burst time to execute. It calculates the waiting time and turnaround time for each process.

Uploaded by

ay773520785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

SJF

This C++ code implements the shortest job first (SJF) scheduling algorithm. It takes as input a vector of processes with their arrival times and burst times. It sorts the processes by arrival time and then iteratively selects the process with the shortest remaining burst time to execute. It calculates the waiting time and turnaround time for each process.

Uploaded by

ay773520785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>
#include <algorithm>
using namespace std;

struct Process {
int id;
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;};

bool compareArrivalTime(const Process& p1, const Process& p2) {


return p1.arrivalTime < p2.arrivalTime;
}
void sjfScheduling(vector<Process>& processes) {
sort(processes.begin(), processes.end(), compareArrivalTime);
int n = processes.size();
vector<bool> completed(n, false);
int completedProcesses = 0;
int currentTime = 0;
while (completedProcesses < n) {
int shortestBurstTime = INT_MAX;
int shortestBurstTimeJob = -1;

for (int i = 0; i < n; i++) {


if (!completed[i] && processes[i].arrivalTime <= currentTime &&
processes[i].burstTime < shortestBurstTime) {
shortestBurstTime = processes[i].burstTime;
shortestBurstTimeJob = i;
}}

if (shortestBurstTimeJob == -1) {
currentTime++;
continue;
}

completed[shortestBurstTimeJob] = true;
completedProcesses++;
processes[shortestBurstTimeJob].waitingTime = currentTime + 0;
processes[shortestBurstTimeJob].turnaroundTime =
processes[shortestBurstTimeJob].waitingTime +
processes[shortestBurstTimeJob].burstTime;
currentTime += processes[shortestBurstTimeJob].burstTime;
}

cout << "Process\tArrival Time\tBurst Time \n";


for (int i = 0; i < n; i++) {
cout << processes[i].id <<"\t\t"
<< processes[i].burstTime << "\t\t" << processes[i].waitingTime << "\
t\t"<< "\n";
}}

int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Enter burst time for process " << i + 1 << ": ";
cin >> processes[i].burstTime;
getchar();
}
sjfScheduling(processes);

return 0;
}

You might also like