操作系统实验报告.docx

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

操作系统实验报告.docx

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

操作系统实验报告.docx

操作系统实验报告

实验二进程调度

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时间(需运行的时间片个数)

}

用PCB来模拟进程;

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

队列类型描述如下:

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();

模拟调度,当就绪队列的队首进程运行一个时间片后,放到就绪队列末尾,每次都是队首进程进行调度,一个进程运行结束就从就绪队列中删除,当到t个时间片后,唤醒阻塞队列队首进程。

calculate();

就绪进程运行一次,usecpu加1,当就绪队列为空时unusecpu加1,CPU利用率为use_cpu/(use_cpu+unuse_cpu)。

5源代码:

#include

#include

structPCB_type

{

intpid;//进程名

intstate;//进程状态

//2--表示"执行"状态

//1--表示"就绪"状态

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

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

};

structQueueNode{

structPCB_typePCB;

structQueueNode*next;

};

structQueueNode*ready_head=NULL,//ready队列队首指针

*ready_tail=NULL,//ready队列队尾指针

*block_head=NULL,//blocked队列队首指针

*block_tail=NULL;//blocked队列队尾指针

intuse_cpu,unuse_cpu;

voidstart_state()//读入假设的数据,设置系统初始状态

{

intn,m;

inti;

structQueueNode*p,*q;

printf("输入就绪节点个数n:

");

scanf("%d",&n);

printf("输入阻塞节点个数m:

");

scanf("%d",&m);

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

p->next=NULL;

ready_head=ready_tail=p;

for(i=0;i

{

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

p->next=NULL;

p->PCB.state=1;

printf("输入就绪进程%d的pid和cpu_time:

",i+1);

scanf("%d%d",&p->PCB.pid,&p->PCB.cpu_time);

ready_tail->next=p;

ready_tail=p;

}

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

q->next=NULL;

block_head=block_tail=q;

for(i=0;i

{

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

q->next=NULL;

q->PCB.state=0;

printf("输入阻塞进程%d的pid和cpu_time:

",i+1);

scanf("%d%d",&q->PCB.pid,&q->PCB.cpu_time);

block_tail->next=q;

block_tail=q;

}

printf("\n处于就绪状态的进程有:

\n");

p=ready_head->next;

i=1;

while(p)

{printf(“进程%d的pid和state和cpu_time:

%5d%5d%5d\n",i,p->PCB.pid,p->PCB.state,p->PCB.cpu_time);

p=p->next;

i++;

}

}

voiddispath()//模拟调度

{

intx=0,t;

use_cpu=0;

unuse_cpu=0;

printf("输入t:

");

scanf("%d",&t);

printf("开始调度\n");

while(ready_head!

=ready_tail||block_head!

=block_tail)

{

structQueueNode*p,*q;

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;

printf("进程%d调度\t",p->PCB.pid);

use_cpu++;

x++;

p->PCB.cpu_time--;

if(p->PCB.cpu_time)

{

ready_tail->next=p;

ready_tail=p;

}

else

{

printf("进程%d完成\t",p->PCB.pid);

free(p);

}

}

else

{

unuse_cpu++;

x++;

printf("空闲一个时间片\t");

}

if(x==t&&block_head!

=block_tail)

{

q=block_head->next;

block_head->next=q->next;

q->next=NULL;

if(block_head->next==NULL)

{

block_tail=block_head;

}

ready_tail->next=q;

ready_tail=q;

x=0;

}

}

}

voidcalculate()//计算CPU利用率

{

printf("\ncpu的利用率%\n",(float)use_cpu/(use_cpu+unuse_cpu));

}

voidmain()

{start_state();

dispath();

calculate();

}

6运行结果:

7实验总结:

实验帮我复习了数据结构和C语言,且巩固课本知识,知道了如何定义结构体,如何在链接队列中增删节点。

模拟进程调度帮我们巩固了进程三状态之间的变迁。

懂得调式的重要性。

总之,我们xx了理论联系实际。

多看书,多上机。

实验三可变分区存储管理

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.设计思想

模拟内存分配和回收,要设置两个链队列,一个空闲区链和一个占用区链,空闲区链节点有起始地址,大小和指向下一节点的指针等数据域,占用区链节点有起始地址,大小,作业名和指向下一节点的指针等数据域,本实验用最坏适应算法,每次作业申请内存都是从空闲链队头节点分配,如果相等,就删除空闲头结点,如果小于申请的,就不分配,否则就划分内存给作业,剩下的内存大小,重新插入空闲链队,按从大到小,接着把作业占用的内存放到占用区链节点的末尾。

每次作业运行完,就要回收其占用的内存大小,把作业节点按从大到小插入到空闲链队中。

5.源代码:

#include

#include

structfreelinkNode{

intlen;

intaddress;

structfreelinkNode*next;

};

structbusylinkNode{

charname;

intlen;

intaddress;

structbusylinkNode*next;

};

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

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

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

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

{

structfreelinkNode*p;

structbusylinkNode*q;

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

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

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

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

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

p->address=64;

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

p->next=NULL;

free_head->next=p;

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

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

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

busy_head->next=q;busy_tail=q;

}

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

{

freelinkNode*w,*u,*v;

busylinkNode*p;

if(free_head->next->len>=require)

{

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

p->name=name;

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

p->len=require;

p->next=NULL;

busy_tail->next=p;

busy_tail=p;

}

else

printf("Can'tallocate");

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;

}

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)/*模拟内存回收*/

{

intlen;

intaddress;

busylinkNode*q,*p;

freelinkNode*w,*u,*v;

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=(structfreelinkNode*)malloc(sizeof(structfreelinkNode));

w->len=len;

w->address=address;

u=free_head;

v=free_head->next;

while((v!

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

{u=v;

v=v->next;

}

u->next=w;

w->next=v;

}

}

}

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

{

printf("过了时间%d后:

\n",time);

}

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

{

freelinkNode*p;

printf("内存的空闲情况为:

\n");

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

p=free_head->next;

while(p!

=NULL)

{printf("内存的起始地址和内存的大小%5d\t%5d:

\n",p->address,p->len);

p=p->next;

}

}

voidmain()

{

intt1=1,t2=2,t3=3,t4=4;

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();

past(t4);

freeMemo('D');

printlink();

}

6.运行结果:

7.实验总结:

巩固编程能力,和调式能力,复习课本知识,xx理论联系实际的重要性,动手能力非常重要,多看书,多独立思考,品味痛苦的过程,享受成功的喜悦。

操作系统实验报告

 

院系:

数计学院

xx级:

大类6xx

学号:

100511624

姓名:

xx

指导教师:

xx

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

当前位置:首页 > 求职职场 > 简历

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

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