1、進(jìn)程調(diào)度中多級反饋隊列調(diào)度算法的模擬實現(xiàn)
Following The Source Code:
?1?#include?<queue>
?2?#include?<iostream>
?3?#include?<cstdlib>
?4?
?5?
?6?/*
?7?--------+?|Ready?Queue?1|?--------+?to?CPU
?8????__________________|
?9???+
10?--------+?|Ready?Queue?2|?--------+?to?CPU
11????__________________|
12???+
13?--------+?|Ready?Queue?3|?--------+?to?CPU
14????__________________|
15???+
16?---------+?|Ready?Queue?4|?--------+?to?CPU
17???+__________________|
18?以上加號為進(jìn)程執(zhí)行流向
19?*/
20?
21?int?time[4]={1,2,3,4};??//各個隊列的時間片?
22?char?*str[4]={"first","second","third","fourth"};
23?queue<int>?Rqueue[4];???//各個優(yōu)先級隊列定義?
24?
25?int?execu(int?n)???
26?//n是指哪一個隊列?
27?//proc假設(shè)為一個進(jìn)程?
28?{
29?????int?proc;???//代表一個進(jìn)程?
30?????while(!Rqueue[n-1].empty())
31?????{
32?????????proc=Rqueue[n-1].front();?//取隊首進(jìn)程進(jìn)行調(diào)度?
33?????????proc?=?proc?-?time[n-1];?//時間片輪轉(zhuǎn),執(zhí)行調(diào)度后得到的進(jìn)程?
34?????????cout<<?proc?<<"?in?the?"<<str[n-1]<<"?queue"<<endl;???????????????
35?????????if(?proc?>=0?)???//假設(shè)進(jìn)程時間片用完,但還沒有執(zhí)行完?
36?????????{
37???????????if(n-1<3)Rqueue[n].push(proc);?//將其壓入下一個低優(yōu)先級的隊列?
38???????????else
39?????????????Rqueue[n-1].push(proc);?//如果是最后一個隊列,則壓入本身?
40?????????}
41?????????Rqueue[n-1].pop();???????//進(jìn)行下一次隊列調(diào)度
42?????}
43?????return?0;??//返回,代表該隊列的所有進(jìn)程均已調(diào)度過?
44?}
45?
46?int?schedule(int&?proc)?//最初執(zhí)行一個程序,創(chuàng)建一個進(jìn)程,調(diào)度算法開始?
47?{
48?????Rqueue[0].push(proc);???//將進(jìn)程壓入第一個隊列?
49?????for(int?i=1;?i<=4;?i++)
50?????execu(i);????
51?????//當(dāng)execu()返回時,意味著上一優(yōu)先級隊列里的進(jìn)程均已調(diào)度完,進(jìn)行下一個隊列的調(diào)度?
52?}
53?
54?
55?int?main(int?argc,?char*?argv[])
56?{
57?/*
58?????for(int?i=0;?i<100;?i++)
59?????{
60?????????int?n=int(rand())%4;????
61?????????Rqueue[n].push(int(rand())%3);
62?????}
63?*/
64?????int?proc=15;????????//新進(jìn)程
65?????schedule(proc);?
66?????system("PAUSE");
67?????return?0;
68?}
69?
以上即為我的實驗成果,經(jīng)編譯運(yùn)行后,證明是正確的。
如果中間還有不正確的,請不吝指教。
謝謝!