forked from sreeharicodes/CPU-scheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.c
More file actions
134 lines (130 loc) · 1.92 KB
/
sjf.c
File metadata and controls
134 lines (130 loc) · 1.92 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<stdio.h>
#include<string.h>
struct process{
char name[10];
int at,bt,wt,tt,sta;
}p[20];
struct done{
char name[10];
int st,ct;
}d[20];
void read(int n)
{
int i;
printf("\nEnter the details of the processes\n");
for(i=0;i<n;i++)
{
printf("\nEnter the name:");
scanf("%s",p[i].name);
printf("\nEnter the at:");
scanf("%d",&p[i].at);
printf("\nEnter the bt:");
scanf("%d",&p[i].bt);
p[i].sta=0;
}
}
int sjf(int n)
{
int ls,i,j,k,num,idle,found;
idle=0;
for(ls=0,i=0,num=0;ls<n;)
{
found=0;
for(j=0;j<n;j++)
{
if(found==0 && p[j].sta==0 && p[j].at<=i)
{
found++;
k=j;
}
if(found>0 && p[j].sta==0 && p[j].at<=i)
{
if(p[j].bt<p[k].bt)
{
k=j;
}
}
}
if(found==0 && idle==0)
{
idle=1;
strcpy(d[num].name,"idle");
d[num].st=i;
i++;
}
else if(found==0 && idle==1)
{
i++;
}
else if(found>0)
{
if(idle==1)
{
d[num].ct=i;
num++;
idle=0;
}
strcpy(d[num].name,p[k].name);
d[num].st=i;
d[num].ct=i+p[k].bt;
num++;
p[k].wt=i-p[k].at;
p[k].tt=p[k].wt+p[k].bt;
i=i+p[k].bt;
p[k].sta=1;
ls++;
}
}
return(num);
}
void display(int n,int num)
{
float awt,att;
awt=att=0;
int i,j;float g,h;
g=h=0;
for(i=0;i<n;i++)
{
g+=p[i].wt;
h+=p[i].tt;
}
awt=g/n;
att=h/n;
printf("average wt =%f and tt =%f",awt,att);
printf("\nname\tat\tbt\twt\ttt\n");
for(j=0;j<n;j++)
{
printf("%s\t%d\t%d\t%d\t%d\n",p[j].name,p[j].at,p[j].bt,p[j].wt,p[j].tt);
}
printf("\n\nGantt Chart\n_______________________\n");
for(i=0;i<num;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<num;i++)
{
printf("|%s\t",d[i].name);
}
printf("|\n");
for(i=0;i<num;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<num;i++)
{
printf("%d\t",d[i].st);
}
printf("%d\n",d[i-1].ct);
printf("\n");
}
void main()
{
int n,i,j,k,p;
printf("\nEnter the number of process:");
scanf("%d",&n);
read(n);
p=sjf(n);
display(n,p);
}