-
Notifications
You must be signed in to change notification settings - Fork 0
/
Non-preemptive Scheduling
86 lines (79 loc) · 2.14 KB
/
Non-preemptive Scheduling
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
#include<stdio.h>
#include <stdbool.h>
struct Process
{
int pid;
int bt;
};
bool comparison1(struct Process a,struct Process b)
{
return (a.bt < b.bt);
}
bool comparison2(struct Process a,struct Process b)
{
return (a.bt > b.bt);
}
void findWaitingTime(struct Process proc[], int n, int wt[])
{
wt[0] = 0;
int i=0;
for (i = 1; i < n ; i++ )
wt[i] = proc[i-1].bt + wt[i-1] ;
}
void findTurnAroundTime(struct Process proc[], int n,int wt[], int tat[])
{
int i;
for (i = 0; i < n ; i++)
tat[i] = proc[i].bt + wt[i];
}
void findavgTime(struct Process proc[], int n)
{
int wt[n], i, tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
printf("\nArrivalTime \t Burst time \t Waiting time \t Turn around time\n");
for (i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("\t%d\t\t%d\t\t%d\t\t%d\n",proc[i].pid,proc[i].bt,wt[i],tat[i]);
}
printf("Average waiting time = %f", (float)total_wt / (float)n);
printf("\nAverage turn around time = %f", (float)total_tat / (float)n);
}
void sort(struct Process *p,int n,int c){
int i=0,j=0;
for(i=0;i<n;i++){
for(j=i;j<n;j++){
bool swap = false;
if(c==1 && comparison1(p[i],p[j])){
swap = true;
}else if(c==2 && comparison2(p[i],p[j])){
swap = true;
}
if(swap==true){
struct Process t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
}
int main()
{
struct Process proc[] = {{6, 12}, {8, 16}, {1, 2}, {4, 8} , {3,6}};
int n = sizeof proc / sizeof proc[0],i;
printf( "\n\t\t==========================================\n\t\t\tNon-Prempritive Schedule\t\t\t\t\t\n\t\t==========================================\n\n\n");
sort(proc,n,2);
printf("Order in which process gets executed\n");
for (i = 0 ; i < n; i++)
printf("PID:%d\t",proc[i].pid);
findavgTime(proc, n);
printf("\n\n\t\t\tSJF\t\t\t\t\t\n");
sort(proc,n,1);
printf("Order in which process gets executed\n");
for (i = 0 ; i < n; i++)
printf("PID:%d\t",proc[i].pid);
findavgTime(proc, n);
return 0;
}