Linux操作系统实验(3)(模拟实现请求分页虚存页面替换算法)
数据结构:typedef struct _Page{ // 页面int pageID;//页号}Page;typedef struct _PageQueue{ //页面队列Page page;struct _PageQueue* next; //下一页面}PageQueue;typedef struct _Block{ //块记录结构
·
数据结构:
typedef struct _Page{ // 页面
int pageID; //页号
}Page;
typedef struct _PageQueue{ //页面队列
Page page;
struct _PageQueue* next; //下一页面
}PageQueue;
typedef struct _Block{ //块记录结构
Page *page; //页面
long time; //最后访问时间
int state; //页块是否空闲
}Block;
typedef struct _BlockQueue{ //块队列
Block block;
struct _BlockQueue *next;
}BlockQueue;
typedef struct process{ // 进程结构
PageQueue pages; //页面
unsigned int pageLength; // 页面数
}process;//进程
函数:
BlockQueue* InitializeBlockQueue(unsigned int size); //初始化主存块,把首地址返回,如果分配失败返回NULL
int GetBlockQueueSize(BlockQueue *blockQueue); //获取块长度
void ResetBlockQueue(BlockQueue *blockQueue); //清空块内容
void PrintBlockList(BlockQueue *blockQueue, int pageID, int color); //打印块信息
PageQueue* InitializePageQueue(unsigned int pageLength, int maxPageID); //初始化页面
void InitializeProcess(process *proc, unsigned int pageSize, unsigned int maxPageID); //初始化进程
BlockQueue* SearchPage(BlockQueue *blockQueue, Page page); //搜索特定页面
BlockQueue* SearchIdleBlock(BlockQueue *blockQueue); //搜索空闲块
BlockQueue* GetOldestBlock(BlockQueue *blockQueue); //取得主存中停留最久的页面,返回它的地址
void FIFO(BlockQueue *blockQueue, process *proc); //先进先出算法
代码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<sys/time.h>
#include<unistd.h>
#define BUSY 1
#define IDLE 0
#define blockNumber 3
#define n 10
int Time = 0;
typedef struct _Page{ // 页面
int pageID; //页号
}Page;
typedef struct _PageQueue{ //页面队列
Page page;
struct _PageQueue* next; //下一页面
}PageQueue;
typedef struct _Block{ //块记录结构
Page *page; //页面
long time; //最后访问时间
int state; //页块是否空闲
}Block;
typedef struct _BlockQueue{ //块队列
Block block;
struct _BlockQueue *next;
}BlockQueue;
typedef struct process{ // 进程结构
PageQueue pages; //页面
unsigned int pageLength; // 页面数
}process;//进程
BlockQueue* InitializeBlockQueue(unsigned int size){
//初始化主存块,把首地址返回,如果分配失败返回NULL
BlockQueue *p1, *p2;
BlockQueue *block;
block = NULL;
for(int i = 0; i < size; ++i){
p1 = (BlockQueue*)malloc(sizeof(BlockQueue));
p1->block.time = 0;
p1->block.state = 0;
p1->block.page = NULL;
p1->next = NULL;
if(block == NULL) block = p1;
else p2->next = p1;
p2 = p1;
}
return block;
}
int GetBlockQueueSize(BlockQueue *blockQueue){
//获取块长度
BlockQueue *presentBlock;
presentBlock = blockQueue;
int blockQueueSize = 0;
while(presentBlock != NULL){
blockQueueSize++;
presentBlock = presentBlock->next;
}
return blockQueueSize;
}
void ResetBlockQueue(BlockQueue *blockQueue){
//清空块内容
BlockQueue *presentBlock;
presentBlock = blockQueue;
while(presentBlock != NULL){
presentBlock->block.page = NULL;
presentBlock->block.state = IDLE;
presentBlock->block.time = 0;
presentBlock = presentBlock->next;
}
}
void PrintBlockList(BlockQueue *blockQueue, int pageID, int color){
//打印块信息
BlockQueue *presentBlock;
char strl[5], *str2;
presentBlock = blockQueue;
while(presentBlock != NULL){
if(presentBlock->block.state == IDLE)
printf("| |\n");
else{
printf("| %d |\n", presentBlock->block.page->pageID);
}
presentBlock = presentBlock->next;
}
printf("\n");
}
PageQueue* InitializePageQueue(unsigned int pageLength, int maxPageID){
//初始化页面
srand(100);
PageQueue *head;
head = NULL;
PageQueue *p, *q;
for(int i = 0; i < pageLength; ++i){
p = (PageQueue*)malloc(sizeof(PageQueue));
p->page.pageID = (int)(rand() % (maxPageID + 1));
printf("%d ", p->page.pageID);
p->next = NULL;
if(head == NULL) head = p;
else q->next = p;
q = p;
}
printf("\n");
return head;
}
void InitializeProcess(process *proc, unsigned int pageSize, unsigned int maxPageID){
//初始化进程
printf("进程初始化:\n");
proc->pageLength = pageSize;
proc->pages.next = InitializePageQueue(pageSize, maxPageID);
}
BlockQueue* SearchPage(BlockQueue *blockQueue, Page page){
//搜索特定页面
BlockQueue *p;
int blockSize;
p = blockQueue;
while(p != NULL){
if(p->block.page != NULL){
if(p->block.page->pageID == page.pageID)
return p;
}
}
return NULL;
}
BlockQueue* SearchIdleBlock(BlockQueue *blockQueue){
//搜索空闲块
BlockQueue *p;
p = blockQueue;
while(p != NULL){
if(p->block.state == IDLE)
return p;
else p = p->next;
}
return NULL;
}
BlockQueue* GetOldestBlock(BlockQueue *blockQueue){
//取得主存中停留最久的页面,返回它的地址
BlockQueue *p;
p = blockQueue;
if(p == NULL) return p;
int cnt = 0, cur = 0;
int t = p->block.time;
while(p != NULL){
if(t > p->block.time){
t = p->block.time;
cur = cnt;
}
p = p->next;
cnt++;
}
BlockQueue *q;
q = blockQueue;
cnt = 0;
while(q != NULL){
if(cnt == cur) return q;
q = q->next;
cnt++;
}
}
void FIFO(BlockQueue *blockQueue, process *proc){
//先进先出算法
int blockQueueSize = GetBlockQueueSize(blockQueue);
PageQueue *currentPage;
currentPage = proc->pages.next;
BlockQueue *p;
p = blockQueue;
int count = 0, cnt = 0;
while(currentPage != NULL){
int ok = 0;
p = blockQueue;
Time++;
while(p != NULL){
if(p->block.page !=NULL && p->block.page->pageID == currentPage->page.pageID){
ok = 1;
break;
}
p = p->next;
}
if(ok){
PrintBlockList(blockQueue, currentPage->page.pageID, 0);
}
else{
BlockQueue *block;
block = SearchIdleBlock(blockQueue);
if(block != NULL){
block->block.state = BUSY;
block->block.time = Time;
block->block.page = (Page*)malloc(sizeof(Page));
block->block.page->pageID = currentPage->page.pageID;
PrintBlockList(blockQueue, currentPage->page.pageID, 1);
}
else{
BlockQueue *block;
block = GetOldestBlock(blockQueue);
block->block.time = Time;
block->block.page->pageID = currentPage->page.pageID;
PrintBlockList(blockQueue, currentPage->page.pageID, 2);
}
}
currentPage = currentPage->next;
cnt++;
}
printf("%d\n", cnt);
}
int main(){
int pageNumber;
scanf("%d", &pageNumber);
printf("Block Number : %d, Page Number : %d\n", blockNumber, pageNumber);
BlockQueue *blocks;
process proc;
InitializeProcess(&proc, pageNumber, n);
blocks = InitializeBlockQueue(blockNumber);
FIFO(blocks, &proc);
ResetBlockQueue(blocks);
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)