操作系统实验报告.docx

上传人:b****3 文档编号:5337812 上传时间:2022-12-15 格式:DOCX 页数:20 大小:109.79KB
下载 相关 举报
操作系统实验报告.docx_第1页
第1页 / 共20页
操作系统实验报告.docx_第2页
第2页 / 共20页
操作系统实验报告.docx_第3页
第3页 / 共20页
操作系统实验报告.docx_第4页
第4页 / 共20页
操作系统实验报告.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

操作系统实验报告.docx

《操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告.docx(20页珍藏版)》请在冰豆网上搜索。

操作系统实验报告.docx

操作系统实验报告

操作系统

实验报告

 

班级:

计算机1007班姓名:

陶向东

学号:

100511726

日期:

2012.5.27

实验二进程调度

1.目的和要求

通过这次实验,理解进程调度的过程,进一步掌握进程状态的转变、进程调度的策略,进一步体会多道程序并发执行的特点,并分析具体的调度算法的特点,掌握对系统性能的评价方法。

2.实验内容

阅读教材《计算机操作系统》第二章和第三章,掌握进程管理及调度相关概念和原理。

编写程序模拟实现进程的轮转法调度过程,模拟程序只对PCB进行相应的调度模拟操作,不需要实际程序。

假设初始状态为:

有n个进程处于就绪状态,有m个进程处于阻塞状态。

采用轮转法进程调度算法进行调度(调度过程中,假设处于执行状态的进程不会阻塞),且每过t个时间片系统释放资源,唤醒处于阻塞队列队首的进程。

程序要求如下:

1)输出系统中进程的调度次序;

2)计算CPU利用率。

3.实验环境

Windows操作系统、VC++6.0

C语言

4.设计思想

1)程序中进程可用PCB表示,其类型描述如下:

structPCB_type

{

intpid;//进程名

intstate;//进程状态

2——表示“执行”状态

1——表示“就绪”状态

0——表示“阻塞”状态

intcpu_time;//运行需要的CPU时间(需运行的时间片个数)

}

2)设置两个队列,将处于“就绪”状态的进程PCB挂在队列ready中;将处于“阻塞”状态的进程PCB挂在队列blocked中。

队列类型描述如下:

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

#include

#include

#include

usingnamespacestd;

structPCB_type

{

intpid;//进程名

intstate;/*进程状态

2--表示"执行"状态

1--表示"就绪"状态

0--表示"阻塞"状态*/

intcpu_time;//运行需要的CPU时间(需运行的时间片个数)

};

structQueueNode{

structPCB_typePCB;

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"<

cin>>m>>n;

p=(structQueueNode*)malloc(sizeof(structQueueNode));

p->next=NULL;

ready_head=ready_tail=p;

cout<<"就绪队列进程:

"<

for(i=1;i<=m;i++)

{

p=(structQueueNode*)malloc(sizeof(structQueueNode));

p->next=NULL;

cout<<"请输入第"<

cin>>p->PCB.pid>>p->PCB.cpu_time;

p->PCB.state=1;

ready_tail->next=p;

ready_tail=p;

}

k=1;

p=ready_head->next;

while(p)

{

cout<<"第"<

"<PCB.pid<<""<PCB.cpu_time<

p=p->next;

k++;

}

p=(structQueueNode*)malloc(sizeof(structQueueNode));

p->next=NULL;

blocked_head=blocked_tail=p;

cout<<"阻塞队列进程:

"<

for(i=1;i<=n;i++)

{

p=(structQueueNode*)malloc(sizeof(structQueueNode));

p->next=NULL;

cout<<"请输入第"<

cin>>p->PCB.pid>>p->PCB.cpu_time;

p->PCB.state=0;

blocked_tail->next=p;

blocked_tail=p;

}

k=1;

p=blocked_head->next;

while(p)

{

cout<<"第"<

"<PCB.pid<<""<PCB.cpu_time<

p=p->next;

k++;

}

}

voiddispath()

