OS Process Task 安排

来源:互联网 发布:怎样查看淘宝价格曲线 编辑:程序博客网 时间:2024/05/18 01:37
Code:
  1. #include <iostream>   
  2. #include<string.h>   
  3.   
  4. using namespace std;   
  5. const int block_time=5; //定义时间片的长度为10秒   
  6. const int maxPCB=100;   //定义最大进程数   
  7.   
  8. //------------------------------定义进程结构体------------------------   
  9. typedef struct node   
  10. {   
  11.     char name[20];   
  12.     int status;   
  13.     /**  
  14.         三个状态:1:运行态,2:就绪态,3:完成态。  
  15.     */  
  16.     int time;   
  17.     int privilege;   
  18.     int finished;//完成状态   
  19.     int wait_time;   
  20. }pcb;   
  21.     pcb pcbs[maxPCB]; //结构体数组   
  22.     int quantity;     //总进程数量   
  23.   
  24.   
  25. //----------------------------初始化函数------------------------------   
  26. void initpcbfoo()   
  27. {   
  28.     /*  
  29.     * 初始化pcb信息  
  30.     */  
  31.     int i;   
  32.     for(i=0;i<maxPCB;i++)   
  33.     {   
  34.         strcpy(pcbs[i].name,"");   
  35.         pcbs[i].status=0;   
  36.         pcbs[i].time=0;   
  37.         pcbs[i].privilege=0;   
  38.         pcbs[i].finished=0;   
  39.         pcbs[i].wait_time=0;   
  40.     }   
  41.     quantity=0;//开始至进程数量为0;   
  42. }   
  43.   
  44.   
  45. //-------------------从文件中读数据函数 -------------   
  46. int readdatafoo()   
  47. {   
  48.     FILE *fp;   
  49.     char fname[20];   
  50.     int i;   
  51.     cout<<"请输入进程信息文件(提示:aaa.txt):";   
  52.     cin>>fname;   
  53.     if((fp=fopen(fname,"r"))==NULL)   
  54.     {   
  55.         cout<<"错误,文件打不开,请检查文件名"<<endl;   
  56.     }   
  57.     else  
  58.     {   
  59.         while(!feof(fp))   
  60.         {   
  61.             fscanf(fp,"%s %d %d %d",pcbs[quantity].name,&pcbs[quantity].status,   
  62.                 &pcbs[quantity].time,&pcbs[quantity].privilege);   
  63.             quantity++;   
  64.         } //输出所读入的数据  次序为:进程名 进程状态 所需时间 优先数   
  65.         cout<<"所读入的 进程信息数据"<<endl;   
  66.         cout<<"进程名 进程状态 所需时间 优先数"<<endl;   
  67.         for(i=0;i<quantity;i++)   
  68.         {   
  69.             cout<<""<<pcbs[i].name<<"        "<<pcbs[i].status<<"     "<<pcbs[i].time<<"     "<<pcbs[i].privilege<<endl;   
  70.         }   
  71.         return(1);   
  72.     }   
  73.     return(0);   
  74. }   
  75.   
  76.   
  77. void init()   
  78. {     //重置数据,以供另一个算法使用   
  79.     int i;   
  80.     for(i=0;i<maxPCB;i++)   
  81.     {   
  82.         pcbs[i].finished=0; pcbs[i].wait_time=0;   
  83.     }   
  84. }   
  85.   
  86. //先进先出算法   
  87. void FIFOfoo()   
  88. {   
  89.     int i,j;   
  90.     int total;   
  91.     //输出FIFO算法执行流   
  92.     cout<<endl<<"*****************************************************"<<endl;   
  93.     cout<<"FIFO算法:"<<endl;   
  94.     cout<<"进程名 等待时间"<<endl;   
  95.     for(i=0;i<quantity;i++)   
  96.     {   
  97.         cout<<""<<pcbs[i].name<<"    "<<pcbs[i].wait_time<<endl;   
  98.         for(j=i+1;j<quantity;j++)   
  99.         {   
  100.             pcbs[j].wait_time+=pcbs[i].time;   
  101.         }   
  102.     }   
  103.     total=0;   
  104.     for(i=0;i<quantity;i++)   
  105.     {   
  106.          total+=pcbs[i].wait_time;   
  107.     }   
  108.     cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantity<<endl;   
  109. }   
  110. /*--------------------------间片轮转调度算法---------------------------------*/  
  111. void RRtimefoo()   
  112. {   
  113.     cout<<endl<<"*******************************************************"<<endl;   
  114.     cout<<"时间片轮转调度(时间片设为5):"<<endl;   
  115.     int i,j,number,flag=1;   
  116.     int passed_time=0;   
  117.     int max_time=0;   
  118.     int round=0;   
  119.     int queue[1000];   
  120.     int total=0;   
  121.     while(flag==1)   
  122.     {   
  123.         flag=0;   
  124.         number=0;   
  125.         for(i=0;i<quantity;i++)   
  126.         {   
  127.             if(pcbs[i].finished==0)   
  128.             {   
  129.                 number++;   
  130.                 j=i;   
  131.             }   
  132.         }   
  133.         if(number==1)   
  134.         {   
  135.             queue[total]=j;   
  136.             total++;   
  137.             pcbs[j].finished=1;   
  138.         }   
  139.         if(number>1)   
  140.         {   
  141.             for(i=0;i<quantity;i++)   
  142.             {   
  143.                 if(pcbs[i].finished==0)   
  144.                 { flag=1;   
  145.                     queue[total]=i;   
  146.                     total++;   
  147.                     if(pcbs[i].time<=block_time*(round+1))   
  148.                     {   
  149.                         pcbs[i].finished=1;   
  150.   
  151.                     }   
  152.                 }   
  153.             }   
  154.         }   
  155.         round++;   
  156. }   
  157.     if(queue[total-1]==queue[total-2])   
  158.     {   
  159.         total--;   
  160.     }   
  161.   
  162.     for(i=0;i<total;i++)   
  163.     {   
  164.         cout<<pcbs[queue[i]].name<<" ";   
  165.         cout<<endl;   
  166.     }   
  167. cout<<"*******************************************************"<<endl;   
  168. }   
  169. //显示   
  170. void help()   
  171. {   //cout<<" Define what you want do!/n";   
  172.     cout<<"*****Created by husiwen******/n";   
  173.     cout<<"--------------------------------/n";   
  174.     cout<<"**  *1.时间片轮转调度         **/n";   
  175.     cout<<"**  *2.先来先服务调度         **/n";   
  176.     cout<<"**  *0.退出                   **/n";   
  177.     cout<<"--------------------------------/n";   
  178. }   
  179. void seek()   
  180. {   
  181.   int flag;   
  182.   cout<<"请选择你要进行的操作:";   
  183.   cin>>flag;   
  184.   while(0!=flag)   
  185.   {   
  186.       switch(flag)   
  187.       {   
  188.           case  1:   //时间片轮转调度   
  189.           {   
  190.               cout<<"       你选择的是时间片轮转调度"<<endl;   
  191.               initpcbfoo();   
  192.             int openflag=readdatafoo();   
  193.             if(1 == openflag)   
  194.             {   
  195.                 RRtimefoo();   
  196.                 init();   
  197.             }   
  198.           }   
  199.           flag = 0;   
  200.           seek();   
  201.           break;  //case 1: RRtimefoo;   
  202.   
  203.           case  2:   //先来先服务调度   
  204.           {   
  205.               cout<<"       你选择的是先来先服务调度"<<endl;   
  206.                initpcbfoo();   
  207.                int openflag=readdatafoo();   
  208.             if(1 == openflag)   
  209.             {   
  210.                 //init();   
  211.                 //initpcbfoo();   
  212.                 FIFOfoo();   
  213.                 init();   
  214.             }   
  215.   
  216.           }   
  217.           flag = 0;   
  218.           seek();   
  219.           break;     //case 2: FIFOfoo   
  220.           default:   
  221.           cout<<"erro/n";   
  222.           seek();// 输入错误 ,再次调用 seek 函数 提示用户输入选择操作;   
  223.           break;   
  224.       }   
  225.   }   
  226.   return ;   
  227. }   
  228. //主函数   
  229. int  main()   
  230. {   
  231.     help();   
  232.     seek();   
  233.     return 0;   
  234. }   

 

原创粉丝点击