zoukankan      html  css  js  c++  java
  • 模拟LRU算法&通道处理算法

    最近写了两个程序,模拟操作系统的算法,只是基本实现了课本上的基本功能,实际应该是很复杂的。

     

    模拟LRU页面置换算法:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #define TN 3             //分配给该程序的主存页数
      6 #define PN 10             //地址流
      7 
      8 typedef struct PAGE{
      9     int kPage;   //the kth line 分配给该程序的主存页数的索引
     10     int aPage;   //address page 程序页号
     11     int cnt;     //到当前页地址的距离计数器
     12 }PAGE;
     13 
     14 PAGE pg[PN];     //页地址流
     15 int page[TN];    //主存页面表
     16 
     17 /*页面命中和替换页面时都需要初始化调入的页面*/
     18 void init(int k, int p){
     19     int i;
     20     for(i=0;i<k;i++){
     21         if(pg[i].aPage==p)
     22             pg[i].cnt=1;
     23     }   
     24 }
     25 
     26 /*查找页面表中最大的计数器,返回对应的索引*/
     27 int findMax(){
     28     int ans=0, max_cnt=-1, cnt, index;
     29     int i,j;
     30     for(i=0;i<TN;i++) {
     31         /*在主存页面表中查找与给定页地址流页面相等的页面*/
     32         for(j=0;j<PN;j++){
     33             if(page[i] == pg[j].aPage){
     34                 cnt = pg[j].cnt;
     35                 break;
     36             }
     37         }
     38         if(max_cnt < cnt) {  
     39             max_cnt = cnt;
     40             ans = i;
     41             index = j;
     42         }
     43     } 
     44     return ans;
     45 }
     46 
     47 void LRU(){
     48     int i,p,k=0;  /*p:当前页面,k:页地址流全局索引*/
     49     int replaceIndex;
     50     int old;
     51 
     52     while(~scanf("%d",&p)){
     53         int inserted=0, hit=0;
     54             
     55         pg[k].aPage=p;
     56         /*预处理*/
     57         for(i=0;i<=k;i++) {
     58             pg[i].cnt+=1;
     59         }
     60         if(k == 0){
     61             printf("\n输出结果:\n\n");
     62             printf("时间\t页地址\t操作\t主存索引\t源页面\t目标页面\n");    
     63         }
     64         printf("%d\t%d\t",k+1,p);
     65         for(i=0;i<TN;i++) {
     66             if(p == page[i]) {      //命中
     67                 pg[k].cnt=1;
     68                 inserted=1;         //相当于调进
     69                 hit=1;
     70                 printf("命中\t%d\t\t%d\t%d",i+1,p,p);
     71                 break;
     72             }
     73             if(!page[i]){           //有空余页
     74                 page[i]=p;
     75                 pg[k].kPage=i;
     76                 pg[k].cnt=1;
     77                 inserted=1;
     78                 printf("调进\t%d\t\t-\t%d",i+1,p);
     79                 break;
     80             }
     81         }
     82         if(hit == 1) init(k,p);
     83         
     84         /*替换操作,调进页面设置*/
     85         if(!inserted){
     86             init(k,p);
     87             replaceIndex=findMax();
     88             old=page[replaceIndex];
     89             page[replaceIndex]=p;     //置换
     90             printf("替换\t%d\t\t%d\t%d",replaceIndex+1,old,p);
     91         }
     92         printf("\n");
     93         k++;
     94     }
     95 }
     96 int main(){
     97     memset(page,0,sizeof(page));        
     98     memset(pg,0,sizeof(pg));        
     99     printf("输入页地址流:\n");
    100     LRU();
    101     return 0;
    102 }

     测试结果:

    字节多路通道响应和设备处理:

      1 #include <iostream>
      2 using namespace std;
      3 
      4 #define N 5        //设备数目
      5 #define GAP 10       //通道处理时间
      6 
      7 struct Device{
      8     int cycle;       //设备周期
      9     int start;     //起始时间
     10     int priority;  //优先级
     11     bool applied;  //是否提出申请
     12 }d[N];
     13 
     14 /*初始化各设备起始状态*/
     15 void init() {
     16     for(int i=0; i < N; i++) {
     17         d[i].applied = false;
     18         cin>>d[i].start>>d[i].cycle>>d[i].priority;
     19     }
     20 }
     21 
     22 int cmp(const void *a, const void *b){
     23     int c=((Device *)b)->priority - ((Device *)a)->priority;        //按优先级排序
     24     if(c == 0) return ((Device *)a)->start - ((Device *)b)->start;  
     25     else if(c > 0) 
     26             return 1;
     27     else return -1;
     28 }
     29 
     30 void swap(Device **a, Device *b){
     31     Device tmp;
     32     tmp.cycle = (*a)->cycle;
     33     tmp.priority = (*a)->priority;
     34     tmp.start = (*a)->start;
     35     tmp.applied = (*a)->applied;
     36 
     37     (*a)->cycle = b->cycle;
     38     (*a)->priority = b->priority;
     39     (*a)->start = b->start;
     40     (*a)->applied = b->applied;
     41     
     42     b->cycle = tmp.cycle;
     43     b->priority = tmp.priority;
     44     b->start =tmp.start;
     45     b->applied = tmp.applied;
     46 }
     47 
     48 /*p最终指向通道选中的设备,selected是选中设备的索引*/
     49 void Quicksort(Device **p, int &selected) {
     50     for(int i=0; i<N; i++) {
     51         if(d[i].applied == true && selected!=i) {                       //只对申请的设备排序
     52             if(d[i].priority > (*p)->priority) {
     53                 swap(p, &d[i]); 
     54                 selected=i;
     55             }
     56             else if(d[i].priority == (*p)->priority){
     57                 if(d[i].start < (*p)->start)
     58                     swap(p, &d[i]);
     59                     selected=i;
     60             }
     61         }
     62     }
     63 }
     64 
     65 void check_lose(int handle_time,int selected){
     66     for(int i=0; i<N; i++) {
     67         if(i!=selected) {
     68             int next_t=d[i].start+d[i].cycle; //下次申请时间
     69             if(handle_time>=next_t){
     70                 int cnt=(handle_time-d[i].start)/d[i].cycle;  //连续cnt次丢失信息
     71 
     72                 /*判断并输出第几号设备在第几次申请未得到响应*/
     73                 printf("      %d号设备第",i);
     74                 int k=0;
     75                 int c=d[i].start/d[i].cycle;  //起始计数器
     76                 while(k<cnt){
     77                     printf("%d ",c++);
     78                     k++;
     79                 }
     80                 printf("次申请未得到响应!\n");
     81                 d[i].start+=cnt*d[i].cycle;
     82             }
     83         }
     84     }
     85 }
     86 
     87 void handle(int end_time){
     88     int t,j,i=0;
     89     int selected;                  //选中设备的索引
     90     Device *p;                      //应选中的设备
     91     for(t = 0; t<end_time; t+=GAP){
     92         int k=0;
     93         printf("第%d周期: ",++i);
     94         for(j = 0; j<N; j++) {
     95             selected=0;
     96             if(d[j].start <= t) { 
     97                 d[j].applied=true;  //申请
     98                 if(k==0) {
     99                     p=&d[j];   //暂存第一个提交申请的设备
    100                     selected=j;
    101                 }
    102                 k++;
    103             }
    104         }
    105 
    106         if(k == 0) {
    107             printf("无设备提出申请!\n");
    108         }
    109         else{
    110             Quicksort(&p,selected);
    111             printf("%d号设备被选中\n",selected);
    112             check_lose(t,selected);
    113 
    114             /*选中的设备的后处理*/
    115             p->applied = false;
    116             p->start+=p->cycle;            
    117         }
    118     }
    119 }
    120 
    121 int main(){
    122     init();
    123     int end_time;
    124     scanf("%d",&end_time);
    125     printf("\nresult:\n");
    126     handle(end_time);
    127     return 0;
    128 }

     



  • 相关阅读:
    堆排序
    cocos2d-x -------之笔记篇 动画的实现
    cocos2d-x -------之笔记篇 3D动作说明
    Cocos2d-x 学习之路------(CCCallfunc 系列)
    cocos2d-x -------之笔记篇 环境的安装
    poj 1141 Brackets Sequence(区间DP)
    1051 接龙游戏
    1229 数字游戏
    java反射机制(笔记)
    Linux中搭建FTP服务器
  • 原文地址:https://www.cnblogs.com/Seiyagoo/p/2437136.html
Copyright © 2011-2022 走看看