kal0120 2023-05-28 18:30 采纳率: 100%
浏览 22
已结题

操作系统实验LRU算法

初学编程
操作系统LRU算法c++实现
private处报错

#include<iostream>
#include<list>
#include<stdlib.h>
#include<time.h>
using namespace std;

class LRU{
    public:
        LRU(){data=1;used=0;time=0;}
        LRU(int d,int u,int t){
            data = d;used = u;time = t;
        }
        
        void be_used();
         
        friend class List;
        
//    private:        
        int used;
        int data;
        int time;
};

void LRU::be_used(){
    used++;    
}

bool comp_used(LRU& p1,LRU& p2){
    if(p1.used == p2.used){
        return (p1.time < p2.time);
    }else {
        return (p1.used < p2.used);    
    }
}

class List{
    public:
        int lru(LRU& p,int num){
            int interrupt_LRU = 1;
//            cout<<p.data<<" "<<p.used<<endl;
            for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                if((*it).data == p.data){
//                    cout<<"no interrupt_LRU"<<endl;
                    it->be_used();
                    interrupt_LRU = 0;
                }
            }            
            
            if(interrupt_LRU){
//                cout<<"interrupt_LRU"<<endl;
                if(l.size()<num){
                    l.push_back(p);
                //    cout<<(*l.begin()).data;
                }else {
                    l.pop_front();
                    l.push_front(p);
                }
            }
            
            for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                (*it).time++;
            }
            l.sort(comp_used);
/*        
            for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                cout<<(*it).data<<" ";
            }
            cout<<endl;
            cout<<endl;
*/        
            return interrupt_LRU;
        }
    private:
        list<LRU> l;
};

int main(){
    int num = 0;
     int x = 0;
     
    cout<<"输入页框数:";
     cin>>num;
     cout<<endl;
    
    cout<<"输入访问页面序列长度:";
    cin>>x;
    cout<<endl;

    List l;
    int con_LRU = x;
    int fail_LRU = 0;
    
    while(con_LRU){
        int a=rand()%9+1;
//        cout<<"新页:"<<a<<endl;
        LRU page(a,1,0);
//        cout<<page.data<<" "<<page.used<<endl;
            
        if(l.lru(page,num)){
            fail_LRU++;
        }
        
        con_LRU--;
    }
    cout<<"LRU缺页中断率:"<<fail_LRU*100/x<<"%"<<endl;
    return 0;
}

  • 写回答

2条回答 默认 最新

  • GoodCoder666 2023-05-28 20:06
    关注

    报错是因为 comp_used 函数访问了 private 关键字保护的属性。

    其实你已经使用了一个 friend,为何不再来一个?

    #include<iostream>
    #include<list>
    #include<stdlib.h>
    #include<time.h>
    using namespace std;
     
    class LRU{
        public:
            LRU(){data=1;used=0;time=0;}
            LRU(int d,int u,int t){
                data = d;used = u;time = t;
            }
            
            void be_used();
             
            friend class List;
            friend bool comp_used(LRU&, LRU&); // 声明 friend 函数
            
       private:        
            int used;
            int data;
            int time;
    };
     
    void LRU::be_used(){
        used++;    
    }
     
    bool comp_used(LRU& p1,LRU& p2){
        if(p1.used == p2.used){
            return (p1.time < p2.time);
        }else {
            return (p1.used < p2.used);    
        }
    }
     
    class List{
        public:
            int lru(LRU& p,int num){
                int interrupt_LRU = 1;
    //            cout<<p.data<<" "<<p.used<<endl;
                for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                    if((*it).data == p.data){
    //                    cout<<"no interrupt_LRU"<<endl;
                        it->be_used();
                        interrupt_LRU = 0;
                    }
                }            
                
                if(interrupt_LRU){
    //                cout<<"interrupt_LRU"<<endl;
                    if(l.size()<num){
                        l.push_back(p);
                    //    cout<<(*l.begin()).data;
                    }else {
                        l.pop_front();
                        l.push_front(p);
                    }
                }
                
                for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                    (*it).time++;
                }
                l.sort(comp_used);
    /*        
                for(list<LRU>::iterator it = l.begin();it!=l.end();++it){
                    cout<<(*it).data<<" ";
                }
                cout<<endl;
                cout<<endl;
    */        
                return interrupt_LRU;
            }
        private:
            list<LRU> l;
    };
     
    int main(){
        int num = 0;
         int x = 0;
         
        cout<<"输入页框数:";
         cin>>num;
         cout<<endl;
        
        cout<<"输入访问页面序列长度:";
        cin>>x;
        cout<<endl;
     
        List l;
        int con_LRU = x;
        int fail_LRU = 0;
        
        while(con_LRU){
            int a=rand()%9+1;
    //        cout<<"新页:"<<a<<endl;
            LRU page(a,1,0);
    //        cout<<page.data<<" "<<page.used<<endl;
                
            if(l.lru(page,num)){
                fail_LRU++;
            }
            
            con_LRU--;
        }
        cout<<"LRU缺页中断率:"<<fail_LRU*100/x<<"%"<<endl;
        return 0;
    }
    

    这样是可以通过编译的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • kal0120 2023-05-29 00:07
    关注

    img


    谢谢回答,我去试了一下,dev-c++还是报错了,但是换visual studio编译通过。是不是我dev-c++的问题

    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 已采纳回答 5月29日
  • 创建了问题 5月28日

悬赏问题

  • ¥15 fpga二选一数据选择器语句分析
  • ¥15 matlab有svec这个函数吗?
  • ¥15 无法调用VideoWriter_fourcc
  • ¥15 VB6.0无法加载网页验证码图片到picturebox中,求解。
  • ¥15 C#和GDAL对栅格处理
  • ¥15 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
  • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
  • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
  • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
  • ¥15 Oracle触发器字段变化时插入指定值