1、先来先服务短作业优先时间片轮转高优先权算法#include#define N 4#include /时间片系列#include#include#define MAX 4 /进程数量#define RR 4 /时间片大小/时间片系列using namespace std;struct time string name; float arriveTime; float runTime; float finishTime; float totalTime; float weightTotalTime; ;/时间片系列struct prochar num;int arriveTime;int burs
2、t;int rt; /记录进程被运行的次数struct pro *next;int TOTALTIME; /记录所有进程的总时间/时间片系列/函数声明struct pro* creatList();void insert(struct pro *head,struct pro *s);struct pro* searchByAT(struct pro *head,int AT);void del(struct pro* p);int getCount(struct pro *head,int time);struct pro* searchEnd(struct pro *head);void
3、move(struct pro *headF,struct pro *headT,int n);struct pro* creatList() /创建链表,按照进程的到达时间排列,记录所有进程的信息struct pro* head=(struct pro*)malloc(sizeof(struct pro);head-next=NULL; struct pro* s;int i;TOTALTIME=0;cout输入进程名 & 到达时间 & 运行时间(example:a 0 0):endl; coutname arrivetime runtimeendl;for(i=0;is-num; cins
4、-arriveTime; cins-burst; TOTALTIME+=s-burst; /计算总时间 s-rt=1; /rt的初始值为1 s-next=NULL; insert(head,s);return head; /到达队列中的进程按照其到达时间的先后顺序排列void insert(struct pro *head,struct pro *s) /插入节点struct pro *p=searchByAT(head,s-arriveTime);s-next=p-next;p-next=s;return;struct pro* searchByAT(struct pro *head,int
5、 AT) /查找第一个到达时间大于等于AT的节点,返回其前一个指针struct pro *p,*q;p=head;q=head-next;while(q!=NULL&q-arriveTimenext;return p;void del(struct pro* p) /删除p的下一个节点struct pro *tmp;tmp=p-next;p-next=tmp-next;free(tmp);return;int getCount(struct pro *head,int time) /察看在time之前到达但未移动到运行队列的进程数量int count=0;struct pro *s,*t;s=
6、head;t=s-next;while(t!=NULL&t-arriveTimenext; count+; /count记录当前时刻到达的进程数return count;struct pro* searchEnd(struct pro *head) /查找并返回循坏队列的尾节点的前一个节点struct pro *p,*q;p=head;q=head-next;while(q-next!=head) p=q; q=q-next;return p;void move(struct pro *headF,struct pro *headT,int n) /将headF后的n个节点移动到循环队列hea
7、dT中struct pro *r,*s,*t;s=headF;t=s-next;r=t; /r记录要移动的第一个节点while(n1) t=t-next; n-;s-next=t-next; /以上完成从原队列中摘除相关节点,r,t分别为第一个和最后一个节点s=searchEnd(headT);if(s!=headT)s=s-next;t-next=s-next;s-next=r;struct pro *f=s;void run(struct pro *head)int time=0; /记录当前时间int newarrive;/新到达进程数struct pro *runhead=(struc
8、t pro*)malloc(sizeof(struct pro);runhead-next=runhead; /创建新的循环链表,存放当前就绪队列中的进程struct pro *p,*q;p=runhead; q=p-next; /q记录当前应当运行的进程while(time0) move(head,runhead,newarrive); /将head后的newarrive个节点移动到runhead队列中 if(runhead-next=runhead) /就绪队列中没有进程 time+; else if(q=runhead) p=q; q=q-next; else cout进程名:numen
9、dl; cout到达时间:arriveTimert=1) printf(响应时间:%dn,time-q-arriveTime); else printf(第%d次运行开始时间:%dn,q-rt,time); if(q-burstburst; printf(第%d次运行结束时间:%dn,q-rt,time); printf(周转时间:%dn,time-q-arriveTime); printf(*n); struct pro *tmp=q; q=q-next; p-next=q; runhead-next=q; free(tmp); else /q-burstRR time+=RR; print
10、f(第%d次运行结束时间:%dn,q-rt,time); printf(*n); q-burst-=RR; q-rt+; struct pro *h; h=runhead-next; if(h-next!=runhead) while(h-next!=runhead) h=h-next; runhead-next=q-next; h-next=q;q-next=runhead; q=runhead-next; else struct pro *n; n=q; p=q; q=q-next; /时间片系列 void InputTime(time *p) int i;/counter coutinp
11、ut name & arrive time & run time(example:a 0 0):endl;coutname arrivetime runtimeendl; for(i=0;iname; pi.name=name; cintemp1; pi.arriveTime=temp1; cintemp2; pi.runTime=temp2; void Print(time *p,float totalTimeSum,float weightTotalTimeSum) cout*运行次序:endl; for(int k=0;k=N-1;k+) coutpk.name ; coutendl;
12、cout平均周转时间:totalTimeSum/Nendl; cout平均带权周转时间:weightTotalTimeSum/Nendl; void sort(time *p) for(int i=0;i=N-1;i+) for(int j=0;j=i;j+) if(pi.arriveTimepj.arriveTime) time temp; temp=pi; pi=pj; pj=temp; void deal(time *p,float &totalTimeSum,float &weightTotalTimeSum) int k;/counter for(k=0;k=N-1;k+) if(k
13、=0) pk.finishTime=pk.arriveTime+pk.runTime; else pk.finishTime=pk-1.finishTime+pk.runTime; for(k=0;k=N-1;k+) pk.totalTime=pk.finishTime-pk.arriveTime; pk.weightTotalTime=pk.totalTime/pk.runTime; totalTimeSum+=pk.totalTime; weightTotalTimeSum+=pk.weightTotalTime; void FCFS(time *p) float totalTimeSum
14、=0,weightTotalTimeSum=0; sort(p); deal(p,totalTimeSum,weightTotalTimeSum); coutendl; cout*endl; cout*先来先服务:endl;Print(p,totalTimeSum,weightTotalTimeSum);cout*endl; void SWF(time *p) float totalTimeSum=0,weightTotalTimeSum=0; sort(p); for(int m=0;mN-1;m+) if(m=0) pm.finishTime=pm.arriveTime+pm.runTim
15、e; else pm.finishTime=pm-1.finishTime+pm.runTime; int i=0; for(int n=m+1;n=N-1;n+) if(pn.arriveTime=pm.finishTime) i+; float min=pm+1.runTime; int follow=m+1; for(int k=m+1;km+i;k+) if(pk+1.runTimemin) min=pk+1.runTime; follow=k+1; time temp; temp=pm+1; pm+1=pfollow; pfollow=temp; deal(p,totalTimeSu
16、m,weightTotalTimeSum); coutendl; cout*endl; cout*短作业优先:endl; Print(p,totalTimeSum,weightTotalTimeSum);cout*endl;void TRRF(time *p) float totalTimeSum=0,weightTotalTimeSum=0; sort(p); for(int m=0;mN-1;m+) if(m=0) pm.finishTime=pm.arriveTime+pm.runTime; else pm.finishTime=pm-1.finishTime+pm.runTime; i
17、nt i=0; for(int n=m+1;n=N-1;n+) if(pn.arriveTime=pm.finishTime) i+; float max=(pm.finishTime-pm+1.arriveTime)/pm+1.runTime; int follow=m+1; for(int k=m+1;km+i;k+) if(max=(pm.finishTime-pk+1.arriveTime)/pk+1.runTime) max=(pm.finishTime-pk+1.arriveTime)/pk+1.runTime; follow=k+1; time temp; temp=pm+1;
18、pm+1=pfollow; pfollow=temp; deal(p,totalTimeSum,weightTotalTimeSum); coutendl;cout*endl; cout*最高响应比优先:endl; Print(p,totalTimeSum,weightTotalTimeSum);cout*endl; void main() int lg; cout请选择任一种算法:endl; cout1.FCFS(先来先服务) 2.SWF(短作业优先) 3.TRRF(高响应比优先) 4.RR(时间片轮转) 0.退出lg;coutendl; time aN,bN,cN; while (lg)
19、if(lg=1) InputTime(a); FCFS(a); coutendl; /time *b=a;time *c=a; if(lg=2) InputTime(b); SWF(b); coutendl; if(lg=3) InputTime(c); TRRF(c); coutendl; /时间片系列 if(lg=4) coutendl; cout*时间片模拟*endl;struct pro *head=creatList();printf(当前时间片大小为:%dn,RR);run(head); coutendl; cout1.FCFS(先来先服务) 2.SWF(短作业优先) 3.TRRF(高响应比优先) 4.RR(时间片轮转) 0.退出lg; coutendl; cout您选择了退出模拟。endl;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1