生活随笔
收集整理的這篇文章主要介紹了
操作系统-高响应比优先调度算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
高響應比優先算法
算法思想
響應比算法:(服務時間+等待時間)/服務時間
先將作業按到達時間排序,并將條件最優的作業移動到第一位,之后每次調度作業都更新后面所有作業的響應比,并將最高的響應比往前移。
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std
;
struct work
{char name
[20];float arrive
;float service
;float finish
;float T
;float W
;float waitTime
;float H
;
};int main(){int n
;struct work z
[20];cin
>>n
;for(int i
=0;i
<n
;i
++){cin
>>z
[i
].name
; }for(int i
=0;i
<n
;i
++){cin
>>z
[i
].arrive
;}for(int i
=0;i
<n
;i
++){cin
>>z
[i
].service
;}struct work a
=z
[0];int min
=0;for(int i
=1;i
<n
;i
++){if(a
.arrive
>=z
[i
].arrive
&&a
.service
>z
[i
].service
){a
=z
[i
];min
=i
;}}swap(z
[0],z
[min
]);for(int i
=1;i
<n
-1;i
++){for(int j
=i
+1;j
<n
;j
++){if(z
[i
].arrive
>z
[j
].arrive
)swap(z
[i
],z
[j
]);}}z
[0].finish
=z
[0].arrive
+z
[0].service
;for(int i
=1;i
<n
-1;i
++){ if(z
[i
-1].finish
<z
[i
].arrive
){z
[i
].finish
=z
[i
].arrive
+z
[i
].service
;continue;}for(int l
=i
;l
<n
;l
++){z
[l
].waitTime
=z
[i
-1].finish
-z
[l
].arrive
;z
[l
].H
=(z
[l
].service
+z
[l
].waitTime
)/z
[l
].service
;}for(int j
=i
+1;j
<n
;j
++){if(z
[j
].arrive
<=z
[i
-1].finish
&&z
[j
].H
>z
[i
].H
){swap(z
[i
],z
[j
]);for(int k
=j
;k
>i
+1;k
--){if(z
[k
].arrive
<z
[k
-1].arrive
){swap(z
[k
],z
[k
-1]);}else{break;}}}}if(z
[i
].arrive
>z
[i
-1].finish
){z
[i
].finish
=z
[i
].arrive
+z
[i
].service
;}else{z
[i
].finish
=z
[i
-1].finish
+z
[i
].service
;}if(i
==n
-2){if(z
[i
+1].arrive
>z
[i
].finish
){z
[i
+1].finish
=z
[i
+1].arrive
+z
[i
+1].service
;}else{z
[i
+1].finish
=z
[i
].finish
+z
[i
+1].service
;} }} for(int i
=0;i
<n
;i
++){z
[i
].T
=z
[i
].finish
-z
[i
].arrive
;z
[i
].W
=z
[i
].T
/z
[i
].service
;}for(int i
=1;i
<n
-1;i
++){for(int j
=i
+1;j
<n
;j
++){if(z
[i
].arrive
>z
[j
].arrive
)swap(z
[i
],z
[j
]);}}cout
<<"作 業 名:";for(int i
=0;i
<n
;i
++){cout
<<z
[i
].name
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;cout
<<"到達時間:";for(int i
=0;i
<n
;i
++){cout
<<z
[i
].arrive
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;cout
<<"服務時間:";for(int i
=0;i
<n
;i
++){cout
<<z
[i
].service
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;cout
<<"完成時間:";for(int i
=0;i
<n
;i
++){cout
<<z
[i
].finish
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;cout
<<"周轉時間:";for(int i
=0;i
<n
;i
++){cout
<<z
[i
].T
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;cout
<<"帶權周轉時間:";for(int i
=0;i
<n
;i
++){cout
<<fixed
<<setprecision(2)<<z
[i
].W
;if(i
!=n
-1)cout
<<" ";}cout
<<endl
;return 0;
}
總結
以上是生活随笔為你收集整理的操作系统-高响应比优先调度算法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。