-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJF.c
60 lines (57 loc) · 1.39 KB
/
SJF.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
#include <stdio.h>
struct process
{
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_BURST(const void *a, const void *b)
{
unsigned int l = ((struct process*)a)->burst;
unsigned int r = ((struct process*)b)->burst;
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","P.NO.","BURST","WAIT","TAT");
printf("\n------------------------------------------------------------------");
for(i = 0; i < n ; i++)
printf("\n%10d | %10d | %10d | %10d",proc[i].pid,proc[i].burst,proc[i].wait,proc[i].TAT);
}
int main()
{
// SJF scheduling
int i,n;
printf("Enter the number of processes : ");
scanf("%d",&n);
struct process proc[n];
printf("Enter burst times of processes in order\n");
for(i=0;i<n;i++)
{
proc[i].pid = i+1;
scanf("%d",&proc[i].burst);
}
qsort(proc,n,sizeof(struct process),compar_BURST);
findWaitTime(n,proc);
for(i = 0; i < n ; i++)
proc[i].TAT = proc[i].burst + proc[i].wait;
// qsort(proc,n,sizeof(struct process),compar_ID);
printTable(n,proc);
printf("\n");
return 0;
}