操作系统实验指导书
void Int2() { printf(\ exit(0); }
main() { int exitpid; signal(SIGINT,SIG_IGN); signal(SIGQUIT,SIG_IGN); if(pid1=fork()) { signal(SIGUSR1,Int1); signal(SIGINT,SIG_IGN); pause(); exit(0); } else { if(pid2=fork()) { signal(SIGUSR1,Int1); signal(SIGINT,SIG_IGN); pause(); exit(0); } else { signal(SIGINT,IntDelete); waitpid(-1,&exitpid,0); printf(\ exit(0); } } }
18
操作系统实验指导书
实验7存储管理实验
1.实验目的
通过请求页式存储管理中页面置换算法模拟设计,了解虚拟存储技术的技术特点,掌握请求页式存储管理的页面置换算法。
2.实验设备
计算机、安装了Linux的虚拟机。
3. 实验内容及要求
设计一个虚拟存储区和内存工作区,并使用下列算法计算访问命中率.
(1) 进先出的算法(FIFO)
(2) 最近最少使用的算法(LRU) (3) 最佳淘汰算法(OPT) (4) 最少访问页面算法(LFU) (5) 最近最不经常使用算法(NUR)
命中率=(1-页面失效次数)/页地址流长度
4.实验步骤
4.1定义页面存储管理的相关数据结构及初始化
#define TRUE 1 #define FALSE 0 #define INVALID -1 #define NUL 0
#define total_instruction 320 /*指令流长*/ #define total_vp 32 /*虚页长*/ #define clear_period 50 /*清零周期*/
typedef struct{ /*页面结构*/ int pn,pfn,counter,time; }pl_type;
pl_type pl[total_vp]; /*页面结构数组*/
19
操作系统实验指导书
struct pfc_struct{ /*页面控制结构*/ int pn,pfn;
struct pfc_struct *next; };
typedef struct pfc_struct pfc_type;
pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail; int diseffect,a[total_instruction];
int page[total_instruction], offset[total_instruction]; void initialize(); void FIFO(); void LRU(); void OPT(); void NUR();
void initialize(total_pf) /*初始化相关数据结构*/ int total_pf; /*用户进程的内存页面数*/ {
int i;
diseffect=0;
for(i=0;i pl[i].pn=i;pl[i].pfn=INVALID; /*置页面控制结构中的页号,页面为空*/ pl[i].counter=0;pl[i].time=-1; /*页面控制结构中的访问次数为0,时间为-1*/ } for(i=1;i pfc[i-1].next=&pfc[i];pfc[i-1].pfn=i-1;/*建立pfc[i-1]和pfc[i]之间的连接*/ } pfc[total_pf-1].next=NUL;pfc[total_pf-1].pfn=total_pf-1; freepf_head=&pfc[0]; /*页面队列的头指针为pfc[0]*/ } 4.2 FIFO算法 void FIFO(total_pf) /*FIFO(First in First out)ALGORITHM*/ int total_pf; /*用户进程的内存页面数*/ { int i; pfc_type *p, *t; initialize(total_pf); /*初始化相关页面控制用数据结构*/ busypf_head=busypf_tail=NUL; /*忙页面队列头,对列尾链接*/ 20 操作系统实验指导书 for(i=0;i if(pl[page[i]].pfn==INVALID) /*页面失效*/ { diseffect+=1; /*失效次数*/ if(freepf_head==NUL) /*无空闲页面*/ { p=busypf_head->next; pl[busypf_head->pn].pfn=INVALID; /*释放忙页面队列中的第一个页面*/ freepf_head=busypf_head; freepf_head->next=NUL; busypf_head=p; } p=freepf_head->next; /*按方式调新页面入内存页面*/ freepf_head->next=NUL; freepf_head->pn=page[i]; pl[page[i]].pfn=freepf_head->pfn; if(busypf_tail==NUL) busypf_head=busypf_tail=freepf_head; else { busypf_tail->next=freepf_head; busypf_tail=freepf_head; } freepf_head=p; } } printf(\ } 4.3 LRU算法 void LRU(total_pf) int total_pf; { int min,minj,i,j,present_time; initialize(total_pf);present_time=0; for(i=0;i if(pl[page[i]].pfn==INVALID) /*页面失效*/ { diseffect++; if(freepf_head==NUL) /*无空闲页面*/ 21 操作系统实验指导书 { min=32767; for(j=0;j if(min>pl[j].time&&pl[j].pfn!=INVALID) { min=pl[j].time; minj=j; } freepf_head=&pfc[pl[minj].pfn]; pl[minj].pfn=INVALID; pl[minj].time=-1; freepf_head->next=NUL; } pl[page[i]].pfn=freepf_head->pfn; pl[page[i]].time=present_time; freepf_head=freepf_head->next; } else pl[page[i]].time=present_time; present_time++; } printf(\ } 4.4 NUR算法 void NUR(total_pf) int total_pf; { int i,j,dp,cont_flag,old_dp; pfc_type *t; initialize(total_pf); dp=0; for(i=0;i if(pl[page[i]].pfn==INVALID) /*页面失效*/ { diseffect++; if(freepf_head==NUL) /*无空闲页面*/ { cont_flag=TRUE;old_dp=dp; while(cont_flag) if(pl[dp].counter==0&&pl[dp].pfn!=INVALID) cont_flag=FALSE; 22