1、实验报告材料一进程调度算法江西师范大学计算机信息工程学院学生实验报告专业 12级物联网班 姓名 严超 学号 1208067042 日期 2014/05/8 课程名称操作系统教程实验室名称W4313实验名称进程调度算法指导教师张练兴成绩1、实验目的用代码实现模拟操作系统的进程调度,以加深对进程的概念及进程调度算法的理解2、实验原理和内容(1)先来先服务(FCFS)调度算法:从“就绪队列”中选择一个最先进入队列的进程,为它分配处理器,使之开始运行。(2)优先数调度算法:根据进程的情况或要求赋予进程一个优先级,进程运行过程中优先级不再改变。每次调度时,就绪队列中优先级最高的进程被率先调度,同级的采用
2、先来先服务(FCFS)。3、实验步骤进程调度算法:(1)编写进程控制块数据结构(2)统一按照FCFS调度算法创建队列(3)在FCFS调度算法中,将就绪队列队首进程调入执行,如果在队列中存在到达时间小于等于当前时间的结点,将该结点的状态设为就绪状态。如果当前进程执行完了,就将其状态改为完成状态,并将其插入到队尾。(4)在优先级调度算法中,将就绪队列队首进程调入执行,如果在队列中存在到达时间小于等于当前时间的结点,将该结点的状态设为就绪状态,并对队列中的结点按优先级数的大小进行排序(队首除外)。如果当前进程执行完了,就将其状态改为完成状态,并将其插入到队尾。(5)输出运行后的结果,如周转时间和带权
3、周转时间。4、程序及运行结果(或实验数据记录及分析)进程调度算法: 本次实验让我更加明白进程调度的概念,更加了解进程调度的工作原理。在前期,我是直接将结果显示出来,后来,我又在原有的基础上加了显示每一时刻队列的信息。在编写此代码过程中遇到很多问题,例如指针问题,指针指来指去,总是指错地址。具体代码:#include#include#define MAX 1000typedef struct progress int ID; /进程名 char state; /进程状态 int super; /优先数 int arrive_time; /到达时间 int serve_time; /服务时间 st
4、ruct progress *next;node;node* sortFCFS(node* head,node* q) node *p,*pre; int done=0; if(head=NULL)|(q-arrive_time)arrive_time) /*到达时间最先者,插入队首*/ q-next=head; head=q; else /* 进程比较到达时间,插入适当的位置中*/ p=head; pre=p-next; while(pre!=NULL) if(q-arrive_time)arrive_time) /*若插入进程比当前进程到达时间小,*/ /*插入到当前进程前面*/ q-ne
5、xt=pre; p-next=q; pre=NULL; done=1; else p=p-next; pre=pre-next; if(done=0) p-next=q; return head;/*函数功能:创建单链表参数:空返回值:指向节点的指针head */node* create() node *head; node *p,*q; int x,count=0; head=NULL; printf(ntt请输入进程名【输入-1结束】:); while(scanf(%d,&x)!=EOF & x!= -1) p=(node *)malloc(sizeof(node); printf(tt请
6、输入优先级数【优先数高者优先】:); scanf( %d,&p-super); printf(tt请输入到达时间【到达时间不得小于0】:); scanf( %d,&p-arrive_time); printf(tt请输入服务时间【服务时间必须大于0】:); scanf( %d,&p-serve_time); p-ID=x; p-state=w; p-next=NULL; head=sortFCFS(head,p); printf(ntt请输入进程名(输入-1结束):); return head;/*函数功能:输出单链表参数:指向节点的指针head返回值:空*/void print(node *
7、head) node *p; printf(nt|-结点信息情况-|); printf(ntt| 进程名 |优先级数|到达时间|服务时间| 状态 |); p=head; while(p) printf(ntt|%8d|%8d|%8d|%8d|%8c|,p-ID,p-super,p-arrive_time,p-serve_time,p-state); p=p-next; printf(nt|-结点信息情况-|n);/*函数功能:利用先来先服务调度算法 参数:指向节点的指针head返回值:空存在问题: */void FCFS(node* head) int start_time,finish_ti
8、me=0,round_time,all_time=0; int done=1; int clock=0; float right_round_time; node* p,*q,*flag; flag=p=head; clock=p-arrive_time; all_time=start_time=head-arrive_time; while(p) all_time+=p-serve_time; p=p-next; p=head; while(done) done=0; printf(nnt|-第%2d 时刻-|,clock); while(p) if(p-arrive_timestate=w
9、|p-state=r) p-state=r;done=1; p=p-next; while(flag-next)flag=flag-next; print(head); printf(t|-第%2d 时刻-|nn,clock); if(clock=all_time)break; clock+; finish_time=start_time+head-serve_time; if(finish_time=clock) head-state=f; flag-next=head; head=head-next; flag=flag-next; flag-next=NULL; start_time=f
10、inish_time; p=head; p=head; finish_time=p-arrive_time; printf(t|-FCFS调度算法-|); printf(nt| 进程名 |到达时间|服务时间|开始时间|完成时间|周转时间|带权周转|); while(p) if(p-arrive_timearrive_time; finish_time=start_time+p-serve_time; round_time=finish_time-p-arrive_time; right_round_time=(float)round_time/p-serve_time; printf(nt|%
11、8d|%8d|%8d|%8d|%8d|%8d|%8.2f|,p-ID, p-arrive_time,p-serve_time,start_time,finish_time,round_time,right_round_time); p=p-next; printf(nt|-FCFS调度算法-|); printf(n); void SortBySuper(node *head) node *p1,*q1,*temp; temp=(node*)malloc(sizeof(node); for(p1=head-next;p1!=NULL&p1-state=r;p1=p1-next) for(q1=p
12、1-next;q1!=NULL&q1-state=r;q1=q1-next) if(p1-supersuper) temp-ID=p1-ID; p1-ID=q1-ID; q1-ID=temp-ID; temp-super=p1-super; p1-super=q1-super; q1-super=temp-super; temp-arrive_time=p1-arrive_time; p1-arrive_time=q1-arrive_time; q1-arrive_time=temp-arrive_time; temp-serve_time=p1-serve_time; p1-serve_ti
13、me=q1-serve_time; q1-serve_time=temp-serve_time; temp-state=p1-state; p1-state=q1-state; q1-state=temp-state; /*函数功能:利用优先数调度算法 参数:指向节点的指针head返回值:空存在问题:*/void priority(node* head) int start_time,finish_time=0,round_time,all_time=0; int done=1; int clock=0; float right_round_time; node* p,*q,*flag; fl
14、ag=p=head; clock=p-arrive_time; all_time=start_time=head-arrive_time; while(p) all_time+=p-serve_time; p=p-next; p=head; while(done) done=0; printf(nnt|-第%2d 时刻-|,clock); while(p) if(p-arrive_timestate=w|p-state=r) p-state=r;done=1; p=p-next; SortBySuper(head);while(flag-next)flag=flag-next; print(h
15、ead); printf(t|-第%2d 时刻-|nn,clock); if(clock=all_time)break; clock+; finish_time=start_time+head-serve_time; if(finish_time=clock) head-state=f; flag-next=head; head=head-next; flag=flag-next; flag-next=NULL; start_time=finish_time; p=head; p=head; finish_time=p-arrive_time; printf(nt|-优先数调度算法-|); p
16、rintf(nt| 进程名 |到达时间|服务时间|开始时间|完成时间|周转时间|带权周转|); while(p) if(p-arrive_timearrive_time; finish_time=start_time+p-serve_time; round_time=finish_time-p-arrive_time; right_round_time=(float)round_time/p-serve_time; printf(nt|%8d|%8d|%8d|%8d|%8d|%8d|%8.2f|,p-ID, p-arrive_time,p-serve_time,start_time,finis
17、h_time,round_time,right_round_time); p=p-next; printf(nt|-优先数调度算法-|); printf(n);int main() char choice; node *head; do printf(ntt|-MUNE-|n); printf(tt|-1.FCFS调度算法-|n); printf(tt|-2.优先数调度算法-|n); printf(ttt你的选择是:bb); while(scanf( %c,&choice)!=EOF&choice!=1&choice!=2); head=create(); switch(choice) case 1:print(head); FCFS(head);break; case 2:print(head); priority(head);break; 运行结果截图:先来先服务调度算法运行结果优先级调度算法运行结果
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1