操作系统原理高优先权调度算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《操作系统原理》

课程设计报告书

题目:高优先权调度算法

学号:

学生姓名:

专业:

指导教师:

5月30日

目录

1 功能描述 (1)

2 系统设计 (1)

2.1总体设计 (1)

2.2详细设计 (1)

2.3程序运行流程图 (1)

3 系统实现 (2)

3.1程序代码 (3)

4 系统测试与分析 (7)

4.1程序运行开始界面见图2和图3 (7)

4.2高优先权程序正常运行 (8)

教师评分表 (13)

1 功能描述

先权是指在创建进程时所赋予的优先权,是可以随进程的推进或随其等待时间的增加而改变的,以便获得更好的调度性能。高优先权优先调度算法可以使紧迫型作业进入系统后能得到优先处理。此算法常被用于批处理系统,作为作业调度算法,也作为多种操作系统中的进程调度算法,还可用于实时系统。该算法用于作业调度时,系统将从后备队列中选择若干个优先权最高的作业装入内存。当用于进程调度时,该算法是把处理机分配给就绪队列中优先权最高的进程。

2 系统设计

2.1总体设计

验内容利用C语言来实现对N个进程采用动态优先权优先算法的进程调度。优先数改变的原则:进程每运行一个时间片,优先数减1。

2.2详细设计

1.在运行界面里输入进程名称,进程优先级和进程时间;

2.每运行一个时间单位,作业的优先权级数减一;

3.在运行出的用户界面中显示初始作业名,作业状态,优先权级数,需要服务的时间,已经运行的时间;

4.每次调度前后显示作业队列;

2.3程序运行流程图

程序运行流程图见流程图1

3 系统实现

用c++编写的高优先权调度算法算法。

3.1程序代码

程序源代码如下:

#include

#include

struct PCB{

char p_name[20];

int p_priority;

int p_needTime;

int p_runTime;

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)

{

case '1':

system("cls");

HighPriority();

break;

default:

break;

}

system("pause");

return 0;

}

void Information()

{

printf(" 按回车键进入演示程序");

getchar();

system("cls");

}

char Choice()

{

printf("\n\n");

printf(" 1.演示最高优先数优先算法。");

printf(" 按1继续:");

char ch = getchar();

return ch;

system("cls");

}

void HighPriority()

{

struct PCB *processes, *pt;

//pt作为临时节点来创建链表,使用for语句,限制进程数为5个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_priority);

printf("输入进程运行时间:");

scanf("%d", &p->p_needTime);

p->p_runTime = 0;

p->p_state = 'W';

p->next = NULL;

pt->next = p;

pt = p;

printf("\n\n");

}

getchar(); //接受回车

//processes作为头结点来存储链表

processes = processes->next;

int cases = 0;

struct PCB *psorted = processes;

while (1)

{

pt = processes;

//对链表按照优先数排序

//psorted用来存放排序后的链表

psorted = SortList(psorted);

printf("The execute number: %d\n\n", cases);

printf("**** 当前正在运行的进程是:%s\n", psorted->p_name);

psorted->p_state = 'R';

printf("qname state super ndtime runtime\n");

printf("%s\t%c\t%d\t%d\t%d\t\n\n", 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("**** 当前就绪状态的队列为:\n\n");

//pt指向已经排序的队列

pt = psorted->next;

while (pt != NULL)

{

printf("qname state super ndtime runtime\n");

printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);

pt = pt->next;

}

//pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的

pt = psorted;

struct PCB *ap;

ap = NULL; //ap指向pt的前一个节点

while (pt != NULL)

{

if (pt->p_needTime == pt->p_runTime)

{

if (ap == NULL)

{

pt = psorted->next;

psorted = pt;

}

else

ap->next = pt->next;

}

ap = pt;

pt = pt->next;

}

if (psorted->next == NULL)

相关文档
最新文档