操作系统实验源代码Word文件下载.docx
- 文档编号:1540951
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:33
- 大小:20.40KB
操作系统实验源代码Word文件下载.docx
《操作系统实验源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《操作系统实验源代码Word文件下载.docx(33页珍藏版)》请在冰点文库上搜索。
link;
while(second!
=NULL)
if((p->
(second->
super))/*若插入进程比当前进程优先数大,*/
{/*插入到当前进程前面*/
link=second;
first->
link=p;
second=NULL;
insert=1;
else/*插入进程优先数最低,则插入到队尾*/
first=first->
second=second->
if(insert==0)first->
}
voidinput()/*建立进程控制块函数*/
inti,num;
system("
cls"
);
/*清屏*/
printf("
\n请输入进程数:
"
scanf("
%d"
&
num);
for(i=1;
i<
=num;
i++)
\n进程号No.%d:
\n"
i);
p=getpch(PCB);
\n输入进程名:
"
%s"
p->
name);
\n输入进程优先数:
p->
super);
\n输入进程运行时间:
ntime);
rtime=0;
state='
W'
;
link=NULL;
sort();
/*调用sort函数*/
intspace()
intl=0;
PCB*pr=ready;
while(pr!
l++;
pr=pr->
return(l);
voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
\n进程名\t状态\t优先数\t需要运行时间\t已经运行时间\n"
|%s\t"
pr->
|%c\t"
state);
|%d\t"
|%d\t\t"
rtime);
voidcheck()/*建立进程查看函数*/
PCB*pr;
\n****当前正在运行的进程是:
/*显示当前运行进程*/
disp(p);
pr=ready;
\n****当前就绪队列状态为:
/*显示就绪队列状态*/
disp(pr);
voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
\n进程[%s]已完成.\n"
free(p);
voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
(p->
rtime)++;
if(p->
rtime==p->
ntime)
destroy();
/*调用destroy函数*/
else
super)--;
/*调用sort函数*/
voidmain()/*主函数*/
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&
&
(ready!
=NULL))
ch=getchar();
h++;
-----------------------------------------------------"
\n现在是第%d次运行:
h);
p=ready;
ready=p->
R'
check();
running();
\n按任意键继续......\n"
\n\n进程已经完成.\n"
}
实验二:
存储器的分配与回收算法实现
//Memoryallocationandrecoveryalgorithm
iostream>
malloc.h>
usingnamespacestd;
typedefstructFreeLink{//定义自由链
structFreeLink*prior;
charname;
intstart;
intsize;
boolflag;
structFreeLink*next;
}*ptr,*head;
headtop;
ptrp;
voidprint(){//将内存分配情况打印到屏幕上
p=top;
cout<
<
************************内存分配情况表************************"
endl;
区号\t\t"
起始位置\t"
区间长度\t"
区间状态\t"
do{
cout<
name<
\t\t"
start<
size<
if(p->
flag==false){cout<
空闲"
else{cout<
已占用"
p=p->
next;
}
while(p!
=NULL);
voidclear(){//结束操作时清空“内存”以备其他操作
p=top;
top=top->
free(p);
while(top!
voidasc(ptr&
p){//最佳适应法的内存分配函数
intmin;
ptrop;
FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));
请输入要分配内存的进程名"
cin>
>
fl->
name;
请输入要分配内存的大小"
size;
min=256;
fl->
flag=true;
flag==false&
=min&
size>
=fl->
size){
min=p->
op=p;
}
if(op->
start=op->
start;
op->
start=fl->
start+fl->
size=op->
size-fl->
next=op;
prior=op->
prior;
prior->
next=fl;
prior=fl;
gotoflag1;
size==fl->
flag=fl->
flag;
name=fl->
free(fl);
内存过小,分配失败!
gotoflag2;
flag1:
分配成功!
flag2:
;
voiddec(ptr&
p){//最坏适应法的内存分配函数
intmax;
max=fl->
=max){
max=p->
gotoflag3;
gotoflag4;
flag3:
flag4:
voidsplice(ptr&
p){//若被操作的内存有相邻空闲区则将空闲区拼接合并
intx;
next->
flag==false)x=1;
flag==true)||(p->
next==NULL))x=2;
flag==true&
flag==false)||(p->
prior==NULL&
flag==false))x=3;
next==NULL))x=4;
switch(x){
case1:
prior=p->
p->
next=p->
size=p->
size+p->
next!
=NULL)p->
free(p->
next);
break;
case2:
if(p->
next==NULL){
p->
}else{
case3:
prior==NULL){
top=p->
prior=NULL;
start=p->
p->
case4:
name='
@'
flag=false;
voidallocate(ptr&
p){//最先适应法的内存分配函数
fl->
next=p;
gotoa;
free(fl);
gotob;
a:
b:
voidrecover(ptr&
p){//内存回收函数
charn='
'
请输入要回收的内存对应的进程名"
n;
name==n){
splice(p);
gotoc;
p=p->
内存并未分配给对应进程,回收失败!
gotod;
c:
内存回收成功!
d:
intffa(){//最先适应法
charchoice='
print();
ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));
pcb->
next=top;
prior=top->
top->
prior=pcb;
start=top->
请输入要为系统分配的内存块名"
pcb->
gotof;
e:
超过内存最大容量请重新输入要分配内存的大小"
f:
if(pcb->
256)gotoe;
size=top->
size-pcb->
top=pcb;
start+=top->
while(true){
p=top->
请从下列选项中进行选择"
1.分配内存"
2.回收内存"
3.结束操作"
请输入你的选择"
cin>
choice;
while(choice!
='
1'
choice!
2'
3'
switch(choice){
case'
:
allocate(p);
print();
break;
recover(p);
clear();
return0;
intbfa(){//最佳适应法
gotoh;
g:
h:
256)gotog;
asc(p);
intwfa(){//最坏适应法
gotoj;
i:
j:
256)gotoi;
dec(p);
intmain(){//主函数
charchoice='
ptrfree=(FreeLink*)malloc(sizeof(FreeLink));
top=free;
start=0;
size=256;
next=NULL;
***************Memoryallocationandrecoveryalgorithm***************"
************************存储器的分配与回收算法********
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 源代码