-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.c
77 lines (71 loc) · 1.71 KB
/
fcfs.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
#include "algorithms.h"
#include <time.h>
//The algorithm return total running time
Result* FCFSA(process* parray[],int nump){
srand(time(NULL));
heap* ready;//"ready" heap is sorted by using getting time
process** standby = (process**)malloc(sizeof(process*)*(nump+1));
int first=0,last=0;
Running* running = init_Running(0);
Running* fake = 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(1,nump);
for(time=0;time<nump;time++){
if(parray[time]->arrivalT==0)
standby[last++]=parray[time];
else
heap_insert(ready,parray[time]);
}
for(time=0;/*infinitely*/;time++){
while(IsAvailProcess(ready,1,time)){
ChangeRunning(fake,ready);
standby[last]=fake->Process;
last++;
last%=nump+1;
}
if(running->Process==NULL){
idle++;
if(last!=first){
running->Process=standby[first];
first++;
first%=nump+1;
}
}
else if(RunningFinished(running)){
int i=0;
FinishProcess(running,time,finished);
if(last!=first){
running->Process=standby[first];
first++;
first%=nump+1;
}
}
else if(RunningInterrupted(running,BYIO)){
interrupt_process(running->Process,BYIO,time);
heap_insert(ready,running->Process);
if(last!=first){
running->Process=standby[first];
first++;
first%=nump+1;
}
else{
running->Process=NULL;
}
}
if(AllFinished(running,ready,ready)){
break;
}
checkingList(result,running,time);
SpendTime(running);
}
result->totaltime=time;
result->idle=idle;
printf("finished algorithm!!!\n");
return result;
}