-
Notifications
You must be signed in to change notification settings - Fork 1
/
edf.cpp
80 lines (70 loc) · 1.62 KB
/
edf.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
73
74
75
76
77
78
79
80
/* Author : Vignesh Rajagopalan */
/* Earliest Deadline First in a non-preemptive environment (Aperiodic) */
#include<cstdio>
#include<algorithm>
using namespace std;
struct process{
char pr;
int A,ex,end,S;
}P[10];
bool operator < (const process& p1, const process& p2)
{return((p1.A == p2.A) ? (p1.S < p2.S) : (p1.A < p2.A));}
void get(int i){
scanf("%c%d%d%d\n",&P[i].pr,&P[i].A,&P[i].ex,&P[i].S);
P[i].end = P[i].A + P[i].ex;
}
void move(int i, int j){
if (i+1==j) swap(P[i],P[j]);
else{
process temp = P[j];
while(j<i){
P[j] = P[j-1];
j--;
}
P[i] = temp;
}
}
void reOrder(int N){
int current_end = P[0].end;
for(int i=1;i<N;i++){
if(P[i].S < current_end) P[i].A = -1; //Process misses the Starting deadline
else if(P[i].A < current_end){
if (i != N-1){
int j=i+1, min = i;
while(P[j].A<=current_end && j<N){
if(P[j].S<P[min].S) min = j;
j++;
}
move(i,min);
P[i].A = current_end;
P[i].end = P[i].A + P[i].ex;
current_end = P[i].end;
}
else{
P[i].A = current_end;
P[i].end = P[i].A + P[i].ex;
current_end = P[i].end;
}
}
else if(P[i].A > current_end){
P[i].end = P[i].A + P[i].ex;
current_end = P[i].end;
}
}
}
void put(int N){
printf("PROCESS\tSTART-TIME\tEND-TIME\n");
for(int i=0;i<N;i++){
if(P[i].A == -1) printf("-----------%c misses------------\n",P[i].pr);
else printf("%c\t%d\t\t%d\n",P[i].pr,P[i].A,P[i].end);
}
}
int main(){
int N;
scanf("%d\n",&N);
for(int i=0;i<N;i++) get(i);
sort(P,P+N); //Sorting the process according to arrival time.
reOrder(N); //Scheduling the process according to EDF
put(N);
return 0;
}