admin 管理员组文章数量: 1086019
2024年4月26日发(作者:美国枪击案)
动态高优先权算法实验报告
一、 实验目的
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。提高自己的动手
能力,主要是通过自己去思考并自己编码更进一步及更贴切的去理解弄明白动态优先权算
法的模拟加深对进程概念和进程调度过程的工作流程及其原理!
二、 实验要求
1.在运行界面里输入进程名称,进程优先级和进程时间;
2.每运行一个时间单位,作业的优先权级数减一;
3.在运行出的用户界面中显示初始作业名,作业状态,优先权级数,需要服务的时间,
已经运行的时间;
4.每次调度前后显示作业队列;
三、 实验内容
动态优先权是指在创建进程时所赋予的优先权,是可以随着进程的推进或随其等待时
间得增加而改变的。
实验内容利用C语言来实现对N个进程采用动态优先权优先算法的进程调度。优先数
1
改变的原则:进程每运行一个时间片,优先数减1。
四、 实验结果
登陆界面:
图1
2
图2
输入进程名字,进程优先数和进程时间:
3
图3
4
图4
图5
5
图6
6
图7
7
图8
五、 实验小结
8
本次实验代码和基于时间片轮转算法代码是一样的,是在别人代码的基础上,结合自
己对高优先权算法和时间片轮转算法的了解,通过switch语句把两者合二为一了,当输入
1的时候,执行HighPriority函数,也就是动态高优先权函数。在实现的过程中,使用for
语句,限制进程数为5个:for (int i = 0; i != 5; ++i),,定义pt作为临时节点来创建链表,
processes作为头结点来存储链表,psorted用来存放排序后的链表。
这次实验,在最初的时候,遇到了很大的麻烦,毕竟是改的别人代码,所以对代码理
解的不是很透彻,但在同学们和老师的帮助下,终于调试成功了。也使我更加明白了高优
先权调度的过程。
六、 附录
#include
#include
struct PCB{
char p_name[20];
int p_priority;
int p_needTime;
int p_runTime;
9
char p_state;
struct PCB* next;
};
void HighPriority();
void RoundRobin();
void Information();
char Choice();
struct PCB* SortList(PCB* HL);
int main()
{
Information();
char choice = Choice();
switch(choice)
10
{
case '1':
system("cls");
HighPriority();
break;
case '2':
system("cls");
RoundRobin();
break;
default:
break;
}
system("pause");
11
return 0;
}
void Information()
{
printf("nn");
printf("
n");
printf("
printf("
nnn");
printf("
printf("
printf("
printf("
*********************************************
模拟进程调度算法n");
*********************************************
班 级: 计科08-1班n");
姓 名: 卢彩方n");
学 号: 2n");
实验日期: 2011年05月17日nnnnnn");
12
printf(" 按回车键进入演示程序");
getchar();
system("cls");
}
char Choice()
{
printf("nn");
printf("
n");
printf("
printf("
nnn");
printf("
printf("
*********************************************
进程调度演示n");
*********************************************
1.演示最高优先数优先算法。n");
2.演示轮转法算法。n");
13
printf(" 3.退出程序。nnnn");
printf(" 选择进程调度方法:");
char ch = getchar();
return ch;
system("cls");
}
void HighPriority()
{
struct PCB *processes, *pt;
//pt作为临时节点来创建链表
processes = pt = (struct PCB*)malloc(sizeof(struct PCB));
for (int i = 0; i != 5; ++i)
{
14
struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
printf("进程号No.%d:n", i);
printf("输入进程名:");
scanf("%s", p->p_name);
printf("输入进程优先数:");
scanf("%d", &p->p_priority);
printf("输入进程运行时间:");
scanf("%d", &p->p_needTime);
p->p_runTime = 0;
p->p_state = 'W';
p->next = NULL;
pt->next = p;
pt = p;
15
printf("nn");
}
getchar(); //接受回车
//processes作为头结点来存储链表
processes = processes->next;
int cases = 0;
struct PCB *psorted = processes;
while (1)
{
++cases;
pt = processes;
//对链表按照优先数排序
//psorted用来存放排序后的链表
16
psorted = SortList(psorted);
printf("The execute number: %dnn", cases);
printf("**** 当前正在运行的进程是:%sn", psorted->p_name);
psorted->p_state = 'R';
printf("qname state super ndtime runtimen");
printf("%st%ct%dt%dt%dtnn", psorted->p_name, psorted->p_state,
psorted->p_priority, psorted->p_needTime, psorted->p_runTime);
pt->p_state = 'W';
psorted->p_runTime++;
psorted->p_priority--;
printf("**** 当前就绪状态的队列为:nn");
//pt指向已经排序的队列
pt = psorted->next;
while (pt != NULL)
17
{
printf("qname state super ndtime runtimen");
printf("%st%ct%dt%dt%dtnn",
pt->p_priority, pt->p_needTime, pt->p_runTime);
pt->p_name, pt->p_state,
pt = pt->next;
}
//pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的
pt = psorted;
struct PCB *ap;
ap = NULL; //ap指向pt的前一个节点
while (pt != NULL)
{
if (pt->p_needTime == pt->p_runTime)
{
18
if (ap == NULL)
{
pt = psorted->next;
psorted = pt;
}
else
ap->next = pt->next;
}
ap = pt;
pt = pt->next;
}
if (psorted->next == NULL)
break;
19
getchar();
}
}
struct PCB* SortList(PCB* HL)
{
struct PCB* SL;
SL = (struct PCB*)malloc(sizeof(struct PCB));
SL = NULL;
struct PCB* r = HL;
while (r != NULL)
{
struct PCB* t = r->next;
struct PCB* cp = SL;
20
struct PCB* ap = NULL;
while (cp != NULL)
{
if (r->p_priority > cp->p_priority)
break;
else
{
ap = cp;
cp = cp->next;
}
}
if (ap == NULL)
{
21
r->next = SL;
SL = r;
}
else
{
r->next = cp;
ap->next = r;
}
r = t;
}
return SL;
}
//轮转算法
22
void RoundRobin()
{
struct PCB *processes, *pt;
//pt作为临时节点来创建链表
processes = pt = (struct PCB*)malloc(sizeof(struct PCB));
for (int i = 0; i != 5; ++i)
{
struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
printf("进程号No.%d:n", i);
printf("输入进程名:");
scanf("%s", p->p_name);
printf("输入进程运行时间:");
scanf("%d", &p->p_needTime);
23
p->p_runTime = 0;
p->p_state = 'W';
p->next = NULL;
pt->next = p;
pt = p;
printf("nn");
}
getchar(); //接受回车
//processes作为头结点来存储链表
processes = processes->next;
int cases = 0;
while (1)
{
24
++cases;
pt = processes;
printf("The execute number: %dnn", cases);
printf("**** 当前正在运行的进程是:%sn", pt->p_name);
pt->p_state = 'R';
printf("qname state super ndtime runtimen");
printf("%st%ct%dt%dt%dtnn",
pt->p_priority, pt->p_needTime, pt->p_runTime);
pt->p_name, pt->p_state,
pt->p_state = 'W';
pt->p_runTime++;
pt->p_priority--;
printf("**** 当前就绪状态的队列为:nn");
pt = pt->next;
while (pt != NULL)
25
{
printf("qname state super ndtime runtimen");
printf("%st%ct%dt%dt%dtnn",
pt->p_priority, pt->p_needTime, pt->p_runTime);
pt->p_name, pt->p_state,
pt = pt->next;
}
//检测是否运行时间等于需要时间,是的话从队列里面删除,不是的话加到队列
最尾部
pt = processes;
if (pt->p_needTime == pt->p_runTime)
{
pt->p_state = 'C';
pt = processes->next;
processes = pt;
26
}
else
{
if (pt ->next != NULL)
{
//寻找最后一个节点
while (pt->next != NULL)
pt = pt->next;
struct PCB* ptem;//临时节点用来帮助把头结点插到尾部
ptem = processes->next;
pt->next = processes;
processes->next = NULL;
processes = ptem;
27
}
}
pt = processes;
if (pt == NULL)
break;
getchar();
}
}
28
版权声明:本文标题:高优先权优先调度算法 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1714141836a667148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论