时间片轮转法源程序.docVIP

  • 11
  • 0
  • 约3.56千字
  • 约 5页
  • 2016-09-28 发布于重庆
  • 举报
时间片轮转法源程序

#includestdio.h #includestdlib.h #define MAX 5 //进程数量 #define RR 2 //时间片大小 /*时间片轮转算法*/ struct pro { int num; int arriveTime; int burst; 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 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; for(i=0;iMAX;i++) { s=(struct pro*)malloc(sizeof(struct pro)); printf(请输入进程名:\n); scanf(%d,(s-num)); printf(请输入到达时间:\n); scanf(%d,(s-arriveTime)); printf(请输入运行时间:\n); scanf(%d,(s-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 AT) //查找第一个到达时间大于等于AT的节点,返回其前一个指针 { struct pro *p,*q; p=head; q=head-next; while(q!=NULLq-arriveTime=AT) { p=q; q=q-next; } 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=head; t=s-next; while(t!=NULLt-arriveTime=time) { s=t; t=t-next; 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个节点移动到循环队列headT中 { struct pro *r,*s,*t; s

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档