{

structQueueNode*p;

intx=0,t;

cout<<"输入时间片t:

";

cin>>t;

use_cpu=0;

unuse_cpu=0;

while(ready_head!

=ready_tail||blocked_head!

=blocked_tail)

{

if(ready_head!

=ready_tail)

{

p=ready_head->next;

ready_head->next=p->next;

p->next=NULL;

if(ready_head->next==NULL)

{

ready_tail=ready_head;

}

p->PCB.state=2;

cout<<"运行进程:

"<PCB.pid<

p->PCB.cpu_time--;

use_cpu++;

if(p->PCB.cpu_time)

{

p->PCB.state=1;

ready_tail->next=p;

ready_tail=p;

ready_tail->next=NULL;

}

else

{

cout<

cout<<"进程"<PCB.pid<<"运行完成!

"<

cout<

free(p);

}

}

else

{

cout<<"CPU未运行!

"<

unuse_cpu++;

}

x++;

if(x==t&&blocked_head!

=blocked_tail)

{

p=blocked_head->next;

blocked_head->next=p->next;

p->next=NULL;

if(blocked_head->next==NULL)

{

blocked_tail=blocked_head;

}

ready_tail->next=p;

ready_tail=p;

ready_tail->next=NULL;

p->PCB.state=1;

x=0;

}

}

}

voidcalculate()

{

cout<<"CPU利用率为:

"<<(use_cpu/double(use_cpu+unuse_cpu))*100<<"%"<

}

voidmain()

{

start_state();

dispath();

calculate();

}

6.实例运行结果

7.实验总结

此次实验通过C语言程序动态地模拟了操作系统中进程的轮转调度算法,模拟程序对PCB进行相应的调度模拟操作。

通过结点建立链表来模拟就绪队列和阻塞队列,每个结点可视为一个就绪队列进程或阻塞队列进程,每个结点包括此进程的PCB信息,同时还包括一个结构体类型的指针指向下一个结点,模拟操作系统中通过先来先服务原则排成的队列。

假设初始状态为:

有n个进程处于就绪状态,有m个进程处于阻塞状态,每次调度时,把CPU分配给就绪队列队首进程,将队首进程的状态置为“运行”,讲需要服务时间减一,CPU运行时间自加一,讲此进程挂到就绪队列尾部,输出次进程名,当此进程需要服务时间为0时,释放次进程。

当经过t个时间片,将阻塞队列队首进程唤醒,挂到就绪队列队尾,知道所有进程都执行完毕。

通过对CPU利用率的计算,明白了时间片大小对系统性能有很大影响,很小的时间片固然有利于短进程,但会增加系统开销,而长时间片则会降低CPU的利用率,同时不利于交互式用户的需求。

通过这次实验,我进一步理解进程调度的过程,掌握进程状态的转变、进程调度的策略,进一步体会多道程序并发执行的特点和具体调度算法的特点,掌握对系统性能的评价方法。

 

实验三可变分区存储管理

1.目的和要求

通过这次实验,加深对内存管理的认识,进一步掌握内存的分配、回收算法的思想。

2.实验内容

阅读教材《计算机操作系统》第四章,掌握存储器管理相关概念和原理。

编写程序模拟实现内存的动态分区法存储管理。

内存空闲区使用自由链管理,采用最坏适应算法从自由链中寻找空闲区进行分配,内存回收时假定不做与相邻空闲区的合并。

假定系统的内存共640K,初始状态为操作系统本身占用64K。

在t1时间之后,有作业A、B、C、D分别请求8K、16K、64K、124K的内存空间;在t2时间之后,作业C完成;在t3时间之后,作业E请求50K的内存空间;在t4时间之后,作业D完成。

要求编程序分别输出t1、t2、t3、t4时刻内存的空闲区的状态。

3.实验环境

Windows操作系统、VC++6.0

C语言

4.设计思想

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->next=NULL;//创建自由链头结点

busy_head=busy_tail=(structbusylink*)malloc(sizeof(structbusylink));

busy_head->next=NULL;//创建占用链头结点

p=(structfreelink*)malloc(sizeof(structfreelink));

p->address=64;

p->len=640-64;(OS占用了64K)

p->next=NULL;

free_head->next=p;

q=(structbusylink*)malloc(sizeof(structbusylink));

q->name=’S’;/*S表示操作系统占用*/

q->len=64;q->address=0;q->next=NULL;

busy_head->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’);

printlink();

past(t3);

requireMemo(‘E’,50);

printlink();

freeMemo(‘D’);

printlink();

}

5.源程序

#include

#include

