操作系统实验(3)
实验题目:存储管理实验课程名称:操作系统学院:管理学院
专业班级:
学号(1):
姓名(1):
学号(2): 姓名(2): 任课教师:
2010年05月06日
学院:管理学院班级:
组员:组员:
评定:
实验题目:存储管理实验
(1)实验目的:通过编写和调试存储管理的模拟程序以加深对存储管理方案的理解。
(2)实验内容:设计一个可变式分区分配的存储管理方案。并模拟实现分区的分配和回收过程。
对分区的管理法可以是下面三种算法之一:
首次适应算法
循环首次适应算法
最佳适应算法
(3)源代码(含注释):
首次适应算法(FF):
#include
#include
#include
#include
struct nc{
int n; /*内存号*/
int rl; /*内存容量*/
int sdz; /*首地址*/
struct nc *next;}*head;
struct PCB{
int num; /*进程号*/
char name[10]; /*进程名*/
int nv; /*进程大小*/
int itime; /*进程进入内存时间*/
int ncn; /*装入的内存号*/
int sdzv; /*存入内存的首地址*/
int ntime; /*进程运行总时间*/
int rtime; /*进程运行时间*/
char state; /*进程状态*/
struct PCB *link;}*tou;
int all; /*进程总数变量*/
int tim=0; /*当前时间变量*/
void innc() /*将进程装入内存*/
{
struct nc *md;
struct PCB *xx;
xx=tou;
while(xx!=NULL)
{if(xx->ncn==0&&(xx->itime<=tim||xx->itime<=tim-1))
{md=head;
while(md!=NULL)
{if(md->rl>=xx->nv)
{xx->ncn=md->n;
xx->sdzv=md->sdz;
md->sdz=md->sdz+xx->nv;
md->rl=md->rl-xx->nv;
goto leep;
}
md=md->next;
}
leep:if(md==NULL) printf("the process %s can't into nc\n",xx->name); }
xx=xx->link;
}
}
void sort()
{
struct PCB *p1,*p2;
p1=p2=tou;
while(p1->link!=NULL)
{
if((p1->link)->itime<=tim&&(p1->link)->ncn!=0)
{tou=p2=p1;
while(p1->link!=NULL) {p1=p1->link;}
p1->link=tou;tou=tou->link;p2->link=NULL;
}
else p1=p1->link;
}
}
void creatnc() /*创建内存链表*/
{
struct nc *p1,*p2;
int ncx,ncy;
printf("input the numeber of nc(input 0 to end):");
scanf("%d",&ncx);
p2=(struct nc*)malloc(sizeof(struct nc));p1->next=NULL;head=p2=p1; while(ncx>0)
{
p1->n=ncx;
printf("input the large of nc:");
scanf("%d",&p1->rl);
printf("input the sdz of nc:");
scanf("%d",&ncy);
p1->sdz=ncy;
p2->next=p1;p2=p1;
printf("input the numeber of nc(input 0 to end):");
scanf("%d",&ncx);
if(ncx<=0) break;
p1=(struct nc*)malloc(sizeof(struct nc));
p1->next=NULL;
}
}
void creatPCB() /*创建进程链表*/
{
struct PCB *p1,*p2;
char ch;
int i;
printf("how many PCB you want to creat:");
scanf("%d",&all);
for(i=0;i { p1=(struct PCB*)malloc(sizeof(struct PCB)); p1->link=NULL; if(i==0) tou=p2=p1; printf("input the name of the NO.%d PCB:",i); scanf("%s",p1->name); printf("input the large of the PCB:"); scanf("%d",&p1->nv); printf("input the time of the PCB into the nc:"); scanf("%d",&p1->itime); printf("input the time of the PCB running:"); scanf("%d",&p1->ntime); p1->state='w';p1->rtime=0;p1->ncn=0; if(i!=0) {p2->link=p1;p2=p1;} } innc(); if(tou->itime>tim) sort(); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t ncn \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->ncn); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } void check() /* 建立进程查看函数 */ { struct PCB* pr; pr=tou->link; printf("now the time is:%d\n" ,tim); printf("\n the process that running now is:%s",tou->name); /*显示当前运行进程*/ if(tou->itime<=tim&&tou->ncn!=0) disp(tou); printf("\n the ready team are:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { if(pr->itime<=tim) disp(pr); pr=pr->link; } } void look() { struct nc *pr; printf("\n num \t rl \t sdz \t \n"); pr=head; while(pr!=NULL) { printf("%d \t %d \t %d \t\n",pr->n,pr->rl,pr->sdz); pr=pr->next; } } void freenc() /*内存释放*/ { struct nc *dnc; struct PCB *pr,*pl; dnc=head;pr=pl=tou; while(dnc!=NULL) { if(dnc->n==pr->ncn) break; else dnc=dnc->next; } if(pr->sdzv+pr->nv==dnc->n) {dnc->n=pr->sdzv;dnc->rl=dnc->rl+pr->nv;} else { while(pl!=NULL) {if(pl->ncn==dnc->n) {if(pr->sdzv+pr->nv pl=pl->link; } dnc->sdz=dnc->sdz-pr->nv;dnc->rl=pr->nv; } } void destory() { struct PCB *pr; freenc(); pr=tou; if(pr->link!=NULL) {tou=tou->link; free(pr); all--; if(tou->itime>tim) sort(); } else {free(pr); all=0;} } void running() { struct PCB *pr; pr=tou; pr->rtime=pr->rtime+1; pr->state='r'; tim++; check(); look(); if(pr->rtime>=pr->ntime) destory(); else pr->state='w'; innc(); sort(); } void main() { clrscr(); creatnc(); creatPCB(); check(); while(all>0) {running(); printf("press any key to continue\n"); getch();} printf("the process are finished\n"); } 循环首次适应算法(NF): #include #include #include #include struct nc{ int n; /*内存号*/ int rl; /*内存容量*/ int sdz; /*首地址*/ struct nc *next;}*head; struct PCB{ int num; /*进程号*/ char name[10]; /*进程名*/ int nv; /*进程大小*/ int itime; /*进程进入内存时间*/ int ncn; /*装入的内存号*/ int sdzv; /*存入内存的首地址*/ int ntime; /*进程运行总时间*/ int rtime; /*进程运行时间*/ char state; /*进程状态*/ struct PCB *link;}*tou; int all; /*进程总数变量*/ int tim=0; /*当前时间变量*/ void innc() /*将进程装入内存*/ { struct PCB *xx; xx=tou; while(xx!=NULL) {if(xx->itime<=tim) {while(head!=NULL) {if(head->rl>xx->nv) {head->rl=head->rl-xx->nv;xx->sdzv=head->sdz;head->sdz=head->sdz+xx->nv;xx->ncn=head->n;break;} head=head->next; } if(head==NULL) printf("the PCB can't enter nc as the nc no rl"); } xx=xx->link; } } void sort() { struct PCB *p1,*p2; p1=p2=tou; while(p1!=NULL) { if((p1->link)->itime<=tim) {tou=p2=p1; while(p1->link!=NULL) {p1=p1->link;} p1->link=tou;tou=tou->link;p2->link=NULL; } else p1=p1->link; } } void creatnc() /*创建内存链表*/ { struct nc *p1,*p2; int ncx,ncy; printf("input the numeber of nc(input 0 to end):"); scanf("%d",&ncx); head=p1=p2=(struct nc*)malloc(sizeof(struct nc)); while(ncx>0) { p1->n=ncx; printf("input the large of nc(input 0 to end):"); scanf("%d",&p1->rl); printf("input the sdz of nc:"); scanf("%d",&ncy); p1->sdz=ncy; p2->next=p1;p2=p1; printf("input the numeber of nc(input 0 to end):"); scanf("%d",&ncx); p1=(struct nc*)malloc(sizeof(struct nc)); } p2->next=head; } void creatPCB() /*创建进程链表*/ { struct PCB *p1,*p2; char ch; int i; printf("how many PCB you want to creat:"); scanf("%d",&all); for(i=0;i { p1=(struct PCB*)malloc(sizeof(struct PCB)); p1->link=NULL; if(i==0) tou=p2=p1; printf("input the name of the NO.%d PCB:",i); scanf("%s",p1->name); printf("input the large of the PCB:"); scanf("%d",p1->nv); printf("input the time of the PCB into the nc:"); scanf("%d",&p1->itime); printf("input the time of the PCB running:"); scanf("%d",&p1->ntime); p1->state='w';p1->rtime=0; p2->link=p1;p2=p1; } innc(); if(tou->itime>tim) sort(); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t ncn \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->ncn); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { PCB* pr; pr=tou->link; printf("now the time is:%d\n" ,tim); printf("\n the process that running now is:%s",tou->name); /*显示当前运行进程*/ if(tou->itime<=tim) disp(tou); printf("\n the ready team are:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { if(pr->itime<=tim) disp(pr); pr=pr->link; } } void freenc() /*内存释放*/ { struct nc *dnc; struct PCB *pr,*pl; dnc=head;pr=pl=tou; while(dnc!=NULL) { if(dnc->n==pr->ncn) break; else dnc=dnc->next; } if(pr->sdzv+pr->nv==dnc->n) {dnc->n=pr->sdzv;dnc->rl=dnc->rl+pr->nv;} else { while(pl!=NULL) {if(pl->ncn==dnc->n) {if(pr->sdzv+pr->nv } dnc->n=dnc->n-pr->nv;dnc->rl=pr->nv; } } void destory() { struct PCB *pr; freenc(); pr=tou; if(pr->link!=NULL) {tou=tou->link; free(pr); all--; if(tou->itime>tim) sort(); } else {free(pr); all=0;} } void running() { struct PCB *pr; pr=tou; pr->rtime=pr->rtime+1; pr->state='r'; tim++; check(); if(pr->rtime>=pr->ntime) destory(); else pr->state='w'; sort(); innc(); } void main() { char ch; clrscr(); creatnc(); creatPCB(); check(); while(all>0) { running(); ch=getchar(); } printf("the process are finished\n"); } 最佳适应算法(BF): #include #include #include #include struct nc{ int n; /*内存号*/ int rl; /*内存容量*/ int sdz; /*首地址*/ struct nc *next;}*head; struct PCB{ int num; /*进程号*/ char name[10]; /*进程名*/ int nv; /*进程大小*/ int itime; /*进程进入内存时间*/ int ncn; /*装入的内存号*/ int sdzv; /*存入内存的首地址*/ int ntime; /*进程运行总时间*/ int rtime; /*进程运行时间*/ char state; /*进程状态*/ struct PCB *link;}*tou; int all; /*进程总数变量*/ int tim=0; /*当前时间变量*/ void innc() /*将进程装入内存*/ { struct nc *md; struct PCB *xx; xx=tou;md=head; while(xx!=NULL) {if(xx->itime<=tim) {while(md!=NULL) {if(md->rl>xx->nv) {md->rl=md->rl-xx->nv;xx->sdzv=md->sdz;md->sdz=md->sdz+xx->nv;xx->nc n=md->n;break;} md=md->next; } if(md==NULL) printf("the PCB can't enter nc as the nc no rl"); } xx=xx->link; } } void sort() { struct PCB *p1,*p2; p1=p2=tou; while(p1!=NULL) { if((p1->link)->itime<=tim) {tou=p2=p1; while(p1->link!=NULL) {p1=p1->link;} p1->link=tou;tou=tou->link;p2->link=NULL; } else p1=p1->link; } } void ncsort() { struct nc *p1,*p2,*p3; int i=0,j; p1=head; while(p1!=NULL) { i++; p1=p1->next;} for(j=0;j { p1=head;p2=p1->next; while(p2->next!=NULL) { if(p1->rl>p2->rl) p3=p1;p1=p2;p2=p3; p1=p1->next;p2=p1->next; } } } void creatnc() /*创建内存链表*/ { struct nc *p1,*p2; int ncx,ncy; printf("input the numeber of nc(input 0 to end):"); scanf("%d",&ncx); head=p1=p2=(struct nc*)malloc(sizeof(struct nc)); while(ncx>0) { p1->n=ncx; printf("input the large of nc(input 0 to end):"); scanf("%d",&p1->rl); printf("input the sdz of nc:"); scanf("%d",&ncy); p1->sdz=ncy; p2->next=p1;p2=p1; printf("input the numeber of nc(input 0 to end):"); scanf("%d",&ncx); p1=(struct nc*)malloc(sizeof(struct nc)); } ncsort(); } void creatPCB() /*创建进程链表*/ { struct PCB *p1,*p2; char ch; int i; printf("how many PCB you want to creat:"); scanf("%d",&all); for(i=0;i { p1=(struct PCB*)malloc(sizeof(struct PCB)); p1->link=NULL; if(i==0) tou=p2=p1; printf("input the name of the NO.%d PCB:",i); scanf("%s",p1->name); printf("input the large of the PCB:"); scanf("%d",p1->nv); printf("input the time of the PCB into the nc:"); scanf("%d",&p1->itime); printf("input the time of the PCB running:"); scanf("%d",&p1->ntime); p1->state='w';p1->rtime=0; p2->link=p1;p2=p1; } innc(); if(tou->itime>tim) sort(); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t ncn \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->ncn); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { PCB* pr; pr=tou->link; printf("now the time is:%d\n" ,tim); printf("\n the process that running now is:%s",tou->name); /*显示当前运行进程*/ if(tou->itime<=tim) disp(tou); printf("\n the ready team are:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { if(pr->itime<=tim) disp(pr); pr=pr->link; } } void freenc() /*内存释放*/ { struct nc *dnc; struct PCB *pr,*pl; dnc=head;pr=pl=tou; while(dnc!=NULL) { if(dnc->n==pr->ncn) break; else dnc=dnc->next; } if(pr->sdzv+pr->nv==dnc->n) {dnc->n=pr->sdzv;dnc->rl=dnc->rl+pr->nv;} else { while(pl!=NULL) {if(pl->ncn==dnc->n) {if(pr->sdzv+pr->nv } dnc->n=dnc->n-pr->nv;dnc->rl=pr->nv; } } void destory() { struct PCB *pr; freenc(); pr=tou; if(pr->link!=NULL) {tou=tou->link; free(pr); all--; if(tou->itime>tim) sort(); } else {free(pr); all=0;} } void running() { struct PCB *pr; pr=tou; pr->rtime=pr->rtime+1; pr->state='r'; tim++; check(); if(pr->rtime>=pr->ntime) destory(); else pr->state='w'; sort(); innc(); } void main() { char ch; clrscr(); creatnc(); creatPCB(); check(); while(all>0) { running(); ch=getchar(); } printf("the process are finished\n"); } (4)每个函数的流程图: 首次适应算法(FF ): (1)innc() 函数:该函数用于将进程装入内存。 否 是 是 是 否 是 否 开始 struct nc *md; struct PCB *xx; xx=tou; xx 是否为空 结束 xx->ncn==0&&(xx->itime<=tim||xx->itime<=tim-1) md=head md 是否为空 leep:若md 为空 输出:进程XX 无法进入内存 md->rl 大于等于xx->nv (2)sort() 函数: 是 否 否 是 否 是 xx->ncn=md->n; xx->sdzv=md->sdz; md->sdz=md->sdz+xx->nv md->rl=md->rl-xx->nv; goto leep; md=md->nex xx=xx->link 开始 struct PCB *p1,*p2; p1=p2=tou; p1->link 是否为空? 结束 (p1->link)->itime 小于等于tim 而且(p1->link)->ncn 不为零 p1=p1->link tou=p2=p1 p1->link 是否为空? p1=p1->link p1->link=tou tou=tou->link p2->link=NULL (3)creatnc()函数:该函数用于创建内存链表。 是 否 否 (4)creatPCB ()函数:该函数用于创建进程链表。 是 否 开始 输入内存号 ncx 大于0? 结束 p1->n=ncx 输入内存大小 输入内存的首地址 p1->sdz=ncy p2->next=p1 p2=p1 输入内存号 ncx 小于等于0? break p1=(struct nc*)malloc(sizeof(struct nc)) p1->next=NULL 开始 输入要创建的PCB 个数 i 小于all ? 是 (5)disp(PCB * pr) 函数:该函数用于建立进程显示函数,显示当前进程。 (6)check ()函数:该函数用于查看进程。 否 是 p1=(struct PCB*)malloc(sizeof(struct PCB)) p1->link=NULL 若(i==0) 则tou=p2=p1 输入第X 个PCB 的名 输入这个PCB 的大小 输入这个PCB 进入内存的时间 输入这个PCB 运行的时间 p1->state='w' p1->rtime=0 p1->ncn=0 若i 不为零 则 p2->link=p1;p2=p1 调用函数innc () tou->itime 大于tim ? 调用函数sort () 结束 开始 分别输出进程名、进程状态、装入的内存号、进程运行总时间和进程运行时间 结束 开始 显示当前运行进程 显示就绪队列状态 pr 是否为空?