当前位置:
文档之家› 操作系统最佳置换算法实验报告
操作系统最佳置换算法实验报告
if(flag)
{ flag=0;//important
for(k=recent;k++)
;
time_count2=k-recent+load_number-i;//?可能出错
if(time_count1<time_count2)
{ time_count1=time_count2;
#define MIX 10
#include <stdio.h>
//最佳置换算法(前提:给出页面号引用窜)
//数据结构
int recent=0;//将被装入内存的页号的标号(从0开始计)
int count;//序列号总数
int load_number;//装入内存的页面数
//序列号
int array[MAX];
flag1=i;//
}
}
for(k=0;k<load_number;k++)
if(page_t[k].pageNum ==array[recent+time_count1-load_number+flag1])
{recentP=&page_t[k];break;}
}
}
if(j==count)//采用先进先出算法
while(ch!='q')
{
printf("please input a count:\n");
scanf("%d",&count); //
printf("\n");
printf("please input page numbers:\n");
for(i=0;i<count;i++)
scanf("%d",&array[i]); //
{
int i,nCount=0,flag=0;
recentP->pageNum =array[recent];
recent++;
recentP++;
nCount++;
while(nCount<load_number)
{ //扫描是否有相等的页号
for(i=0;i<nCount;i++)
if(page_t[i].pageNum ==array[recent])
{ flag=1;break;}//*{}is important*
if(flag)
{recent++;flag=0;
printf("%d命中\n",array[recent-1]);}//*importance
else
{recentP->pageNum=array[recent];
recent++;
//页面
struct pageTable
{
int pageNum;
int wuliNum;
//...
};
struct pageTable page_t[MIX];//页表
struct pageTable*recentP;
//传递内存页面数组、数组长度、命中索引号三个参数;并将该数组按最近最久排序,
//前面元素为最久未使用,而末尾元素为最近未使用
if(page_t[i].pageNum ==array[recent])
{ no=1;break;}//*{}is important*
if(!no)
{printf("%d命中\n",array[recent-1]);//*importance
recent++;
getline(page_t,load_number,i);
//for(i=0;istruct page_table<count;i++)
printf("please input a 'q' to exit input!\n");
scanf("%c",&ch);
scanf("%c",&ch);
}
recentP=page_t;
}
//装载至内存函数
//
void firstLoad()
{
int i,j,no=0,k=0;
int flag=0,flag1=0;//有该页号,则置1
//记录与进入内存的页号相等的将被装入的页号的最大次数
int time_count1=0,time_count2=0;
//先检查是否与array[recent]命中,是则n置1
for(i=0;i<load_number;i++)
void getline(struct pageTable a[],int n,int index)
{
int i,temp;
//for(i=0;i<n;i++)//找命中页面号
temp=a[index].pageNum ;
for(i=index;i<n-1;i++)
a[i].pageNum =a[i+1].pageNum;
}
else
{no=0;//important
//检查是否有与内存中页号相同的页号在等待,有则falg置1,无则falg置0
for(i=0;i<load_number;i++)
{ for(j=recent;j<count;j++)
if(page_t[i].pageNum ==array[j])
{flag=1;break;}
recentP++;
nCount++;
}
}
//show()
for(i=0;i<nCount;i++)
printf("%d ",page_t[i].pageNum);
printf("\n");
}
//search函数找一个要替换的页号
void search() //给replace_p,replace_n赋值!
实验报告
系别
班级
学号
姓名
时间
地点
计算机科学系
课程名称
操作系统
实验名称
最佳置换算法
实验过程
一.实验目的
1.了解内存管理的功能。
2.掌握进程可变内存管理的几种内存分配与回收算法。
3.掌握可变分区算法中空闲分区的合并方法
二.实验内容
实现内存分配算法——最佳置换算法。
三.实验程序
#define MAX 100
recentP=page_t;
recentP->pageNum =array[recent];
recent++;
}
//
void main()
{//初始化数据
input();
while(recent<=count)
{
firstLoad();
search();
}
}
四.运行截图
五.实验总结
通过这次实验,我对最佳置换算法的掌握更加熟练了。最佳置换算法是用一维数组存储页面号序列,存储装入物理块中的页面。并记录物理块中对应页面的最后访问时间。每当发生缺页时,就从物理块中找出最后访问时间最大的页面,调出该页,换入所缺的页面。
printf("\n");
for(i=0;i<count;i++)
printf("%d ",array[i]);
printf("\n");
printf("please input the numbers of page to load:\n");
scanf("%d",&load_number); //
a[n-1].pageNum=temp;
}
//input页面号引用窜(空格结束)
void input()
{
char ch;int i;
//页面号
printf("please input an anykey not include 'q',and then begin.\n");scanf("%c",&ch);