-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority.c
64 lines (57 loc) · 1.65 KB
/
priority.c
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
#include "algorithms.h"
//The algorithm return total running time
Result* PRIORITYA(process* parray[],int nump,int IsPreemptive){
srand(time(NULL));
heap* standby;//"standby" heap is sorted by using getting time
heap* ready;//"ready" heap is sorted by using left cpu time
Running* running=init_Running(0);
Result* result = init_Result();
stack* finished = result -> finished;
int idle=-1;
int time=0;
//initialize heap
//first, make heap with key value 1(getting time)
//second, insert all processes in parray into heap
ready=init_heap(2,nump);
standby=init_heap(1,nump);
for(time=0;time<nump;time++)heap_insert(standby,parray[time]);
for(time=0;/*infinitely*/;time++){
FromstandbyToready(standby,ready,time);
if(running->Process==NULL){
idle++;
if(IsAvailProcess(ready,0,time))
ChangeRunning(running,ready);
}
else if(RunningFinished(running)){
FinishProcess(running,time,finished);
if(IsAvailProcess(ready,0,time)){
ChangeRunning(running,ready);
}
}
else if(RunningInterrupted(running,BYIO)){
interrupt_process(running->Process,BYIO,time);
heap_insert(standby,running->Process);
if(IsAvailProcess(ready,0,time)){
ChangeRunning(running,ready);
}
else{
running->Process=NULL;
}
}
else if(IsPreemptive&&RunningPreemptive(running,ready,PRIORITY)){
interrupt_process(running->Process,BYPROCESS,time);
heap_insert(standby,running->Process);
if(IsAvailProcess(ready,0,time)){
ChangeRunning(running,ready);
}
}
if(AllFinished(running,ready,standby)){
break;
}
checkingList(result,running,time);
SpendTime(running);
}
result->totaltime=time;
result->idle=idle;
return result;
}