1. 程式人生 > >程序排程演算法模擬

程序排程演算法模擬

最近學了作業系統的程序排程的各種演算法,手癢實現了一下,

僅供參考,若有bug,請指出.^V^.

 

先到先服務(FCFS)

 1 //先來先服務排程演算法
 2 #include <bits/stdc++.h>
 3 #define TIME 500 //限制最長程序執行時間
 4 #define N 100
 5 #define state int
 6 using namespace std;
 7 struct PCB{
 8     string name;        //程序名稱
 9     int pos;            //程序順序
10
double arrival_t; //到達的時間 11 double start_t; //開始時間 12 double end_t; //結束時間 13 double service_t; //服務時間 14 double around_t; //週轉時間 15 double val_around_t; //帶權週轉時間 16 // bool operator<(const PCB b)const 17 // { 18 // return arrival_t>b.arrival_t;
19 // } 20 }pcb[N]; 21 int num; 22 double cur_t = 0; 23 bool cmp(PCB a, PCB b){ 24 return a.arrival_t < b.arrival_t; 25 } 26 void fcfs(){ 27 cout<<"FCFS演算法執行如下:\n"; 28 sort(pcb, pcb + num, cmp); 29 for(int i = 0; i < num; ++i){ 30 pcb[i].start_t = max(cur_t, pcb[i].arrival_t); //
開始時間 31 pcb[i].end_t = pcb[i].start_t + pcb[i].service_t; //結束時間 32 pcb[i].around_t = pcb[i].end_t - pcb[i].arrival_t; // 週轉時間 33 pcb[i].val_around_t = pcb[i].around_t/pcb[i].service_t; //帶權週轉時間 34 cur_t = pcb[i].end_t; 35 } 36 } 37 38 void init(){ 39 cout<<"輸入程序個數: "; 40 cin>>num; 41 for(int i = 0; i < num; ++i){ 42 cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間"<<endl; 43 cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t; 44 pcb[i].pos = i + 1; 45 } 46 } 47 48 void output(){ 49 cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<endl; 50 for(int i = 0; i < num; ++i){ 51 cout<<pcb[i].name<<" "<<pcb[i].arrival_t<<" "<<pcb[i].start_t<<" "<<pcb[i].end_t<<endl; 52 } 53 } 54 int main(){ 55 init(); 56 fcfs(); 57 output(); 58 return 0; 59 }

 

最短作業優先排程演算法(SJF)

 1 //短作業優先排程演算法 (非搶佔式)
 2 #include <bits/stdc++.h>
 3 #define TIME 500 //限制最長程序執行時間
 4 #define N 100
 5 #define state int
 6 using namespace std;
 7 double cur_t = 0;
 8 struct PCB{
 9     string name;        //程序名稱
10     int pos;            //程序順序
11     double arrival_t;      //到達的時間
12     double start_t;        //開始時間
13     double end_t;          //結束時間
14     double service_t;      //服務時間
15     double around_t;       //週轉時間
16     double val_around_t;   //帶權週轉時間   週轉時間 / 服務時間
17 
18     bool operator < (const PCB &a) const{
19         if(service_t == a.service_t)
20             return arrival_t > a.arrival_t;
21         return service_t > a.service_t;
22     }
23 
24 }pcb[N];
25 priority_queue<PCB> q;
26 bool vis[N];
27 int num;
28 bool cmp(PCB a, PCB b){
29         if(a.arrival_t == b.arrival_t)
30             return a.service_t < b.service_t; 
31     return a.arrival_t < b.arrival_t;
32 }
33 
34 void init(){
35     cout<<"輸入程序個數: ";
36     cin>>num;
37     for(int i = 0; i < num; ++i){
38         cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間"<<endl;
39         cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t;
40         pcb[i].pos = i + 1 ;
41     }
42 }
43 
44 void sjf(){
45     //short job first
46     sort(pcb, pcb + num, cmp);
47     q.push(pcb[0]);
48     int index = 0;
49     for(int i = 1; i < num; ++i){
50         while( !q.empty() ){
51             PCB cnt = q.top();
52             q.pop();
53             cnt.start_t = max(cur_t, cnt.arrival_t);
54             cnt.end_t = cnt.start_t + cnt.service_t;
55             cnt.around_t = cnt.end_t - cnt.arrival_t;
56             cnt.val_around_t = cnt.around_t / cnt.service_t;
57             for(; i < num; ++i){
58                 if(pcb[i].arrival_t <= cnt.end_t ){
59                     q.push(pcb[i]);
60                 }else{
61                     break;
62                 }
63             }
64             cur_t = cnt.end_t;
65             pcb[index++] = cnt;
66             // cout<< pcb[index -1].end_t<<" ** "<<i<<endl;
67         }
68         bool flag = false;
69         if( i < num ){
70             q.push(pcb[i]);
71             flag = true;
72         }
73         if(!flag) break;
74     }
75 }
76 
77 void output(){
78     cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<endl;
79     for(int i = 0; i < num; ++i){
80         cout<<pcb[i].name<<"      "<<pcb[i].arrival_t<<"       "<<pcb[i].start_t<<"       "<<pcb[i].end_t<<endl;
81     }
82 }
83 
84 int main(){
85     init();
86     sjf();
87     output();
88     return 0;
89 }

 

高響應比優先排程

 1 //高響應比優先順序排程演算法(非搶佔式)
 2 #include <bits/stdc++.h>
 3 #define TIME 500 //限制最長程序執行時間
 4 #define N 100
 5 #define state int
 6 using namespace std;
 7 double cur_t = 0;
 8 struct PCB{
 9     string name;        //程序名稱
10     int pos;            //程序順序
11     double arrival_t;      //到達的時間
12     double start_t;        //開始時間
13     double end_t;          //結束時間
14     double service_t;      //服務時間
15     // double around_t;       //週轉時間
16     // double val_around_t;   //帶權週轉時間   週轉時間 / 服務時間
17     double priority;
18 
19     bool operator < (const PCB &a) const{
20         if(priority == a.priority)
21             return arrival_t > a.arrival_t;
22         return priority < a.priority;
23     }
24 
25 }pcb[N];
26 priority_queue<PCB> q;
27 bool vis[N];
28 int num;
29 bool cmp(PCB a, PCB b){
30     return a.arrival_t < b.arrival_t; // 到達時間早的先執行
31 }
32 
33 void init(){
34     cout<<"輸入程序個數: ";
35     cin>>num;
36     for(int i = 0; i < num; ++i){
37         cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間"<<endl;
38         cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t;
39         pcb[i].pos = i + 1 ;
40     }
41 }
42 
43 void hrrn(){
44     //高響應比優先順序排程演算法
45     // 優先順序 = (作業已等待的時間+作業服務時間) / 作業服務時間
46     sort(pcb, pcb + num, cmp);
47     q.push(pcb[0]);
48     // vis[pcb[0].pos] = true;
49     int index = 0;    
50     for(int i = 1; i < num; ++i){
51         while( !q.empty() ){
52             PCB cnt = q.top();
53             q.pop();
54             cnt.start_t = max(cnt.arrival_t, cur_t);
55             cnt.end_t = cnt.start_t + cnt.service_t;
56             for(; i < num; ++i){
57                 if(pcb[i].arrival_t <= cnt.end_t){
58                     // vis[pcb[i].pos] = true;
59                     q.push(pcb[i]);
60                 }else{
61                     break;
62                 }
63             }
64             cur_t = cnt.end_t;
65             pcb[index++] = cnt;
66 
67             PCB left[N];
68             int xn = 0;
69             while( !q.empty() ){
70                 cnt = q.top(), q.pop();
71                 left[xn++] = cnt;
72             }
73             for(int j = 0; j < xn; ++j){
74                 left[j].priority = (cur_t - left[j].arrival_t + left[j].service_t)/left[j].service_t;
75                 q.push(left[j]);
76             }
77         }
78         bool flag = false;
79         if( i < num ){
80             q.push(pcb[i]);
81             flag = true;
82         }
83         
84         if(!flag) break;
85     }
86 }
87 
88 void output(){
89     cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<endl;
90     for(int i = 0; i < num; ++i){
91         cout<<pcb[i].name<<"      "<<pcb[i].arrival_t<<"       "<<pcb[i].start_t<<"       "<<pcb[i].end_t<<endl;
92     }
93 }
94 int main(){
95     init();
96     hrrn();
97     output();
98     return 0;
99 }
 1 //高響應比優先順序排程演算法(非搶佔式)
 2 #include <bits/stdc++.h>
 3 #define TIME 500 //限制最長程序執行時間
 4 #define N 100
 5 #define state int
 6 using namespace std;
 7 double cur_t = 0;
 8 struct PCB{
 9     string name;        //程序名稱
10     int pos;            //程序順序
11     double arrival_t;      //到達的時間
12     double start_t;        //開始時間
13     double end_t;          //結束時間
14     double service_t;      //服務時間
15     // double around_t;       //週轉時間
16     // double val_around_t;   //帶權週轉時間   週轉時間 / 服務時間
17     double priority;
18 
19     bool operator < (const PCB &a) const{
20         if(priority == a.priority)
21             return arrival_t > a.arrival_t;
22         return priority < a.priority;
23     }
24 
25 }pcb[N];
26 priority_queue<PCB> q;
27 bool vis[N];
28 int num;
29 bool cmp(PCB a, PCB b){
30     return a.arrival_t < b.arrival_t; // 到達時間早的先執行
31 }
32 
33 void init(){
34     cout<<"輸入程序個數: ";
35     cin>>num;
36     for(int i = 0; i < num; ++i){
37         cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間"<<endl;
38         cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t;
39         pcb[i].pos = i + 1 ;
40     }
41 }
42 
43 void hrrn(){
44     //高響應比優先順序排程演算法
45     // 優先順序 = (作業已等待的時間+作業服務時間) / 作業服務時間
46     sort(pcb, pcb + num, cmp);
47     q.push(pcb[0]);
48     // vis[pcb[0].pos] = true;
49     int index = 0;    
50     for(int i = 1; i < num; ++i){
51         while( !q.empty() ){
52             PCB cnt = q.top();
53             q.pop();
54             cnt.start_t = max(cnt.arrival_t, cur_t);
55             cnt.end_t = cnt.start_t + cnt.service_t;
56             for(; i < num; ++i){
57                 if(pcb[i].arrival_t <= cnt.end_t){
58                     // vis[pcb[i].pos] = true;
59                     q.push(pcb[i]);
60                 }else{
61                     break;
62                 }
63             }
64             cur_t = cnt.end_t;
65             pcb[index++] = cnt;
66 
67             PCB left[N];
68             int xn = 0;
69             while( !q.empty() ){
70                 cnt = q.top(), q.pop();
71                 left[xn++] = cnt;
72             }
73             for(int j = 0; j < xn; ++j){
74                 left[j].priority = (cur_t - left[j].arrival_t + left[j].service_t)/left[j].service_t;
75                 q.push(left[j]);
76             }
77         }
78         bool flag = false;
79         if( i < num ){
80             q.push(pcb[i]);
81             flag = true;
82         }
83         
84         if(!flag) break;
85     }
86 }
87 
88 void output(){
89     cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<endl;
90     for(int i = 0; i < num; ++i){
91         cout<<pcb[i].name<<"      "<<pcb[i].arrival_t<<"       "<<pcb[i].start_t<<"       "<<pcb[i].end_t<<endl;
92     }
93 }
94 int main(){
95     init();
96     hrrn();
97     output();
98     return 0;
99 }

 

搶佔式優先比排程演算法

  1 //搶佔式優先比排程演算法
  2 #include <bits/stdc++.h>
  3 #define TIME 500 //限制最長程序執行時間
  4 #define N 100
  5 #define state int
  6 using namespace std;
  7 double cur_t = 0;
  8 struct PCB{
  9     string name;        //程序名稱
 10     int pos;            //程序順序
 11     double arrival_t;      //到達的時間
 12     double start_t;        //開始時間
 13     double end_t;          //結束時間
 14     double service_t;      //服務時間
 15     // double around_t;       //週轉時間
 16     // double val_around_t;   //帶權週轉時間   週轉時間 / 服務時間
 17     double priority;
 18 
 19     bool operator < (const PCB &a) const{
 20         if(priority == a.priority)
 21             return arrival_t > a.arrival_t;
 22         return priority < a.priority;
 23     }
 24 
 25 }pcb[N];
 26 
 27 priority_queue<PCB> q;
 28 bool vis[N];
 29 int num;
 30 bool cmp(PCB a, PCB b){
 31     if(a.arrival_t == b.arrival_t)
 32         return a.priority > b.arrival_t;
 33     return a.arrival_t < b.arrival_t; // 到達時間早的先執行
 34 }
 35 
 36 void init(){
 37     cout<<"輸入程序個數: ";
 38     cin>>num;
 39     for(int i = 0; i < num; ++i){
 40         cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間, 優先順序"<<endl;
 41         cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t>>pcb[i].priority;
 42         pcb[i].pos = i + 1 ;
 43     }
 44 }
 45 
 46 void pps(){
 47     //搶佔式優先順序排程演算法
 48     sort(pcb, pcb + num, cmp);
 49     int index = 0;
 50     q.push(pcb[0]);
 51     vis[pcb[0].pos] = true;
 52     cur_t = max(pcb[0].arrival_t, cur_t);
 53     for(int i = 1; i < num; ++i){
 54         while( !q.empty() ){
 55             PCB cnt = q.top();
 56             q.pop();
 57             cnt.start_t = cnt.start_t == 0 ? cur_t : cnt.start_t;
 58             cnt.end_t = cur_t + cnt.service_t;
 59             bool OK = true;
 60             for(int j = 0 ; j < num; ++j){
 61                 if(pcb[j].arrival_t <= cnt.end_t){
 62                     if( !vis[pcb[j].pos] && pcb[j].priority > cnt.priority){
 63                         vis[pcb[j].pos] = true;
 64                         q.push(pcb[j]);
 65                         cnt.end_t = pcb[j].arrival_t;
 66                         cnt.service_t = cur_t + cnt.service_t - cnt.end_t; 
 67                         q.push(cnt);
 68                         cur_t = pcb[j].arrival_t;
 69                         OK = false;
 70                         break; 
 71                     }else if(!vis[pcb[j].pos]){
 72                         vis[pcb[j].pos] = true;
 73                         q.push(pcb[j]);
 74                     }
 75                 }else{
 76                     break;
 77                 }
 78             }
 79             if( OK ){
 80                 pcb[index++] = cnt;
 81                 cur_t += cnt.service_t;
 82             }
 83         }
 84         bool flag = false;
 85         for(int j = 0; j < num ;j++){
 86             if( !vis[pcb[j].pos] ){
 87                 vis[pcb[j].pos] = true;
 88                 q.push(pcb[j]);
 89                 cur_t = pcb[j].arrival_t;
 90                 flag = true;
 91                 break;
 92             }
 93         }
 94         if( !flag ) break;
 95     }
 96 }
 97 
 98 void output(){
 99     cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<endl;
100     for(int i = 0; i < num; ++i){
101         cout<<pcb[i].name<<"      "<<pcb[i].arrival_t<<"       "<<pcb[i].start_t<<"       "<<pcb[i].end_t<<endl;
102     }
103 }
104 
105 int main(){
106     init();
107     pps();
108     output();
109     return 0;
110 }

 

時間片輪轉排程演算法

 1 //時間輪片排程演算法
 2 #include <bits/stdc++.h>
 3 #define N 100
 4 #define state int
 5 using namespace std;
 6 struct PCB{
 7     string name;        //程序名稱
 8     int pos;            //程序順序
 9     double arrival_t;      //到達的時間
10     double start_t;        //開始時間
11     double end_t;          //結束時間
12     double service_t;      //服務時間
13     double service_tt;     // 備份服務時間
14     double around_t;       //週轉時間
15     double val_around_t;   //帶權週轉時間
16     
17 }pcb[N];
18 int num,TIME;
19 double cur_t = 0;
20 queue<PCB> q;
21 bool cmp(PCB a, PCB b){
22     return a.arrival_t < b.arrival_t;
23 }
24 
25 void init(){
26     cout<<"輸入時間片的時間:";
27     cin>>TIME;
28     cout<<"輸入程序個數: ";
29     cin>>num;
30     for(int i = 0; i < num; ++i){
31         cout<<"請輸入第"<<i+1<<"個 程序名, 到達時間, 服務時間"<<endl;
32         cin>>pcb[i].name>>pcb[i].arrival_t>>pcb[i].service_t;
33         pcb[i].service_tt = pcb[i].service_t;
34         pcb[i].pos = i + 1 ;
35     }
36 }
37 
38 void tt(){
39     //時間輪片排程演算法
40     sort(pcb, pcb + num, cmp);
41     int index = 0;
42     q.push(pcb[0]);
43     cur_t = max(pcb[0].arrival_t, cur_t);
44     for(int i = 1; i < num; ++i){
45         while( !q.empty() ){
46             PCB cnt = q.front();
47             q.pop();
48             cnt.start_t = cnt.start_t == 0 ? cur_t : cnt.start_t;
49             cnt.end_t = cnt.service_t >= TIME ? cur_t + TIME : cur_t + cnt.service_t;
50             for( ; i < num ; ++i){
51                 if(pcb[i].arrival_t <= cnt.end_t){
52                     q.push(pcb[i]);
53                 }else
54                     break;
55             }
56             if(cnt.service_t > TIME){
57                 cnt.service_t -= TIME;
58                 q.push(cnt);
59                 cur_t += TIME;
60             }else{
61                 cur_t += cnt.service_t;
62                 pcb[index++] = cnt;
63             }
64         }
65         bool flag = false;
66         if(i < num){
67             q.push(pcb[i]);
68             cur_t = pcb[i].arrival_t;
69             flag = true;
70         }
71         if( !flag ) break;
72     }
73 
74 }
75 
76 void output(){
77     cout<<"程序名"<<" "<<"到達時間"<<" "<<"開始時間"<<" "<<"結束時間"<<" "<<"週轉時間"<<" "<<"帶權週轉時間"<<endl;
78     for(int i = 0; i < num; ++i){
79         pcb[i].around_t = pcb[i].end_t - pcb[i].start_t;
80         pcb[i].val_around_t = pcb[i].around_t / pcb[i].service_tt;
81         cout<<pcb[i].name<<"      "<<pcb[i].arrival_t<<"       "<<pcb[i].start_t<<"       "<<pcb[i].end_t<<"       "<<pcb[i].around_t<<"      "<<pcb[i].val_around_t<<endl;
82     }
83 }
84 
85 int main(){
86     init();
87     tt();
88     output();
89     return 0;
90 }