操作系统实验报告Word文档格式.docx
《操作系统实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告Word文档格式.docx(20页珍藏版)》请在冰点文库上搜索。
![操作系统实验报告Word文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/11/5e9275f9-d37d-4fb9-80e5-85757a6df893/5e9275f9-d37d-4fb9-80e5-85757a6df8931.gif)
队列类型描述如下:
structQueueNode{
structPCB_typePCB;
StructQueueNode*next;
}
并设全程量:
structQueueNode*ready_head=NULL,//ready队列队首针
*ready_tail=NULL,//ready队列队尾指针
*blocked_head=NULL,//blocked队列队首针
*blocked_tail=NULL;
//blocked队列队尾针
3)设计子程序:
start_state();
//读入假设的数据,设置系统初始状态
dispath();
//模拟调度
calculate();
//计算CPU利用率
5.源程序
#include<
conio.h>
stdio.h>
stdlib.h>
iostream>
usingnamespacestd;
structPCB_type
intstate;
/*进程状态
2--表示"
执行"
状态
1--表示"
就绪"
状态
0--表示"
阻塞"
状态*/
//运行需要的CPU时间(需运行的时间片个数)
};
structQueueNode*next;
};
structQueueNode*ready_head=NULL;
//ready队列队首指针
structQueueNode*ready_tail=NULL;
//ready队列队尾指针
structQueueNode*blocked_head=NULL;
//blocked队列队首指针
structQueueNode*blocked_tail=NULL;
//blocked队列队尾指针
intuse_cpu,unuse_cpu;
voidstart_state()
{
inti,m,n,k;
structQueueNode*p;
cout<
<
"
请输入就绪队列个数m和阻塞队列个数n"
endl;
cin>
>
m>
n;
p=(structQueueNode*)malloc(sizeof(structQueueNode));
p->
next=NULL;
ready_head=ready_tail=p;
就绪队列进程:
for(i=1;
i<
=m;
i++)
请输入第"
个就绪进程的pid和cpu_time"
p->
PCB.pid>
PCB.cpu_time;
PCB.state=1;
ready_tail->
next=p;
ready_tail=p;
k=1;
p=ready_head->
next;
while(p)
cout<
第"
k<
个就绪进程的pid和cpu_time分别为:
PCB.pid<
"
PCB.cpu_time<
p=p->
k++;
blocked_head=blocked_tail=p;
阻塞队列进程:
=n;
个阻塞进程的pid和cpu_time"
PCB.state=0;
blocked_tail->
blocked_tail=p;
p=blocked_head->
个阻塞进程的pid和cpu_time分别为:
voiddispath()
intx=0,t;
输入时间片t:
;
t;
use_cpu=0;
unuse_cpu=0;
while(ready_head!
=ready_tail||blocked_head!
=blocked_tail)
if(ready_head!
=ready_tail)
{
p=ready_head->
ready_head->
next=p->
p->
if(ready_head->
next==NULL)
ready_tail=ready_head;
}
PCB.state=2;
运行进程:
PCB.cpu_time--;
use_cpu++;
if(p->
PCB.cpu_time)
p->
ready_tail->
ready_tail=p;
else
cout<
进程"
运行完成!
free(p);
else
CPU未运行!
unuse_cpu++;
x++;
if(x==t&
&
blocked_head!
p=blocked_head->
blocked_head->
if(blocked_head->
{
blocked_tail=blocked_head;
}
x=0;
}
voidcalculate()
CPU利用率为:
(use_cpu/double(use_cpu+unuse_cpu))*100<
%"
voidmain()
dispath();
6.实例运行结果
7.实验总结
此次实验通过C语言程序动态地模拟了操作系统中进程的轮转调度算法,模拟程序对PCB进行相应的调度模拟操作。
通过结点建立链表来模拟就绪队列和阻塞队列,每个结点可视为一个就绪队列进程或阻塞队列进程,每个结点包括此进程的PCB信息,同时还包括一个结构体类型的指针指向下一个结点,模拟操作系统中通过先来先服务原则排成的队列。
有n个进程处于就绪状态,有m个进程处于阻塞状态,每次调度时,把CPU分配给就绪队列队首进程,将队首进程的状态置为“运行”,讲需要服务时间减一,CPU运行时间自加一,讲此进程挂到就绪队列尾部,输出次进程名,当此进程需要服务时间为0时,释放次进程。
当经过t个时间片,将阻塞队列队首进程唤醒,挂到就绪队列队尾,知道所有进程都执行完毕。
通过对CPU利用率的计算,明白了时间片大小对系统性能有很大影响,很小的时间片固然有利于短进程,但会增加系统开销,而长时间片则会降低CPU的利用率,同时不利于交互式用户的需求。
通过这次实验,我进一步理解进程调度的过程,掌握进程状态的转变、进程调度的策略,进一步体会多道程序并发执行的特点和具体调度算法的特点,掌握对系统性能的评价方法。
实验三可变分区存储管理
通过这次实验,加深对内存管理的认识,进一步掌握内存的分配、回收算法的思想。
阅读教材《计算机操作系统》第四章,掌握存储器管理相关概念和原理。
编写程序模拟实现内存的动态分区法存储管理。
内存空闲区使用自由链管理,采用最坏适应算法从自由链中寻找空闲区进行分配,内存回收时假定不做与相邻空闲区的合并。
假定系统的内存共640K,初始状态为操作系统本身占用64K。
在t1时间之后,有作业A、B、C、D分别请求8K、16K、64K、124K的内存空间;
在t2时间之后,作业C完成;
在t3时间之后,作业E请求50K的内存空间;
在t4时间之后,作业D完成。
要求编程序分别输出t1、t2、t3、t4时刻内存的空闲区的状态。
1.程序中自由链队列的结点类型可描述如下:
structfreelink{
intlen,address;
/*len为分区长度
/*address为分区起始地址
structfreelink*next;
内存占用区用链表描述,其结点类型描述如下:
structbusylink{
charname;
/*作业或进程名name=’S’表示OS占用
intlen,address;
structbusylink*next;
}
structfreelink*free_head=NULL;
//自由链队列(带头结点)队首指针
structbusylink*busy_head=NULL,//占用区队列队(带头结点)首指针
*busy_tail=NULL;
//占用区队列队尾指针
2.设计子函数:
voidstart(void);
/*设置系统初始状态*/
{structfreelink*p;
structbusylink*q;
free_head=(structfreelink*)malloc(sizeof(structfreelink));
free_head->
//创建自由链头结点
busy_head=busy_tail=(structbusylink*)malloc(sizeof(structbusylink));
busy_head->
//创建占用链头结点
p=(structfreelink*)malloc(sizeof(structfreelink));
address=64;
len=640-64;
(OS占用了64K)
q=(structbusylink*)malloc(sizeof(structbusylink));
q->
name=’S’;
/*S表示操作系统占用*/
len=64;
address=0;
next=q;
busy_tail=q;
voidrequireMemo(charname,intrequire);
/*模拟内存分配*/
voidfreeMemo(charname);
/*模拟内存回收*/
voidpast(inttime);
/*模拟系统过了time时间*/
voidprintlink();
/*输出内存空闲情况(自由链的结点)*/
3.设计主函数:
main()
{start();
past(t1);
requireMemo(‘A’,8);
requireMemo(‘B’,16);
requireMemo(‘C’,64);
requireMemo(‘D’,124);
printlink();
past(t2);
freeMemo(‘C’);
past(t3);
requireMemo(‘E’,50);
freeMemo(‘D’);
structfreelink{
intlen,address;
/*len为分区长度*/
/*address为分区起始地址*/
structfreelink*next;
structbusylink{
charname;
/*作业或进程名name='
S'
表示OS占用*/
intlen,address;
structbusylink*next;
structbusylink*busy_head=NULL;
//占用区队列队(带头结点)首指针
structbusylink*busy_tail=NULL;
voidstart(void)/*设置系统初始状态*/
{
structfreelink*p;
busy_head=busy_tail=(structbusylink*)malloc(sizeof(structbusylink));
//初始空闲链表
//(OS占用了64K)
//初始被占用链表
name='
voidrequireMemo(charname,intrequire)/*模拟内存分配*/
if(free_head->
next->
len<
require)
printf("
Can'
tallocate"
);
exit(0);
structbusylink*p;
structfreelink*w;
p=(structbusylink*)malloc(sizeof(structbusylink));
name=name;
address=free_head->
address;
len=require;
busy_tail->
busy_tail=p;
w=free_head->
next=w->
if(w->
len==require)
free(w);
else
w->
address=w->
address+require;
w->
len=w->
len-require;
structfreelink*u,*v;
u=free_head;
v=free_head->
while((v!
=NULL)&
v->
len>
w->
len)
{
u=v;
v=v->
}
u->
next=w;
next=v;
voidfreeMemo(charname)/*模拟内存回收*/
structbusylink*p,*q;
structfreelink*w,*u,*v;
intlen,address;
q=busy_head;
p=busy_head->
while(p!
=NULL&
(p->
name!
=name))
q=p;
p=p->
if(p==NULL)
printf("
%cisnotexist"
name);
if(p==busy_tail)
busy_tail=q;
q->
len=p->
len;
address=p->
free(p);
w=(structfreelink*)malloc(sizeof(structfreelink));
w->
len=len;
address=address;
u=free_head;
v=free_head->
while(v!
u=v;
v=v->
u->
}
voidpast(inttime)/*模拟系统过了time时间*/
经过时间"
time<
后:
voidprintlink()/*输出内存空闲情况(自由链的结点)*/
{
structfreelink*u,*v;
inti=1;
u=free_head;
v=u->
while(v!
=NULL)
个空闲分区信息为:
address:
address<
len:
v=v->
i++;
voidprintlink2()/*输出内存空闲情况(自由链的结点)*/
{
structbusylink*u,*v;
u=busy_head;
个工作分区信息为:
voidmain(void)
start();
past
(1);
requireMemo('
A'
8);
B'
16);
C'
64);
D'
124);
printlink2();
past(3);
freeMemo('
past(4);
E'
50);
past(5);
);
6.实例运行结果
7.实验总结
此次实验通过C语言程序动态地模拟了操作系统中内存的动态分区分配。
此次实验通过建立结点来模拟空闲分区和已占用分区,每个表示空闲分区或已占用分区的结点都包括有次空闲分区的大小和内存地址以及一个指向下一个空闲或占用分区的结构体指针,通过结点形成空