• 进程调度算法模拟


    最近学了操作系统的进程调度的各种算法,手痒实现了一下,

    仅供参考,若有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算法执行如下:
    ";
    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 }
  • 相关阅读:
    unbuntu下安装多个JAVA JDK版本及如何切换
    google play上获取apk文件
    android开发里跳过的坑——android studio打包的APK签名无效
    android studio配置so文件路径
    Android 4.4.2上与BLE 蓝牙锁设备的通讯
    android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted
    android开发里跳过的坑——listview不显示
    如何在Linux服务器中使用arping命令
    如何在CentOS 8上安装OpenLiteSpeed
    如何在Ubuntu 20.04 LTS系统上安装ModSecurity Apache
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9815622.html
Copyright © 2020-2023  润新知