1. 程式人生 > >模擬實現請求分頁虛存頁面替換演算法

模擬實現請求分頁虛存頁面替換演算法

編寫程式, 由必要的資料結構、 主函式、 頁面置換演算法函式和顯示記憶體使用函式構成,
模擬實現請求分頁管理中至少兩種頁面替換演算法。要求程式執行時:
(1)顯示主選單,包含初始化記憶體、隨機生成頁面訪問序列、模擬程序執行過程、顯示內
存使用情況、顯示程序頁面狀態。
(2)接收使用者輸入引數:可用記憶體容量、頁面長度(即主存塊大小)、程序個數、程序
長度(頁面數)等。
(3) 可模擬一個程序的動態執行過程中各頁面的換入換出;也可模擬多個程序併發過程
中各程序頁面訪問過程中的換入換出(採用固定分配、區域性置換)。
(4)每個頁面訪問可用互動式進行,也可按照事先隨機生成頁面訪問序列自動進行。
(5)每個頁面訪問的結果以命中、直接裝入和替換來記錄。
(6) 從 OPT、FIFO、LRU、LFU、SCR、Clock、改進的 Clock 等頁面置換演算法中選擇兩種
實現
 

程式實現的是單程序,FIFO和LRU置換演算法

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <pthread.h>
#include <malloc.h>
#include <semaphore.h>
#include <unistd.h>
using namespace std;
#define BUSY 1
#define IDLE 0
#define inf 0x3f3f3f3f
struct Block;
struct Page{
    int pageID;
    Block* to;
};
struct Block{
    Page* page;
    long time;
    int id;
    int state;
    struct Block* next;
};
struct Process{
    Page *page_list;
    int pageLength;
    int* num;
};
int total_Length,page_length;
int pocess_num;
struct Block* Block_list;
struct Process process;
void init()
{
    printf("請輸入記憶體容量: ");
    scanf("%d",&total_Length);
    printf("請輸入頁面長度: ");
    scanf("%d",&page_length);
    if(total_Length%page_length)
    {
        printf("初始化失敗!\n");
        return;
    }
    Block_list=new(Block);
    Block* p=Block_list;
    p->page=NULL;
    p->time=0;
    p->state=BUSY;
    p->next=NULL;
    int t=total_Length/page_length;
    for(int i=1;i<=t;i++)
    {
        p->next=new(Block);
        p=p->next;
        p->page=NULL;
        p->time=0;
        p->state=IDLE;
        p->id=i;
    }
    printf("初始化成功\n");
}
void show_Block()
{
    Block* p=Block_list->next;
    while(p!=NULL)
    {
        printf("%d:",p->id);
        if(p->state)
        {
            printf("已分配給%d號幀\n",p->page->pageID);
        }
        else
        {
            printf("空閒幀\n");
        }
        p=p->next;
    }
}
void show_Process()
{
    for(int i=1;i<=process.pageLength;i++)
    {
        printf("%d號頁 ",i);
        if(process.page_list[i].pageID==-1)
        printf("未載入記憶體\n");
        else
        printf("已載入第%d幀\n",process.page_list[i].pageID);
    }
}
void creat_process()
{
    printf("請輸入程序容量: \n");
    int length;
    scanf("%d",&length);
    process.pageLength=length/page_length;
    if(length%page_length)
    process.pageLength++;
    process.num=new int[process.pageLength+1];
    printf("共有%d個頁面\n",process.pageLength);
    printf("1.隨機生成頁面訪問序列\n");
    printf("2.手動輸入頁面訪問序列\n");
    printf("請選擇: ");
    int ti;
    scanf("%d",&ti);
    if(ti==1)
    {
        for(int i=1;i<=process.pageLength;i++)
        process.num[i]=rand()%(process.pageLength)+1;
        for(int i=1;i<=process.pageLength;i++)
        printf("%d ",process.num[i]);
        printf("\n");
    }
    else
    {
        for(int i=1;i<=process.pageLength;i++)
        scanf("%d",&process.num[i]);
    }
    process.page_list=new Page[process.pageLength+1];
    for(int i=1;i<=process.pageLength;i++)
    process.page_list[i].pageID=-1;
}
bool hit(int now,int k=0)
{
    Block* p=Block_list->next;
    while(p!=NULL)
    {
        if(p->page==&process.page_list[now])
        {
            if(k!=0)
            {
                p->time=k;
            }
            return true;
        }
        p=p->next;
    }
    return false;
}
bool find_free(int now,int time)
{
    Block* p=Block_list->next;
    while(p!=NULL)
    {
        if(p->state==IDLE)
        {
            p->state=BUSY;
            p->time=time;
            p->page=&process.page_list[now];
            process.page_list[now].to=p;
            process.page_list[now].pageID=p->id;
            return true;
        }
        p=p->next;
    }
    return false;
}
void find_min(int now,int time)
{
    Block* p=Block_list->next;
    int minn=p->time;
    Block* minnp=p;
    while(p!=NULL)
    {
        if(p->time<minn)
        {
            minn=p->time;
            minnp=p;
        }
        p=p->next;
    }
    minnp->time=time;
    printf("替換第%d頁 裝入第%d幀\n",minnp->page->pageID,minnp->id);
    minnp->page=&process.page_list[now];
    process.page_list[now].to=minnp;
    process.page_list[now].pageID=minnp->id;
}
void FIFO()
{
    for(int i=1;i<=process.pageLength;i++)
    {
        int now=process.num[i];
        printf("%d號頁: ",now);
        if(hit(now))
        {
            printf("命中\n");
            continue;
        }
        else if(find_free(now,i))
        printf("直接裝入第%d幀\n",process.page_list[now].to->id);
        else
        find_min(now,i);
    }
}
void LRU()
{
    for(int i=1;i<=process.pageLength;i++)
    {
        int now=process.num[i];
        printf("%d號頁: ",now);
        if(hit(now,i))
        {
            printf("命中\n");
            continue;
        }
        else if(find_free(now,i))
        printf("直接裝入第%d幀\n",process.page_list[now].to->id);
        else
        find_min(now,i);
    }
}
void simulation()
{
    printf("1.FIFO\n");
    printf("2.LRU\n");
    printf("請選擇頁面置換演算法:");
    int ti;
    scanf("%d",&ti);
    if(ti==1)
    FIFO();
    else if(ti==2)
    LRU();
}
int main(int argc, char const *argv[]) {
    printf("\033c");
    printf("----------------------------------------\n");
    printf("-        請求分頁虛存頁面替換模擬      -\n");
    printf("-                                      -\n");
    printf("-                                      -\n");
    printf("-      1.初始化記憶體                    -\n");
    printf("-      2.建立程序並生成頁面訪問序列    -\n");
    printf("-      3.模擬程序執行過程              -\n");
    printf("-      4.顯示記憶體使用情況              -\n");
    printf("-      5.顯示程序頁面狀態              -\n");
    printf("-      0.退出                          -\n");
    printf("-                                      -\n");
    printf("----------------------------------------\n");
    int ti;
    while(1)
    {
        printf("--請選擇功能選項:  ");
        scanf("%d",&ti);
        if(ti==0)
        break;
        else if(ti==1)
        init();
        else if(ti==2)
        creat_process();
        else if(ti==3)
        simulation();
        else if(ti==4)
        show_Block();
        else if(ti==5)
        show_Process();
    }
    return 0;
}