-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprioirity.c
More file actions
70 lines (63 loc) · 1.62 KB
/
prioirity.c
File metadata and controls
70 lines (63 loc) · 1.62 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
#include <stdio.h>
struct process
{
unsigned int prior;
unsigned int pid;
unsigned int burst;
unsigned int wait;
unsigned int TAT;
};
int compar_ID(const void *a, const void *b)
{
unsigned int l = ((struct process*)a)->pid;
unsigned int r = ((struct process*)b)->pid;
return (l-r);
}
int compar_PRIOR(const void *a, const void *b)
{
unsigned int l = ((struct process*)a)->prior;
unsigned int r = ((struct process*)b)->prior;
return (l-r);
}
void findWaitTime(int n, struct process proc[n])
{
int i;
proc[0].wait = 0;
for(i = 1; i < n ; i++)
proc[i].wait = proc[i-1].wait + proc[i-1].burst;
}
void printTable(int n, struct process proc[n])
{
int i;
printf("\n%10s | %10s | %10s | %10s | %10s","P.No","PRIORITY","BURST","WAIT","TAT");
printf("\n--------------------------------------------------------------------------");
for(i = 0; i < n ; i++)
printf("\n%10d | %10d | %10d | %10d | %10d",proc[i].pid,proc[i].prior,proc[i].burst,proc[i].wait,proc[i].TAT);
}
int main()
{
// PRIORITY scheduling
int i,n;
printf("Enter the number of processes : ");
scanf("%d",&n);
struct process proc[n];
printf("Enter priority and burst times of processes in order\n");
for(i=0;i<n;i++)
{
proc[i].pid = i+1;
printf("Enter priority : ");
scanf("%d",&proc[i].prior);
printf("Enter Burst Time: ");
scanf("%d",&proc[i].burst);
}
qsort(proc,n,sizeof(struct process),compar_PRIOR);
findWaitTime(n,proc);
// Calculating TAT
for(i = 0; i < n ; i++)
proc[i].TAT = proc[i].burst + proc[i].wait;
// Sorting the output according to id
qsort(proc,n,sizeof(struct process),compar_ID);
printTable(n,proc);
printf("\n");
return 0;
}