}
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<<"经过时间"<