#include

#include

usingnamespacestd;

structfreelink{

intlen,address;/*len为分区长度*/

/*address为分区起始地址*/

structfreelink*next;

};

structbusylink{

charname;/*作业或进程名name='S'表示OS占用*/

intlen,address;

structbusylink*next;

};

structfreelink*free_head=NULL;//自由链队列(带头结点)队首指针

structbusylink*busy_head=NULL;//占用区队列队(带头结点)首指针

structbusylink*busy_tail=NULL;//占用区队列队尾指针

voidstart(void)/*设置系统初始状态*/

{

structfreelink*p;

structbusylink*q;

free_head=(structfreelink*)malloc(sizeof(structfreelink));

free_head->next=NULL;//创建自由链头结点

busy_head=busy_tail=(structbusylink*)malloc(sizeof(structbusylink));

busy_head->next=NULL;//创建占用链头结点

//初始空闲链表

p=(structfreelink*)malloc(sizeof(structfreelink));

p->address=64;

p->len=640-64;//(OS占用了64K)

p->next=NULL;

free_head->next=p;

//初始被占用链表

q=(structbusylink*)malloc(sizeof(structbusylink));

q->name='S';/*S表示操作系统占用*/

q->len=64;

q->address=0;

q->next=NULL;

busy_head->next=q;

busy_tail=q;

}

voidrequireMemo(charname,intrequire)/*模拟内存分配*/

{

if(free_head->next->len

{

printf("Can'tallocate");

exit(0);

}

else

{

structbusylink*p;

structfreelink*w;

p=(structbusylink*)malloc(sizeof(structbusylink));

p->name=name;

p->address=free_head->next->address;

p->len=require;

p->next=NULL;

busy_tail->next=p;

busy_tail=p;

w=free_head->next;

free_head->next=w->next;

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->next;

while((v!

=NULL)&&v->len>w->len)

{

u=v;

v=v->next;

}

u->next=w;

w->next=v;

}

}

}

voidfreeMemo(charname)/*模拟内存回收*/

{

structbusylink*p,*q;

structfreelink*w,*u,*v;

intlen,address;

q=busy_head;

p=busy_head->next;

while(p!

=NULL&&(p->name!

=name))

{

q=p;

p=p->next;

}

if(p==NULL)

printf("%cisnotexist",name);

else

{

if(p==busy_tail)

busy_tail=q;

else

{

q->next=p->next;

}

len=p->len;

address=p->address;

free(p);

w=(structfreelink*)malloc(sizeof(structfreelink));

w->len=len;

w->address=address;

u=free_head;

v=free_head->next;

while(v!

=NULL&&v->len>w->len)

{

u=v;

v=v->next;

}

u->next=w;

w->next=v;

}

}

voidpast(inttime)/*模拟系统过了time时间*/

{

cout<<"经过时间"<

"<

}

voidprintlink()/*输出内存空闲情况(自由链的结点)*/

{

structfreelink*u,*v;

inti=1;

u=free_head;

v=u->next;

while(v!

=NULL)

{

cout<<"第"<

";

cout<<"address:

"<address<<""<<"len:

"<len<

v=v->next;

i++;

}

cout<

}

voidprintlink2()/*输出内存空闲情况(自由链的结点)*/

{

structbusylink*u,*v;

inti=1;

u=busy_head;

v=u->next;

while(v!

=NULL)

{

cout<<"第"<

";

cout<<"address:

"<address<<""<<"len:

"<len<

v=v->next;

i++;

}

cout<

}

voidmain(void)

{

start();

past

(1);

requireMemo('A',8);

requireMemo('B',16);

requireMemo('C',64);

requireMemo('D',124);

printlink();

printlink2();

past(3);

freeMemo('C');

printlink();

printlink2();

past(4);

requireMemo('E',50);

printlink();

printlink2();

past(5);

freeMemo('D');

printlink();

printlink2();

}

6.实例运行结果

7.实验总结

此次实验通过C语言程序动态地模拟了操作系统中内存的动态分区分配。

此次实验通过建立结点来模拟空闲分区和已占用分区,每个表示空闲分区或已占用分区的结点都包括有次空闲分区的大小和内存地址以及一个指向下一个空闲或占用分区的结构体指针,通过结点形成空

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1