-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_new.cpp
159 lines (149 loc) · 2.76 KB
/
cpu_new.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<bits/stdc++.h>
int burst[100];
int wait[100];
int n;
int burstcpy[100];
int b[100];
void copy()
{
for(int i=0;i<n;i++)
{
burstcpy[i]=burst[i];
b[i]=i;
}
}
void swapp(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void sortt(int A[])
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(A[j]>A[j+1])
{
swapp(&burstcpy[j],&burstcpy[j+1]);
swapp(&b[j],&b[j+1]);
}
}
}
}
void wt_calc(int burstcpy[],int b[])
{
wait[0]=0;
float avgwait=0.0;
float avgtat=0.0;
for(int i=0;i<n;i++)
{
wait[i+1]=wait[i]+burstcpy[i];
printf("Wait time for process %d : %d\n",b[i],wait[i]);
avgwait+=wait[i];
printf("Turn arount time for process %d : %d\n",b[i],wait[i]+burstcpy[i]);
avgtat+=wait[i]+burstcpy[i];
}
printf("Average wait time: %.2f\n",avgwait/n);
printf("Average turn around time: %.2f\n",avgtat/n);
}
void FCFS()
{
copy();
wt_calc(burstcpy,b);
}
void SJF()
{
copy();
sortt(burstcpy);
wt_calc(burstcpy,b);
}
void prior()
{
int priority[100];
for(int i=0;i<n;i++)
{
printf("Enter the priority for process %d\n",i);
scanf("%d",&priority[i]);
}
copy();
sortt(priority);
wt_calc(burstcpy,b);
}
void RR()
{
copy();
int completion[100];
int tq;
int t=0;
int isComplete=0;
float avg_wait=0.0;
float avg_tat=0.0;
printf("Enter the time quantum\n");
scanf("%d",&tq);
for(int i=0;isComplete!=n;i=(i+1)%n)
{
if(burstcpy[i]>0)
{
if(burstcpy[i]>tq)
{
t+=tq;
burstcpy[i]-=tq;
}
else
{
t+=burstcpy[i];
burstcpy[i]=0;
completion[i]=t;
isComplete++;
}
}
}
for(int i=0;i<n;i++)
{
printf("Wait time for process %d is %d\n",i,completion[i]-burst[i]);
avg_wait+=completion[i]-burst[i];
printf("Turn around time for process %d is %d\n",i,completion[i]);
avg_tat+=completion[i];
}
printf("Average wait time: %.2f\n",avg_wait/n);
printf("Average turn around time %.2f\n",avg_tat/n);
}
int main()
{
int opt;
do {
printf("Enter number of process\n");
scanf("%d",&n);
printf("Enter the burst time\n");
for(int i=0;i<n;i++)
{
scanf("%d",&burst[i]);
b[i]=i;
}
printf("Enter the option\n");
printf("1-FCFS,2-SJF,3-Priority,4-Round Robin,5-exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
FCFS();
break;
case 2:
SJF();
break;
case 3:
prior();
break;
case 4:
RR();
break;
case 5:
break;
default:
printf("Invalid input\n");
break;
}
} while(opt!=5);
}