-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg.c
105 lines (90 loc) · 2.47 KB
/
alg.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
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
95
96
97
98
99
100
101
102
103
104
105
#include "alg.h"
Result* init_Result(){
Result* re = (Result*)malloc(sizeof(Result)*1);
re->finished = init_stack();
re->list = init_queue();
re->totaltime=0;
re->idle=0;
return re;
}
Running* init_Running(int timeq){
Running* re = (Running*)malloc(sizeof(Running)*1);
re->Process=NULL;
re->timeQuantum=timeq;
return re;
}
int RunningFinished(Running* running){
if(running->Process==NULL)return 1;
if(running->Process->CpuIO.LeftCpu<=0)return 1;
return 0;
}
void ChangeRunning(Running* running,heap* ready){
running->Process=heap_first(ready);
heap_pop(ready);
}
int IsAvailProcess(heap* ready,int IsFCFS,int time){
if(heap_first(ready)==NULL)return 0;
if(IsFCFS && heap_first(ready)->gettingT > time)return 0;
return 1;
}
int RunningInterrupted(Running* running,int bywhom){
process* rp = running->Process;
if(bywhom==0&&rp->CpuIO.TurnArray[rp->CpuIO.Index]==0)return 1;
return 0;
}
int RunningPreemptive(Running* running,heap* ready,int type){
if(type == 2){
//using priority
if(heap_first(ready)==NULL)return 0;
if(running->Process->priority > heap_first(ready)->priority)return 1;
return 0;
}
else{
if(heap_first(ready)==NULL)return 0;
if(running->Process->CpuIO.LeftCpu > heap_first(ready)->CpuIO.LeftCpu)return 1;
return 0;
}
}
void SpendTime(Running* running){
if(running->Process!=NULL){
running->Process->CpuIO.LeftCpu--;
running->Process->CpuIO.TurnArray[running->Process->CpuIO.Index]--;
running->timeQuantum--;
}
}
int AllFinished(Running* running,heap* heap1,heap* heap2){
if(!RunningFinished(running))return 0;
if(heap_first(heap1)!=NULL||heap_first(heap2)!=NULL)return 0;
return 1;
}
void FinishProcess(Running* running,int time,stack* finished){
running->Process->finishedT=time;
if(running->Process->CpuIO.LeftIO!=0)
running->Process->finishedT+=running->Process->CpuIO.LeftIO;
stack_insert(finished,running->Process);
running->Process=NULL;
}
void FromstandbyToready(heap* standby,heap* ready,int time){
process* st=NULL;
while(1){
st = heap_first(standby);
if(st == NULL)break;
if(st->gettingT>time)break;
heap_insert(ready,st);
heap_pop(standby);
}
}
void Running_free(Running* target){
process_free(target->Process);
free(target);
}
void freeall(Running* running, heap* heap1,heap* heap2){
heap_free(heap1);
heap_free(heap2);
}
void checkingList(Result* result,Running* running,int time){
int pid;
if(running->Process==NULL) pid=-1;
else pid = running->Process->processID;
queue_insert(result->list,pid,time,time+1);
}