#include <iostream>
#include <vector>
using namespace std;
float waitingTimeSJF(int *requestTimes, int *durations, int n)
{
int cpu_time=0;
float wait_time=0;
vector<int> vec_flag;
vec_flag.resize(n);
for(int i=0;i<n;i++){
vec_flag[i]=0;
}
vector<int> vec_ready(n);
int vec_ready_n=0;
int cur=0;
int next=0;
int min=101;
for(int i=0;i<n;i++){
wait_time=wait_time+(cpu_time-requestTimes[cur]);
cpu_time+=durations[cur];
vec_flag[cur]=1;
vec_ready_n=0;
for(int j=0;j<n;j++){
if(vec_flag[j]==0 && requestTimes[j]<=cpu_time){
vec_ready[vec_ready_n++]=j;
}
}
min=101;
for(int k=0;k<vec_ready_n;k++){
if(durations[vec_ready[k]]<min){
min=durations[vec_ready[k]];
next=vec_ready[k];
}
}
cur=next;
}
return (wait_time/n);
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[4]={0,2,4,5};
int b[4]={7,4,1,4};
float result=waitingTimeSJF(a,b,4);
cout<<result<<endl;
return 0;
}
- 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