-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
72 lines (65 loc) · 1.89 KB
/
scheduler.cpp
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
#include "process.h"
struct CompareATime
{
bool operator()(Process const &p1, Process const &p2)
{
return p1.atime > p2.atime;
}
};
struct ComparePTime
{
bool operator()(Process const &p1, Process const &p2)
{
return p1.ptime > p2.ptime;
}
};
void scheduler(Process *ps, unsigned int len, int algo)
{
ofstream log("log.txt", std::ios_base::app); // append to log file
ofstream out("out.txt"); // create out file
// get run date
time_t now = time(0);
char *dt = ctime(&now);
log << "\n===================== Start Logging =====================\n\t" << dt << endl;
switch (algo)
{
case 1:
{
// create two queues one for get (A) and the another for (B)
priority_queue<Process, vector<Process>, CompareATime> q1, q2;
for (int i = 0; i < len; ++i)
{
q1.push(ps[i]);
q2.push(ps[i]);
}
// (A) print names of processes by order
for (int i = 0; i < len; ++i)
{
Process p = q2.top();
out<<p.getName();
q2.pop();
}
out<<endl;
// (B) print processes details by order
for (int i = 0; i < len; ++i)
{
Process p = q1.top();
int response, turnaround, delay, atime, ptime;
p.getTime(atime, ptime);
int static current = atime ; // current instance of time
response=delay=current-atime>0? current-atime : 0;
current += ptime;
turnaround=delay+ptime;
out << p.getName() << ": (response=" << response <<
", turnaround=" << turnaround <<
", delay=" << delay << ")\n";
log << p << endl;
q1.pop();
}
break;
}
default:
throw invalid_argument("received invalid Algo number");
break;
}
}