-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCFS.cpp
More file actions
94 lines (80 loc) · 2.22 KB
/
FCFS.cpp
File metadata and controls
94 lines (80 loc) · 2.22 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <algorithm>
using namespace std;
class Process
{
public:
int Pid;
int AT;
int BT;
int CT;
int WT;
int TAT;
int THPT;
int CPU;
Process() : Pid(0), AT(0), BT(0), CT(0), WT(0), TAT(0), THPT(0), CPU(0)
{
}
Process(int pid, int at, int bt)
: Pid(pid), AT(at), BT(bt), CT(0), WT(0), TAT(0), THPT(0), CPU(0)
{
}
};
bool compare_AT(const Process &p1, const Process &p2)
{
return p1.AT < p2.AT;
}
void print_table(Process *processes, int n, float avg_WT, float avg_TAT, float THPT)
{
cout << "Pid\tAT\tBT\tCT\tTAT\tWT " << endl;
for (int i = 0; i < n; i++)
{
cout << processes[i].Pid << "\t" << processes[i].AT << "\t" << processes[i].BT << "\t" << processes[i].CT << "\t" << processes[i].TAT << "\t" << processes[i].WT << endl;
}
cout << "Average WT is: " << avg_WT << endl;
cout << "Average TAT is: " << avg_TAT << endl;
cout << "Throughput is: " << THPT << endl;
}
void fcfs_scheduling(Process *processes, int n)
{
int current_time = 0;
float total_WT = 0, total_TAT = 0;
for (int i = 0; i < n; i++)
{
if (current_time < processes[i].AT)
{
current_time = processes[i].AT;
}
current_time += processes[i].BT;
processes[i].CT = current_time;
processes[i].TAT = processes[i].CT - processes[i].AT;
processes[i].WT = processes[i].TAT - processes[i].BT;
total_WT += processes[i].WT;
total_TAT += processes[i].TAT;
}
float avg_WT = total_WT / n;
float avg_TAT = total_TAT / n;
float THPT = static_cast<float>(n) / current_time;
print_table(processes, n, avg_WT, avg_TAT, THPT);
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
Process *processes = new Process[n];
for (int i = 0; i < n; i++)
{
int pid, at, bt;
pid = i + 1;
cout << "Enter the AT of process " << pid << ": ";
cin >> at;
cout << "Enter the BT of process " << pid << ": ";
cin >> bt;
processes[i] = Process(pid, at, bt);
}
sort(processes, processes + n, compare_AT);
fcfs_scheduling(processes, n);
delete[] processes;
return 0;
}