-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreemptive_priority.c
61 lines (58 loc) · 1.49 KB
/
Preemptive_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
#include<stdio.h>
struct process{
int id, at, bt, rt, pty, ct, wt, tat;
};
int main()
{
int n, i, j;
printf("Enter number of processess : ");
scanf("%d", &n);
struct process p[n];
int complete[n];
printf("Enter arrival times, burst times and priorities");
for(i = 0; i < n; i++){
p[i].id = i;
scanf("%d %d %d", &p[i].at, &p[i].bt, &p[i].pty);
complete[i] = 0;
p[i].rt = p[i].bt;
}
int completed = 0, T = 0;
while(completed != n){
int sj = -1, sp = 1000000;
for(i = 0; i < n; i++){
if(p[i].at <= T && p[i].pty < sp && !complete[i]){
sj = i;
sp = p[i].pty;
}
}
if(sj == -1){
printf("Idle ");
T++;
continue;
}
if(p[sj].rt == 1){
completed++;
complete[sj]++;
T++;
p[sj].rt--;
p[sj].ct = T;
}
else{
T++;
p[sj].rt--;
}
printf("%d ", sj);
}
printf("\n");
// for(i = 0; i < n - 1; i++)
// for(j = 0; j < n - i - 1; j++){
// if(p[j].ct > p[j + 1].ct){
// struct process temp = p[j];
// p[j] = p[j + 1];
// p[j + 1] = temp;
// }
// }
for(i = 0; i < n; i++){
printf("%d %d %d %d\n", p[i].id, p[i].at, p[i].bt, p[i].ct);
}
}