河南工业大学实验报告课程计算机操作系统实验名称高(动态)优先权优先的进程调度算法模拟系别信息科学与工程学院计算机科学系专业班级实验日期 2012-10-26姓名学号教师审批签字1.实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
2.实验环境装有操作系统Windows XP和开发工具VC++6.0,内存在256M以上的微机;或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。
3.实验内容(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:●进程标识数ID;●进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;●进程已占用的CPU时间CPUTIME;●进程还需占用的CPU时间NEEDTIME。
当进程运行完毕时,NEEDTIME变为0;●进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;●进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;●进程状态STATE;(READY, RUNNING, BLOCK, FINISH)●队列指针NEXT,用来将PCB排成队列。
(3)优先数改变的原则:●进程在就绪队列中呆一个时间片,优先数增加1;●进程每运行一个时间片,优先数减3。
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:ID 0 1 2 3 4PRIORITY 9 38 30 29 0CPUTIME 0 0 0 0 0NEEDTIME 3 3 6 3 4STARTBLOCK 2 -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE READY READY READY READY READY(5)为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:RUNNING PROCESS: $id0READY QUEUE: $id1->$id2BLOCK QUEUE: $id3->$id4FINISH QUEUE: $id0->$id1->$id2->$id3->$id4==================================================================ID PRIORITY CPUTIME NEEDTIME STATE STARTBLOCK BLOCKTIME0 XX XX XX XX XX XX1 XX XX XX XX XX XX2 XX XX XX XX XX XX3 XX XX XX XX XX XX4 XX XX XX XX XX XX==================================================================== 4.实验要求(1)将源程序(priority.c)和程序运行结果写入实验报告。
(2)将该算法执行过程与高响应比优先调度算法的执行过程进行比较。
5.实验过程及结果(1).代码:#include <stdio.h>#include <string.h>#include <stdlib.h>char *State[] = {"READY", "RUNNING", "BLOCK", "FINISH"};typedef struct PCB{i nt id;i nt priority;i nt cputime;i nt needtime;i nt startblock;i nt blocktime;c har *state;s truct PCB *next;s truct PCB *nt;}PCB;PCB *Ready, *Block, *Finish, *Runing, *ALL;PCB *findHighest();void insert(PCB *node, PCB *insert, int i);void ModifyPriority();void ModifyBlock();void BlockToReady();void Print();void FreeAllPCB();void init(){P CB *pro = (PCB *)malloc(sizeof(PCB));R eady->next = ALL = pro;p ro->id = 0;p ro->priority = 9;p ro->cputime = 0;p ro->needtime = 3;p ro->startblock = 2;p ro->blocktime = 3;p ro = pro->next = pro->nt = (PCB *)malloc(sizeof(PCB));p ro->id = 1;p ro->priority = 38;p ro->cputime = 0;p ro->needtime = 3;p ro->startblock = -1;p ro->blocktime = 0;p ro = pro->next = pro->nt = (PCB *)malloc(sizeof(PCB));p ro->id = 2;p ro->priority = 30;p ro->cputime = 0;p ro->needtime = 6;p ro->startblock = -1;p ro->blocktime = 0;pro = pro->next = pro->nt = (PCB *)malloc(sizeof(PCB)); p ro->id = 3;p ro->priority = 29;p ro->cputime = 0;p ro->needtime = 3;p ro->startblock = -1;p ro->blocktime = 0;p ro = pro->next = pro->nt = (PCB *)malloc(sizeof(PCB));p ro->id = 4;p ro->priority = 0;p ro->cputime = 0;p ro->needtime = 4;p ro->startblock = -1;p ro->blocktime = 0;p ro->next = pro->nt = NULL;}PCB *findHighest(){P CB *pro, *highest;P CB *propre = Ready;P CB *highpre = Ready;h ighest = pro = Ready->next;w hile(pro != NULL){if(pro->priority > highest->priority){highest = pro;highpre = propre;}propre = propre->next;pro = pro->next;}h ighpre->next = highest->next;h ighest->next = NULL;r eturn highest;}void insert(PCB *node,PCB *insert,int i) {n ode->state = State[i];n ode->next = insert->next;i nsert->next = node;}void ModifyPriority(){P CB *p;p = Ready->next;w hile(p !=NULL){p->priority++;p = p->next;}}void ModifyBlock(){P CB *p;p = Block->next;w hile(p !=NULL){p->blocktime--;p = p->next;}}void BlockToReady(){P CB *p, *pre, *node;p re = Block;p = pre->next;w hile(p != NULL){if(p->blocktime == 0){p->startblock--;node = p;p = pre->next = node->next;node->next = NULL;insert(node, Ready,0);}else{pre = pre->next;p = p->next;}}}void Print(){P CB *pro, *All;i f(Runing != NULL)printf("RUNNING PROCESS: $id%d\n", Runing->id);p rintf("READY QUEUE: ");p ro = Ready->next;w hile(pro != NULL){printf("->$id%d", pro->id);pro = pro->next;}p uts("");p rintf("BLOCK QUEUE: ");p ro = Block->next;w hile(pro != NULL){printf("->$id%d", pro->id);pro = pro->next;}p uts("");p rintf("FINISH QUEUE: ");p ro = Finish->next;w hile(pro != NULL){printf("->$id%d", pro->id);pro = pro->next;}p uts("");p rintf("===============================================================\n");p rintf("ID PRIORITY CPUTIME NEEDTIME STATE STARTBLOCK BLOCKTIME\n");A ll = ALL;w hile(All != NULL){printf("%d%10d%10d%10d%8s%10d%11d\n",All->id, All->priority, All->cputime, All->needtime, All->state, All->startblock, All->blocktime);All = All->nt;}}void FreeAllPCB(){f ree(Finish);}int main(void){P CB *pro;R eady = (PCB *)malloc(sizeof(PCB));B lock = (PCB *)malloc(sizeof(PCB));F inish = (PCB *)malloc(sizeof(PCB));R eady->next = NULL;B lock->next = NULL;F inish->next = NULL;i nit();w hile(Ready->next != NULL){pro = findHighest();Runing = pro;pro->state = State[1];pro->cputime++;pro->needtime--;if(pro->startblock > 0){pro->startblock--;}pro->priority -= 3;Print();ModifyBlock();ModifyPriority();BlockToReady();if(pro->needtime == 0){insert(pro, Finish, 3);Runing=NULL;}else{if(pro->startblock == 0){insert(pro, Block, 2);}else{insert(pro, Ready, 0);}Runing=NULL;}}P rint();F reeAllPCB();r eturn 0;}程序运行结果(2).试验中的问题将该算法执行过程与高响应比优先调度算法的执行过程进行比较。