⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vreplace.txt

📁 模拟虚拟存储器 调用页面时若没有实页就中断 请求页面置换 算法是FIFO
💻 TXT
字号:
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "conio.h"
#define TRUE 1
#define FALSE 0
#define NULL 0
#define total_vp 10
typedef struct
{  
	int pn,pfn;
}pv_type;
pv_type pv[10];
typedef struct pf_struct
{  
	int pn,pfn;
    struct pf_struct *next;
}pf_type;
pf_type pf[20],*free_head,*busy_head,*busy_tail,*q;
int page[20];
int total;
int count;
void initial()  
{ 
	int i;
    count=0;
    for(i=0;i<total_vp;i++)
    {  
		pv[i].pn=i;
        pv[i].pfn=-1;
    }
    printf("Please enter the number of the page!\n");
    scanf("%d",&total);
    for(i=0;i<=total-1;i++)
    {  
		pf[i].next=&pf[i+1];
        pf[i].pfn=i+1;
        pf[i].pn=-1;
    }
    pf[total-1].next=NULL;
    free_head=&pf[0];
    printf("creat a pagelist:\n");
    for(i=0;i<20;i++)
    { 
		page[i]=rand()%10;
        printf("%2d",page[i]);
    }
    getch();
}
void FIFO()
{  
	int i,j;
    pf_type *p;
    q=busy_head=busy_tail=NULL;
    for(i=0;i<=20-1;i++)
    {   printf("\n--------------------------------------------");
		printf("\nThe %dth page\n",i+1);
		getch();
        if(pv[page[i]].pfn==-1)
        {  
			count+=1;
            printf("Fail ,failed number is  count=%d.",count);
            if(free_head==NULL)
            {
				printf("The Page is in the %dth and %d will be repalced\n",busy_head->pfn,busy_head->pn);
                p=busy_head->next;
                pv[busy_head->pn].pfn=-1;
                free_head=busy_head;
                free_head->next=NULL;
                busy_head=p;
            }
            p=free_head->next;
            free_head->next=NULL;
            free_head->pn=page[i];
            pv[page[i]].pfn=free_head->pn;
            if(busy_tail==NULL)
            busy_head=busy_tail=free_head;
            else
            {  
				busy_tail->next=free_head;
                busy_tail=free_head;
            }
            free_head=p;
        }
        else  printf("Shooting, missed page number remained at count=%d",count);
        printf("\npfn     pn");
        for(j=0;j<=total-1;j++)
        {   
			if(pf[j].pn!=-1)
            printf("\n%d%8d",pf[j].pfn,pf[j].pn);
        }
    }   
	getch();
    printf("\nThe FIFO method' shooting proportion is :%7.5f\nThe total number of lack page is=%d\n",1-(float)count/20,count);}
void main()
{ 
	initial();
    FIFO();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -