非抢占短作业优先算法源代码C语言.docx

非抢占短作业优先算法源代码C语言.docx

ID:57221643

大小:12.05 KB

页数:7页

时间:2020-08-06

非抢占短作业优先算法源代码C语言.docx_第1页
非抢占短作业优先算法源代码C语言.docx_第2页
非抢占短作业优先算法源代码C语言.docx_第3页
非抢占短作业优先算法源代码C语言.docx_第4页
非抢占短作业优先算法源代码C语言.docx_第5页
资源描述:

《非抢占短作业优先算法源代码C语言.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、非抢占短作业优先算法源代码(C语言)#include<stdio.h>#include<stdlib.h>#defineMAX5//进程数/*短作业优先算法*/structpro{intnum;//进程名intarriveTime;//到达时间intburst;//运行时间;structpro*next;};//函数声明structpro*creatList();voidinsert(structpro*head,structpro*s);structpro*searchByAT(structpro*head,

2、intAT);voidrun(structpro*head);voiddel(structpro*p);intgetCount(structpro*head,inttime);structpro*creatList()//创建链表,按照进程的到达时间排列{structpro*head=(structpro*)malloc(sizeof(structpro));head->next=NULL;structpro*s;inti;for(i=0;i<MAX;i++){s=(structpro*)malloc(sizeof(str

3、uctpro));printf("请输入进程名:");scanf("%d",&(s->num));printf("请输入到达时间:");scanf("%d",&(s->arriveTime));printf("请输入运行时间:");scanf("%d",&(s->burst));s->next=NULL;insert(head,s);}returnhead;

4、}voidinsert(structpro*head,structpro*s)//插入节点{structpro*p=searchByAT(head,s->arriveTime);s->next=p->next;p->next=s;return;}structpro*searchByAT(structpro*head,intAT)//查找第一个到达时间大于等于AT的节点,返回其前一个指针{structpro*p,*q;p=head;q=head->next;while(q!=NULL&&q-

5、>arriveTime<=AT){p=q;q=q->next;}returnp;}voiddel(structpro*p)//删除p的下一个节点{structpro*tmp;tmp=p->next;p->next=tmp->next;free(tmp);return;}intgetCount(structpro*head,inttime)//察看当前就绪队列中的进程数{intcount=0;structpro*p,*q;p=head;q=p->next;while(q!=NULL&&a

6、mp;q->arriveTime<=time){count++;p=q;q=q->next;}returncount;}structpro*SJF(structpro*head,intcount)//在头节点后的count个节点中选择burst最小的,返回其前一个节点的指针{intmin;structpro*p,*q,*flag;p=head;q=p->next;min=q->burst;flag=p;//flag记录返回指针while(count>0){if(q->burst<min)

7、{min=q->burst;flag=p;}count--;p=q;q=q->next;}returnflag;}voidrun(structpro*head)//按短作业优先算法调度进程,并输出其运行情况{inttime=0,count;structpro*s,*t;while(head->next!=NULL){count=getCount(head,time);if(count==0)//如果当前就绪队列中没有进程,时间自增time++;elseif(count==1)//如果就绪队列中只有1个进程,则必定是第

8、一个节点{t=head;s=t->next;printf("进程名:%d",s->num);printf("到达时间:%d",s->arriveTime)

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。