-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs_practice.cpp
More file actions
74 lines (63 loc) · 1.76 KB
/
fcfs_practice.cpp
File metadata and controls
74 lines (63 loc) · 1.76 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
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
struct Process
{
int pid, at, bt;
};
bool sortArrive(Process &a, Process &b)
{
return a.at < b.at;
}
void printGanttChart(vector<int> &gantt, vector<int> &time)
{
cout << "\nGantt Chart:\n";
for (int pid : gantt)
cout << "| P" << pid << " ";
cout << "|\n";
for (int i = 0; i < time.size(); i++)
cout << time[i] << " ";
cout << "\n";
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> p(n);
for (int i = 0; i < n; i++)
{
cout << "Enter the arrival time for process " << i + 1 << endl;
cin >> p[i].at;
cout << "Enter the burst time for process " << i + 1 << endl;
cin >> p[i].bt;
}
sort(p.begin(), p.end(), sortArrive);
vector<int> gantt, time, wt(n), tat(n), ct(n);
int current = max(0, p[0].at);
time.push_back(current);
for (int i = 0; i < n; i++)
{
int current = max(current, p[i].at);
current += p[i].bt;
ct[i] = current;
tat[i] = ct[i] - p[i].at;
wt[i] = tat[i] - p[i].bt;
time.push_back(ct[i]);
p[i].pid = i;
gantt.push_back(p[i].pid);
}
float totalWT = 0, totalTAT = 0;
cout << "\n--- First Come First Serve (FCFS) ---\n";
cout << "PID\tAT\tBT\tCT\tTAT\tWT\n";
for (int i = 0; i < n; i++)
{
cout << "P" << p[i].pid << "\t" << p[i].at << "\t" << p[i].bt << "\t" << ct[i] << "\t"
<< tat[i] << "\t" << wt[i] << "\n";
totalWT += wt[i];
totalTAT += tat[i];
}
cout << "Average Waiting Time = " << totalWT / n << "\n";
cout << "Average Turnaround Time = " << totalTAT / n << "\n";
printGanttChart(gantt, time);
return 0;
}