操作系统实验源代码.docx
《操作系统实验源代码.docx》由会员分享,可在线阅读,更多相关《操作系统实验源代码.docx(33页珍藏版)》请在冰豆网上搜索。
操作系统实验源代码
操作系统实验源代码
C++控制台程序(win32consoleapplication)
实验一:
处理机调度算法的实现
#include"stdafx.h"
#include"stdio.h"
#include
#include
#definegetpch(type)(type*)malloc(sizeof(type))
#defineNULL0
structpcb{/*定义进程控制块PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
voidsort()/*建立对进程进行优先级排列函数*/
{
PCB*first,*second;
intinsert=0;
if((ready==NULL)||((p->super)>(ready->super)))/*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else/*进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!
=NULL)
{
if((p->super)>(second->super))/*若插入进程比当前进程优先数大,*/
{/*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else/*插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0)first->link=p;
}
}
voidinput()/*建立进程控制块函数*/
{
inti,num;
system("cls");/*清屏*/
printf("\n请输入进程数:
");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n进程号No.%d:
\n",i);
p=getpch(PCB);
printf("\n输入进程名:
");
scanf("%s",p->name);
printf("\n输入进程优先数:
");
scanf("%d",&p->super);
printf("\n输入进程运行时间:
");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='W';
p->link=NULL;
sort();/*调用sort函数*/
}
}
intspace()
{
intl=0;
PCB*pr=ready;
while(pr!
=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
{
printf("\n进程名\t状态\t优先数\t需要运行时间\t已经运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
voidcheck()/*建立进程查看函数*/
{
PCB*pr;
printf("\n****当前正在运行的进程是:
\n");/*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n****当前就绪队列状态为:
\n");/*显示就绪队列状态*/
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();/*调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort();/*调用sort函数*/
}
}
voidmain()/*主函数*/
{
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&&(ready!
=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n现在是第%d次运行:
\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任意键继续......\n");
}
printf("\n\n进程已经完成.\n");
}
实验二:
存储器的分配与回收算法实现
#include"stdafx.h"//Memoryallocationandrecoveryalgorithm
#include
#include
#include
usingnamespacestd;
typedefstructFreeLink{//定义自由链
structFreeLink*prior;
charname;
intstart;
intsize;
boolflag;
structFreeLink*next;
}*ptr,*head;
headtop;
ptrp;
voidprint(){//将内存分配情况打印到屏幕上
p=top;
cout<<"************************内存分配情况表************************"<cout<<"区号\t\t"<<"起始位置\t"<<"区间长度\t"<<"区间状态\t"<do{
cout<name<<"\t\t"<start<<"\t\t"<size<<"\t\t";
if(p->flag==false){cout<<"空闲"<else{cout<<"已占用"<p=p->next;
}
while(p!
=NULL);
}
voidclear(){//结束操作时清空“内存”以备其他操作
do{
p=top;
top=top->next;
free(p);
}
while(top!
=NULL);
}
voidasc(ptr&p){//最佳适应法的内存分配函数
intmin;
ptrop;
FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));
cout<<"请输入要分配内存的进程名"<cin>>fl->name;
cout<<"请输入要分配内存的大小"<cin>>fl->size;
min=256;
fl->flag=true;
do{
if(p->flag==false&&p->size<=min&&p->size>=fl->size){
min=p->size;
op=p;
}
p=p->next;
}
while(p!
=NULL);
if(op->size>fl->size){
fl->start=op->start;
op->start=fl->start+fl->size;
op->size=op->size-fl->size;
fl->next=op;
fl->prior=op->prior;
op->prior->next=fl;
op->prior=fl;
gotoflag1;
}
if(op->size==fl->size){
op->flag=fl->flag;
op->name=fl->name;
free(fl);
gotoflag1;
}
cout<<"内存过小,分配失败!
"<flag1:
cout<<"分配成功!
"<flag2:
;
}
voiddec(ptr&p){//最坏适应法的内存分配函数
intmax;
ptrop;
FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));
cout<<"请输入要分配内存的进程名"<cin>>fl->name;
cout<<"请输入要分配内存的大小"<cin>>fl->size;
max=fl->size;
fl->flag=true;
do{
if(p->flag==false&&p->size>=max){
max=p->size;
op=p;
}
p=p->next;
}
while(p!
=NULL);
if(op->size>fl->size){
fl->start=op->start;
op->start=fl->start+fl->size;
op->size=op->size-fl->size;
fl->next=op;
fl->prior=op->prior;
op->prior->next=fl;
op->prior=fl;
gotoflag3;
}
if(op->size==fl->size){
op->flag=fl->flag;
op->name=fl->name;
free(fl);
gotoflag3;
}
cout<<"内存过小,分配失败!
"<flag3:
cout<<"分配成功!
"<flag4:
;
}
voidsplice(ptr&p){//若被操作的内存有相邻空闲区则将空闲区拼接合并
intx;
if(p->prior->flag==false&&p->next->flag==false)x=1;
if((p->prior->flag==false&&p->next->flag==true)||(p->prior->flag==false&&p->next==NULL))x=2;
if((p->prior->flag==true&&p->next->flag==false)||(p->prior==NULL&&p->next->flag==false))x=3;
if((p->prior->flag==true&&p->next->flag==true)||(p->prior==NULL&&p->next->flag==true)||(p->prior->flag==true&&p->next==NULL))x=4;
switch(x){
case1:
p->next->prior=p->prior;
p->prior->next=p->next;
p->prior->size=p->prior->size+p->size+p->next->size;
p->prior->next=p->next->next;
if(p->next->next!
=NULL)p->next->next->prior=p->next->prior;
free(p->next);
free(p);
break;
case2:
if(p->next==NULL){
p->prior->next=p->next;
}else{
p->next->prior=p->prior;
p->prior->next=p->next;
}
p->prior->size=p->prior->size+p->size;
free(p);
break;
case3:
if(p->prior==NULL){
top=p->next;
p->next->prior=NULL;
p->next->start=p->start;
p->next->size=p->next->size+p->size;
}else{
p->next->prior=p->prior;
p->prior->next=p->next;
p->next->start=p->start;
p->next->size=p->next->size+p->size;
}
free(p);
break;
case4:
p->name='@';
p->flag=false;
break;
}
}
voidallocate(ptr&p){//最先适应法的内存分配函数
FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));
cout<<"请输入要分配内存的进程名"<cin>>fl->name;
cout<<"请输入要分配内存的大小"<cin>>fl->size;
fl->flag=true;
do{
if(p->flag==false&&p->size>fl->size){
fl->start=p->start;
p->start=fl->start+fl->size;
p->size=p->size-fl->size;
fl->next=p;
fl->prior=p->prior;
p->prior->next=fl;
p->prior=fl;
gotoa;
}
if(p->flag==false&&p->size==fl->size){
p->flag=fl->flag;
p->name=fl->name;
free(fl);
gotoa;
}
p=p->next;
}
while(p!
=NULL);
cout<<"内存过小,分配失败!
"<a:
cout<<"分配成功!
"<b:
;
}
voidrecover(ptr&p){//内存回收函数
charn='';
cout<<"请输入要回收的内存对应的进程名";
cin>>n;
do{
if(p->flag==true&&p->name==n){
splice(p);
gotoc;
}
p=p->next;
}
while(p!
=NULL);
cout<<"内存并未分配给对应进程,回收失败!
"<c:
cout<<"内存回收成功!
"<d:
;
}
intffa(){//最先适应法
charchoice='';
print();
ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));
pcb->next=top;
pcb->prior=top->prior;
top->prior=pcb;
pcb->start=top->start;
cout<<"请输入要为系统分配的内存块名"<cin>>pcb->name;
cout<<"请输入要分配内存的大小"<e:
cout<<"超过内存最大容量请重新输入要分配内存的大小"<f:
cin>>pcb->size;
if(pcb->size>256)gotoe;
top->size=top->size-pcb->size;
top=pcb;
top->flag=true;
top->next->start+=top->size;
print();
while(true){
do{
p=top->next;
cout<<"请从下列选项中进行选择"<cout<<"1.分配内存"<cout<<"2.回收内存"<cout<<"3.结束操作"<cout<<"请输入你的选择";
cin>>choice;
}
while(choice!
='1'&&choice!
='2'&&choice!
='3');
switch(choice){
case'1':
allocate(p);print();break;
case'2':
recover(p);print();break;
case'3':
clear();return0;break;
}
}
}
intbfa(){//最佳适应法
charchoice='';
print();
ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));
pcb->next=top;
pcb->prior=top->prior;
top->prior=pcb;
pcb->start=top->start;
cout<<"请输入要为系统分配的内存块名"<cin>>pcb->name;
cout<<"请输入要分配内存的大小"<g:
cout<<"超过内存最大容量请重新输入要分配内存的大小"<h:
cin>>pcb->size;
if(pcb->size>256)gotog;
top->size=top->size-pcb->size;
top=pcb;
top->flag=true;
top->next->start+=top->size;
print();
while(true){
do{
p=top->next;
cout<<"请从下列选项中进行选择"<cout<<"1.分配内存"<cout<<"2.回收内存"<cout<<"3.结束操作"<cout<<"请输入你的选择";
cin>>choice;
}
while(choice!
='1'&&choice!
='2'&&choice!
='3');
switch(choice){
case'1':
asc(p);print();break;
case'2':
recover(p);print();break;
case'3':
clear();return0;break;
}
}
}
intwfa(){//最坏适应法
charchoice='';
print();
ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));
pcb->next=top;
pcb->prior=top->prior;
top->prior=pcb;
pcb->start=top->start;
cout<<"请输入要为系统分配的内存块名"<cin>>pcb->name;
cout<<"请输入要分配内存的大小"<i:
cout<<"超过内存最大容量请重新输入要分配内存的大小"<j:
cin>>pcb->size;
if(pcb->size>256)gotoi;
top->size=top->size-pcb->size;
top=pcb;
top->flag=true;
top->next->start+=top->size;
print();
while(true){
do{
p=top->next;
cout<<"请从下列选项中进行选择"<cout<<"1.分配内存"<cout<<"2.回收内存"<cout<<"3.结束操作"<cout<<"请输入你的选择";
cin>>choice;
}
while(choice!
='1'&&choice!
='2'&&choice!
='3');
switch(choice){
case'1':
dec(p);print();break;
case'2':
recover(p);print();break;
case'3':
clear();return0;break;
}
}
}
intmain(){//主函数
charchoice='';
ptrfree=(FreeLink*)malloc(sizeof(FreeLink));
top=free;
top->name='@';
top->start=0;
top->size=256;
top->flag=false;
top->prior=NULL;
top->next=NULL;
cout<<"***************Memoryallocationandrecoveryalgorithm***************"<cout<<"************************存储器的分配与回收算法********