TanZek's 技术空间

勇往直前,专注于技术...

首页 新随笔 联系 聚合 管理
  7 Posts :: 19 Stories :: 13 Comments :: 0 Trackbacks
1、进程调度中多级反馈队列调度算法的模拟实现
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 以上加号为进程执行流向
19 */
20 
21 int time[4]={1,2,3,4};  //各个队列的时间片 
22 char *str[4]={"first","second","third","fourth"};
23 queue<int> Rqueue[4];   //各个优先级队列定义 
24 
25 int execu(int n)   
26 //n是指哪一个队列 
27 //proc假设为一个进程 
28 {
29     int proc;   //代表一个进程 
30     while(!Rqueue[n-1].empty())
31     {
32         proc=Rqueue[n-1].front(); //取队首进程进行调度 
33         proc = proc - time[n-1]; //时间片轮转,执行调度后得到的进程 
34         cout<< proc <<" in the "<<str[n-1]<<" queue"<<endl;               
35         if( proc >=0 )   //假设进程时间片用完,但还没有执行完 
36         {
37           if(n-1<3)Rqueue[n].push(proc); //将其压入下一个低优先级的队列 
38           else
39             Rqueue[n-1].push(proc); //如果是最后一个队列,则压入本身 
40         }
41         Rqueue[n-1].pop();       //进行下一次队列调度
42     }
43     return 0;  //返回,代表该队列的所有进程均已调度过 
44 }
45 
46 int schedule(int& proc) //最初执行一个程序,创建一个进程,调度算法开始 
47 {
48     Rqueue[0].push(proc);   //将进程压入第一个队列 
49     for(int i=1; i<=4; i++)
50     execu(i);    
51     //当execu()返回时,意味着上一优先级队列里的进程均已调度完,进行下一个队列的调度 
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;        //新进程
65     schedule(proc); 
66     system("PAUSE");
67     return 0;
68 }
69 

以上即为我的实验成果,经编译运行后,证明是正确的。
如果中间还有不正确的,请不吝指教。
谢谢!
posted on 2006-05-30 13:46 TanZek 阅读(538) 评论(2)  编辑 收藏 引用 所属分类: Subject-Study

评论

# re: 操作系统实验(Operating System Experiment) 2007-06-19 10:46 李俊
怎么运行不了。大哥  回复  更多评论
  

# re: 操作系统实验(Operating System Experiment) 2007-06-19 10:47 李俊
出现这样的错误

--------------------Configuration: 11 - Win32 Debug--------------------
Compiling...
11.cpp
C:\Documents and Settings\Administrator\11.cpp(22) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\11.cpp(22) : error C2501: 'queue' : missing storage-class or type specifiers
C:\Documents and Settings\Administrator\11.cpp(22) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\11.cpp(29) : error C2065: 'Rqueue' : undeclared identifier
C:\Documents and Settings\Administrator\11.cpp(29) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\Administrator\11.cpp(29) : error C2228: left of '.empty' must have class/struct/union type
C:\Documents and Settings\Administrator\11.cpp(29) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

11.obj - 7 error(s), 0 warning(s)  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理