• 内存管理-请求调页


    FIFO

    利用vector存储用户输入的页面号

    利用queue存储内存中的页面号

     1 /**********************************************************************
     2 *
     3 *  File    : memoryManageFIFO.cpp
     4 *  Author  : 阿Q
     5 *  Time    : 2016.13.6
     6 *
     7 **********************************************************************/
     8 #include<stdio.h>
     9 #include<vector>//存储用户输入的页面号
    10 #include<queue>//记录内存块FIFO
    11 using namespace std;
    12 
    13 queue<int> memoryBlock;  //内存块队列
    14 int memorySize=0;  //内存块的大小
    15 
    16 //扫描该页是否在内存块中
    17 int scanMemoryBlock(const int a) {
    18     int i=0;
    19     queue<int> temQue=memoryBlock;
    20     for(; i<memoryBlock.size(); i++) {
    21         if(a==temQue.front()) {
    22             return 0;
    23         }
    24         temQue.pop();
    25     }
    26     return 1;
    27 }
    28 
    29 //显示内存块中的页面号
    30 void displayMemoryBlock() {
    31     int i=0;
    32     queue<int> temQue=memoryBlock;
    33     int tem=memoryBlock.size()<=memorySize?memoryBlock.size():memorySize;
    34     printf("内存中的页:");
    35     for(; i<tem; i++) {
    36         printf("%d ",temQue.front());
    37         temQue.pop();
    38     }
    39     printf("
    
    ");
    40 }
    41 
    42 int main() {
    43     vector<int> vec;
    44     int cut,i=0,tem;
    45     printf("请输入要请求的页数目:");
    46     scanf("%d",&cut);
    47     printf("
    请输入内存块的个数:");
    48     scanf("%d",&memorySize);
    49     printf("
    请依次输入请求的页号(以空格隔开):");
    50     for(i=0; i<cut; i++) {
    51         scanf("%d",&tem);
    52         vec.push_back(tem);
    53     }
    54 
    55     for(i=0; i<cut; i++) {
    56         if(scanMemoryBlock(vec[i])) { //内存块中是否有该页
    57             printf("  请求调页:%d
    ",vec[i]);
    58             if(memoryBlock.size()<memorySize) { //是否有空余的内存空间
    59                 memoryBlock.push(vec[i]);
    60             } else {
    61                 memoryBlock.pop();
    62                 memoryBlock.push(vec[i]);
    63             }
    64         } else {
    65             printf("  该页存在:%d
    ",vec[i]);
    66         }
    67         displayMemoryBlock();
    68     }
    69     return 0;
    70 }

     LUR留坑,看到室友从网上找的代码,写的真烂。。。明天考试,祝我顺利,晚安~16.12.12 22:52

  • 相关阅读:
    学习windows编程 day4 之视口和窗口
    学习windows编程 day4 之 映射模式
    学习windows编程 day4 之 盯裆猫
    Android自动化测试(UiAutomator)简要介绍
    UltraEdit正则表达式介绍及实例
    addr2line -f -e *.so 0x9d69
    Android执行shell命令
    Drawable、Bitmap、byte[]之间的转换
    Ubuntu 12.04 64bit 配置完android 5.0编译环境后出现“could not write bytes: Broken pipe.”而无法进入输入帐号密码的登陆界面
    CameraTest
  • 原文地址:https://www.cnblogs.com/A--Q/p/6137421.html
Copyright © 2020-2023  润新知