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

上传人:飞****2 文档编号:15087679 上传时间:2022-05-10 格式:DOCX 页数:7 大小:16.37KB
返回 下载 相关 举报
非抢占短作业优先算法源代码C语言.docx_第1页
第1页 / 共7页
非抢占短作业优先算法源代码C语言.docx_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《非抢占短作业优先算法源代码C语言.docx》由会员分享,可在线阅读,更多相关《非抢占短作业优先算法源代码C语言.docx(7页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精选优质文档-倾情为你奉上非抢占短作业优先算法源代码(C语言) #include <stdio.h>#include <stdlib.h>#define MAX 5 /进程数/*短作业优先算法*/struct proint num; /进程名int arriveTime; /到达时间int burst; /运行时间;struct pro *next;/函数声明struct pro* creatList();void insert(struct pro *head,struct pro *s); struct pro* searchByAT(struct pro *hea

2、d,int AT); void run(struct pro *head);void del(struct pro* p);int getCount(struct pro *head,int time);struct pro* creatList() /创建链表,按照进程的到达时间排列struct pro* head=(struct pro*)malloc(sizeof(struct pro);head->next=NULL;struct pro* s;int i;for(i=0;i<MAX;i+)s=(struct pro*)malloc(sizeof(struct pro);p

3、rintf("请输入进程名:n");scanf("%d",&(s->num);printf("请输入到达时间:n");scanf("%d",&(s->arriveTime);printf("请输入运行时间:n");scanf("%d",&(s->burst);s->next=NULL;insert(head,s);return head;void insert(struct pro *head,struct pro *s) /插

4、入节点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!=NULL&&q->arriveTime<=AT)p=q;q=q->next;return p;void del(struct pr

5、o* p) /删除p的下一个节点struct pro *tmp;tmp=p->next;p->next=tmp->next;free(tmp);return;int getCount(struct pro *head,int time) /察看当前就绪队列中的进程数int count=0;struct pro *p,*q;p=head;q=p->next;while(q!=NULL&&q->arriveTime<=time)count+;p=q;q=q->next;return count;struct pro* SJF(struct

6、pro *head,int count) /在头节点后的count个节点中选择burst最小的,返回其前一个节点的指针int min;struct pro *p,*q,*flag;p=head;q=p->next;min=q->burst;flag=p; /flag记录返回指针while(count>0)if(q->burst<min)min=q->burst;flag=p;count-;p=q;q=q->next;return flag;void run(struct pro *head) /按短作业优先算法调度进程,并输出其运行情况int time

7、=0,count;struct pro *s,*t;while(head->next!=NULL)count=getCount(head,time);if(count=0) /如果当前就绪队列中没有进程,时间自增time+;else if(count=1) /如果就绪队列中只有1个进程,则必定是第一个节点t=head;s=t->next;printf("进程名:%dn",s->num);printf("到达时间:%dn",s->arriveTime);printf("运行开始时间:%dn",time);prin

8、tf("响应时间:%dn",time-s->arriveTime);time+=s->burst;printf("运行结束时间:%dn",time);printf("周转时间:%dn",time-s->arriveTime);printf("*n");del(t );else /如果就绪队列中的进程数>=2,则在head后的count个节点中进行短作业优先调度t=SJF(head,count);s=t->next;printf("进程名:%dn",s->num

9、);printf("到达时间:%dn",s->arriveTime);printf("运行开始时间:%dn",time);printf("响应时间:%dn",time-s->arriveTime);time+=s->burst;printf("运行结束时间:%dn",time);printf("周转时间:%dn",time-s->arriveTime);printf("*n");del(t);void main()struct pro *head=creatList();printf("按短作业优先算法调度运行结果如下:n");run(head);专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com