当前位置:文档之家› 处理器调度之动态优先数调度算法

处理器调度之动态优先数调度算法

1 处理机调度1.1 实验内容及要求实验内容:按优先数调度算法实现处理器调度。

实验要求:能接受键盘输入的进程数、进程标识、进程优先数及要求运行时间,能显示每次进程调度的情况:运行进程、就绪进程和就绪进程的排列情况。

1.2 实验目的本实验模拟在单处理器环境下的处理器调度,加深了解处理器调度工作。

1.3 实验环境本实验的设计基于Windows7操作系统DevC++5.11环境,用C语言实现编程。

1.4 实验思路(1) 每个进程用一个PCB来代表。

PCB的结构为:进程名——作为进程标识。

优先数——赋予进程的优先数,调度时总是选取优先数大的进程先执行。

要求运行时间——假设进程需要运行的单位时间数。

状态——假设两种状态:就绪和结束,用R表示就绪,用E表示结束。

初始状态都为就绪状态。

指针——按优先数的大小把5个进程连成队列,用指针指出下一个进程PCB的首地址。

(2) 开始运行之前,为每个进程确定它的“优先数”和“要求运行时间”。

通过键盘输入这些参数。

(3) 处理器总是选择队首进程运行。

采用动态改变优先数的办法,进程每运行1次,优先数减1,要求运行时间减1。

(4) 进程运行一次后,若要求运行时间不等于0,则将它加入就绪队列,否则,将状态改为“结束”,退出就绪队列。

(5) 若就绪队列为空,结束,否则转到(3)重复。

1.5 数据结构与全局变量typedef struct pcb{int pname;//进程名int priority;//优先级int runTime;//所需时间int state;//状态struct pcb* next;//下一个进程控制块}PCB; //进程控制块int num;//存储进程数PCB readyHead;//头结点,不存储进程PCB *readyEnd;//指向尾结点的指针1.6 函数说明(1)主函数main()输入进程数并调createProcess()初始化进程;若有进程,则依次调用sortProcess()、runProcess()、printProcessLink()和printProcessInfo()。

(2)进程创建函数createProcess()输入进程标识、优先级和运行时间进行初始化。

(3)进程排序函数sortProcess()用冒泡排序算法根据进程的优先级进行降序排序,每次排序之后优先级最高的进程放在就绪队列首。

(4)进程运行函数runProcess()将数组首的进程优先级和所需时间减1;若剩余所需时间为0,则PCB状态标记为E(结束)。

(5)输出函数printProcessLink()和printProcessInfo()printProcessLink()输出进程链,printProcessInfo()输出进程控制块详细信息。

1.7 运行结果表1.1对表1.1数据的运行结果:1.8 实验心得指针操作是真的烦。

1.9 实验代码#include <stdlib.h>#include <string.h>#include <stdio.h>#include <conio.h>#define N 32void createProcess();void sortProcess();void printProcessLink();void printProcessInfo();void runProcess();typedef struct pcb{char pname[N];//进程名int priority;//进程优先级int runTime;//进程还需运行的时间片char state;//R代表ready,E代表end struct pcb* next;}PCB;int num; //进程数PCB readyHead;//头结点,不存储进程PCB *readyEnd; //尾结点指针int main(){printf("********************************************************\n");printf("*********实习1-1 按优先级调度算法实现处理器调度*********\n");printf("********************************************************\n");createProcess();if(num==1){printProcessInfo();printf("%s starts\n", readyHead.next->pname);printf("%s is running...\n", readyHead.next->pname);printf("%s ends after %d slice(s).", readyHead.next->pname, readyHead.next->runTime);}elsewhile(readyHead.next!=NULL){sortProcess();printProcessInfo();printProcessLink();runProcess();}return 0;}void createProcess(){PCB *p, *prior;printf("How many process do you want to run:");scanf("%d", &num);while(num<=0){printf("Number is invalid. Input again:\n");scanf("%d", &num);}p = (PCB*)malloc(sizeof(PCB));prior = &readyHead;prior->next=p;for (int i = 0; i < num; i++){printf("Input NO.%2d process name:", i+1);scanf("%s", p->pname);printf(" priority:");scanf("%d", &(p->priority));printf(" runTime:");scanf("%d", &(p->runTime));p->state = 'R';p->next = (PCB*)malloc(sizeof(PCB));prior=p;p=p->next;}free(p);p = NULL;prior->next=NULL;readyEnd=prior;printf("\n");}void sortProcess(){char name[N];int i,j, priorityNum, timeNum;PCB *p, *rear;for(p=readyHead.next; p!=NULL; p=p->next)for(rear=p->next; rear!=NULL; rear=rear->next)if(p->priority<rear->priority){strcpy(name, p->pname);priorityNum=p->priority;timeNum=p->runTime;strcpy(p->pname, rear->pname);p->priority=rear->priority;p->runTime=rear->runTime;strcpy(rear->pname, name);rear->priority=priorityNum;rear->runTime=timeNum;}}void printProcessLink(){PCB *p=readyHead.next;printf("process link: \n");while(p!=NULL){printf("%s",p->pname);p=p->next;if(p!=NULL) printf("->");}printf("\n");}void printProcessInfo(){PCB *p=readyHead.next;printf("process information before running:\n");printf("=================================================\n");printf("NAME PRIORITY RUNTIME STATUS NEXT\n");printf("=================================================\n");while(p!=NULL){printf("%-16s %-8d %-8d %-8s %s\n",p->pname, p->priority, p->runTime, (p->state=='R')?"ready":"end", p->next->pname);p=p->next;}}void runProcess(){PCB *p=readyHead.next;printf("process run:\n");printf("%s\n",p->pname);p->priority--;p->runTime--;readyHead.next=p->next;if(p->runTime==0){p->state='E';printf("%s is terminated\n", p->pname);free(p);}else{readyEnd->next=p;p->next=NULL;readyEnd=p;}printf("\n");}。

相关主题