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() {
3
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\ printf(\"输入进程名:\"); scanf(\"%s\ printf(\"输入进程优先数:\"); scanf(\"%d\ printf(\"输入进程运行时间:\"); scanf(\"%d\ 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) {
4
++cases;
pt = processes;
//对链表按照优先数排序
//psorted用来存放排序后的链表 psorted = SortList(psorted);
printf(\"The execute number: %d\\n\\n\
printf(\"**** 当前正在运行的进程是:%s\\n\ psorted->p_state = 'R';
printf(\"qname state super ndtime runtime\\n\"); printf(\"%s\%c\%d\%d\%d\\\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\%c\%d\%d\%d\\\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)
5
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; }
6
4 系统测试与分析
经过测试运行正常,可以达到预期的输出结果。
4.1程序运行开始界面见图2和图3
图2
图3 7
4.2高优先权程序正常运行
输入进程名输入5个进程名、优先数、运行时间,然后程序自动从就绪队列中选择优先级高的进程运行优先执行,一直到所有进程全部运行完毕。具体的见图4—图12。
8
图4
图6 图5 9
图8 图7 10
图9
图10 11
图11
12
图12 教师评分表
评分细则 分数 理解功能及基本原理程度。(20分) 报告文字、图、表格式规范、整齐程度。(15分) 报告内容完整、逻辑性程度。(15分) 程序代码是否有注释、是否有语法错误。(10分) 程序运行是否正常。(20分) 程序运行界面是否美观。(15分) 是否有创新思考、做法等。(5分) 其他说明: 总分: 成绩: 指导老师签名: 日期:
13