-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortestjobfirst.cpp
70 lines (60 loc) · 1.74 KB
/
shortestjobfirst.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
#include "shortestjobfirst.h"
ShortestJobFirst::ShortestJobFirst(bool isPreemptive)
{
this->isPreemptive=isPreemptive;
}
void ShortestJobFirst::addNewProcess(Process p)
{
ReadyQueue.push_back(p);
}
Process ShortestJobFirst::getNextProcess()
{
while(!FutureQueue.empty() && FutureQueue.first().getArrivalTime()<=currentTime)
{
ReadyQueue.push_back(FutureQueue.first());
FutureQueue.pop_front();
}
if(ReadyQueue.empty())
{
//Return NULL PROCESS
return Process();
}
qSort(ReadyQueue.begin(),ReadyQueue.end(),ShortestJobFirst::compare);
return ReadyQueue.front();
}
int ShortestJobFirst::executeCurrentProcess()
{
if(ReadyQueue.empty())
{
int tempCurrentTime=currentTime;
currentTime=FutureQueue.first().getArrivalTime();
return FutureQueue.first().getArrivalTime()-tempCurrentTime;
}
if(isPreemptive)
{
currentTime++;
ReadyQueue.first().setRemainingTime(ReadyQueue.first().getRemainingTime()-1);
if(ReadyQueue.first().getRemainingTime()==0)
{
totalWaitingTime+=currentTime-ReadyQueue.front().getArrivalTime()-ReadyQueue.front().getBurstTime();
ReadyQueue.pop_front();
}
return 1;
}
else
{
int currentProcessTime=ReadyQueue.front().getRemainingTime();
currentTime+=currentProcessTime;
totalWaitingTime+=currentTime-ReadyQueue.front().getArrivalTime()-ReadyQueue.front().getBurstTime();
ReadyQueue.pop_front();
return currentProcessTime;
}
}
QList<Process> ShortestJobFirst::getReadyQueue()
{
return ReadyQueue;
}
bool ShortestJobFirst::compare(Process p1, Process p2)
{
return p1.getRemainingTime()<p2.getRemainingTime();
}