-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduling-algorithms.cpp
More file actions
66 lines (57 loc) · 1.74 KB
/
scheduling-algorithms.cpp
File metadata and controls
66 lines (57 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
string id;
int arrival, burst, completion, turnaround;
};
// First-Come First-Serve
void FCFS(vector<Process> procs) {
cout << "\n=== FCFS Scheduling ===\n";
int time = 0;
for (auto &p : procs) {
if (time < p.arrival) time = p.arrival;
time += p.burst;
p.completion = time;
p.turnaround = p.completion - p.arrival;
}
cout << "PID\tAT\tBT\tCT\tTAT\n";
for (auto &p : procs)
cout << p.id << "\t" << p.arrival << "\t" << p.burst
<< "\t" << p.completion << "\t" << p.turnaround << "\n";
}
// Shortest Job First (non-preemptive)
void SJF(vector<Process> procs) {
cout << "\n=== SJF Scheduling ===\n";
int n = procs.size();
int time = 0, completed = 0;
vector<bool> done(n, false);
while (completed < n) {
int idx = -1, minBT = 1e9;
for (int i = 0; i < n; i++) {
if (!done[i] && procs[i].arrival <= time && procs[i].burst < minBT) {
minBT = procs[i].burst;
idx = i;
}
}
if (idx == -1) { time++; continue; }
time += procs[idx].burst;
procs[idx].completion = time;
procs[idx].turnaround = procs[idx].completion - procs[idx].arrival;
done[idx] = true;
completed++;
}
cout << "PID\tAT\tBT\tCT\tTAT\n";
for (auto &p : procs)
cout << p.id << "\t" << p.arrival << "\t" << p.burst
<< "\t" << p.completion << "\t" << p.turnaround << "\n";
}
int main() {
vector<Process> processes = {
{"P1", 0, 3}, {"P2", 2, 2}, {"P3", 4, 4}, {"P4", 5, 3}, {"P5", 6, 5}
};
FCFS(processes);
SJF(processes);
return 0;
}