1、4、程序及运行结果(或实验数据记录及分析) 本次实验让我更加明白进程调度的概念,更加了解进程调度的工作原理。在前期,我是直接将结果显示出来,后来,我又在原有的基础上加了显示每一时刻队列的信息。在编写此代码过程中遇到很多问题,例如指针问题,指针指来指去,总是指错地址。具体代码:#includestdlib.h#define MAX 1000typedef struct progress int ID; /进程名 char state; /进程状态 int super; /优先数 int arrive_time; /到达时间 int serve_time; /服务时间 struct progres
2、s *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-(pre-arrive_time) /*若插入进程比当前进程到达时间小,*/ /*插入到当前进程前面*/ q-next=pre; p-next=q; p
3、re=NULL; done=1; else p=p- pre=pre- if(done=0) p- 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请输入优先级数【优先数高者优先】: scanf( %dp-super);t
4、t请输入到达时间【到达时间不得小于0】:arrive_time);tt请输入服务时间【服务时间必须大于0】:serve_time); p-ID=x;state=w;next=NULL; head=sortFCFS(head,p); ntt请输入进程名(输入-1结束): 输出单链表指向节点的指针headvoid print(node *head) node *p;nt|-结点信息情况-|ntt| 进程名 |优先级数|到达时间|服务时间| 状态 | p=head; while(p)ntt|%8d|%8d|%8d|%8d|%8c|,p-ID,p-super,p-arrive_time,p-serve
5、_time,p-state); p=p-nt|-结点信息情况-|n利用先来先服务调度算法 存在问题:void FCFS(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; flag=p=head; clock=p-arrive_time; all_time=start_time=head- all_time+=p-serve_time; while(done) done=0;nnt|-第
6、%2d 时刻-|,clock); while(p) if(p-arrive_timer) done=1; p=p- while(flag-next)flag=flag- print(head);t|-第%2d 时刻-|nn if(clock=all_time)break; clock+; finish_time=start_time+head- if(finish_time=clock) head-f flag- head=head- flag=flag- start_time=finish_time; p=head; finish_time=p-t|-FCFS调度算法-|nt| 进程名 |到
7、达时间|服务时间|开始时间|完成时间|周转时间|带权周转| if(p-=finish_time) else start_time=p- finish_time=start_time+p- round_time=finish_time-p- right_round_time=(float)round_time/p-nt|%8d|%8d|%8d|%8d|%8d|%8d|%8.2f|ID,serve_time,start_time,finish_time,round_time,right_round_time);nt|-FCFS调度算法-|nvoid SortBySuper(node *head)
8、node *p1,*q1,*temp; temp=(node*)malloc(sizeof(node); for(p1=head-p1!=NULL&p1-p1=p1-next) for(q1=p1-q1!q1-q1=q1- if(p1-superID=p1-ID; p1-ID=q1- q1-ID=temp- super=p1-super;super=q1-super=temp-arrive_time=p1-arrive_time=q1-arrive_time=temp-serve_time=p1-serve_time=q1-serve_time=temp-state=p1-state;stat
9、e=q1-state=temp-利用优先数调度算法 void priority(node* head) SortBySuper(head);while(flag-nt|-优先数调度算法-|int main() char choice; dontt|-MUNE-|ntt|-1.FCFS调度算法-|ntt|-2.优先数调度算法-|nttt你的选择是:bb while(scanf( %cchoice)!=EOF&choice!=12 head=create(); switch(choice) case :print(head); FCFS(head);break; priority(head);运行结果截图:先来先服务调度算法运行结果优先级调度算法运行结果