先权是指在创建进程时所赋予的优先权,是可以随进程的推进或随其等待时间的增加而改变的,以便获得更好的调度性能。高优先权优先调度算法可以使紧迫型作业进入系统后能得到优先处理。此算法常被用于批处理系统,作为作业调度算法,也作为多种操作系统中的进程调度算法,还可用于实时系统。该算法用于作业调度时,系统将从后备队列中选择若干个优先权最高的作业装入内存。当用于进程调度时,该算法是把处理机分配给就绪队列中优先权最高的进程。
代码实现的基本思路:
1.在运行界面里输入进程名称,进程优先级和进程时间;
2.每运行一个时间单位,作业的优先权级数减一;
3.在运行出的用户界面中显示初始作业名,作业状态,优先权级数,需要服务的时间,已经运行的时间;
4.每次调度前后显示作业队列。
#include <stdio.h>
#include <stdlib.h>
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语句,可以控制进程的个数。
processes = pt = (struct PCB*)malloc(sizeof(struct PCB));
for (int i = 0; i != 3; ++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)
{
++cases;//输出每次在运行的进程是什么
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)
break;
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;
struct PCB* ap = NULL;
while (cp != NULL)
{
if (r->p_priority > cp->p_priority)
break;
else
{
ap = cp;
cp = cp->next;
}
}
if (ap == NULL)
{
r->next = SL;
SL = r;
}
else
{
r->next = cp;
ap->next = r;
}
r = t;
}
return SL;
}
(感觉代码有些还是有点缺陷的,这几天会好好改进,后期会完善这个代码的)