-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPriority.c
More file actions
84 lines (71 loc) · 1.94 KB
/
Priority.c
File metadata and controls
84 lines (71 loc) · 1.94 KB
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
// CPU-Scheduling-Algorithm-In-C
// Non Pre-emptive Priority Scheduling Algorithm
#include<stdio.h>
#include<malloc.h>
void main()
{
int n, i, j, pos, temp, *bt, *wt, *tat, *p, *pt;
float avgwt = 0, avgtat = 0;
printf("\n Enter the number of processes : ");
scanf("%d", &n);
p = (int*)malloc(n*sizeof(int));
bt = (int*)malloc(n*sizeof(int));
wt = (int*)malloc(n*sizeof(int));
tat = (int*)malloc(n*sizeof(int));
printf("\n Enter the burst time and priority for each process ");
for(i=0; i<n; i++)
{
printf("\n Burst time of P%d : ", i);
scanf("%d", &bt[i]);
printf(" Priority of P%d : ", i);
scanf("%d", &pt[i]);
p[i] = i;
}
for(i=0; i<n; i++)
{
pos = i;
for(j=i+1; j<n; j++)
{
if(pt[j] < pt[pos])
{
pos = j;
}
}
temp = pt[i];
pt[i] = pt[pos];
pt[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}
wt[0] = 0;
tat[0] = bt[0];
for(i=1; i<n; i++)
{
wt[i] = wt[i-1] + bt[i-1]; //waiting time[p] = waiting time[p-1] + Burst Time[p-1]
tat[i] = wt[i] + bt[i]; //Turnaround Time = Waiting Time + Burst Time
}
for(i=0; i<n; i++)
{
avgwt += wt[i];
avgtat += tat[i];
}
avgwt = avgwt/n;
avgtat = avgtat/n;
printf("\n PROCESS \t PRIORITY \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n");
printf("--------------------------------------------------------------\n");
for(i=0; i<n; i++)
{
printf(" P%d \t\t %d \t\t %d \t\t %d \t\t %d \n", p[i], pt[i], bt[i], wt[i], tat[i]);
}
printf("\n Average Waiting Time = %f \n Average Turnaround Time = %f \n", avgwt, avgtat);
printf("\n GAANT CHART \n");
printf("---------------\n");
for(i=0; i<n; i++)
{
printf(" %d\t|| P%d ||\t%d\n", wt[i], p[i], tat[i]);
}
}