-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjf_non.c
59 lines (57 loc) · 1.04 KB
/
sjf_non.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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void swap(int a[],int x, int y)
{
int temp=a[x];
a[x]=a[y];
a[y]=temp;
}
void main()
{
srand(time(0));
int n,i,bt[10],wt[10],tt[10],j,p[10],pos,temp;
float avgwt=0,avgtt=0;
//printf("\nEnter no. of processes:\n");
//scanf("%d",&n);
n=rand()%10+1;
printf("\nNo. of processes: %d",n);
//printf("\nEnter the burst times\n");
for(i=0;i<n;i++)
{
// printf("\nEnter burst time of p%d:\t",i);
// scanf("%d",&bt[i]);
p[i]=i+1;
bt[i]=rand()%10+1;
}
printf("\nProcess\tBurst time\tWaiting Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
swap(bt,i,pos);
swap(p,i,pos);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
avgwt+=wt[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+bt[i];
avgtt+=tt[i];
printf("\nP%d\t\t%d\t\t%d\t\t%d\n",p[i],bt[i],wt[i],tt[i]);
}
avgwt/=n;
avgtt/=n;
printf("\nAverage waiting time: %.3f\n",avgwt);
printf("\nAverage turnaround time: %.3f\n",avgtt);
}