




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include<stdio.h>#include<stdio.h>#include<malloc.h>typedefstructProcessNode{//进程结点的基本结构charname;//进程名 intservice_time;//服务时间 intarrive_time;//到达时间 intpriority;//优先级 structFCFS_time{//先到先服务 intfinish_time;//完成时间 intturnaround_time;//周转时间 floatweigtharound_time;//带权周转时间 }FCFS_time;structSJF_time{//短作业优先 intfinish_time; intturnaround_time; floatweigtharound_time; intflag; }SJF_time;structRR_time{//时间片轮转的结点 intfinish_time; intturnaround_time; floatweigtharound_time; intflag_time;//赋值为进程的服务时间,为0则进程完成 }RR_time;structPri_time{//优先权非抢占式 intfinish_time; intturnaround_time; floatweigtharound_time; }Pri_time; structProcessNode*next;}ProcessNode,*Linklist;voidmain(){ intchoice;Linklistp,head;Linklistread_information(); LinklistFCFS_scheduling(Linklisthead);LinklistSJF_scheduling(Linklisthead);LinklistRR_scheduling(Linklisthead);LinklistPri_scheduling(Linklisthead); head=read_information();//读入进程的基本信息 do{ p=head->next;printf("\n"); printf("**********进程初始信息输出**********\n");//输出初始化后的进程基本信息 printf("\n"); printf("进程名称"); printf("到达时间");printf("服务时间");printf("优先级");printf("\n"); while(p) {printf("%c",p->name); printf("%d",p->arrive_time);printf("%d",p->service_time);printf("%d",p->priority);printf("\n"); p=p->next; } printf("\n");printf("************************************\n");//输出进程的调用选择项 printf("\n"); printf("1、FCFS----先到先服务\n"); printf("2、SJF-----短作业优先\n"); printf("3、RR------时间片轮转\n"); printf("4、Pri-----优先权调度\n");printf("5、退出\n"); printf("\n"); printf("************************************\n"); printf("\n"); printf("请在1—5之间选择:"); scanf("%d",&choice); printf("\n");printf("\n");switch(choice) { case1:FCFS_scheduling(head); break; case2:SJF_scheduling(head); break; case3:RR_scheduling(head);break; case4:Pri_scheduling(head);break;// case5:exit(); } }while(choice!=5); }Linklistread_information()//进程读入函数{inti;int num;//ProcessNode; Linklistpro; Linklistp;Linklisthead;printf("\n");printf("************进程调度算法************\n"); printf("\n"); printf("请输入进程的个数:");scanf("%d",&num); printf("\n"); printf("*************初始化信息*************\n"); printf("\n");head=(Linklist)malloc(sizeof(ProcessNode));//头结点 head->next=NULL; p=head;for(i=1;i<=num;i++) {pro=(Linklist)malloc(sizeof(ProcessNode));//创建进程结点 printf("输入第%d个进程信息:\n",i); printf("请输入进程名:"); fflush(stdin); scanf("%c",&pro->name); printf("到达时间:"); scanf("%d",&pro->arrive_time); printf("服务时间:");scanf("%d",&pro->service_time); printf("优先级↑:"); scanf("%d",&pro->priority); //pro->next=head->next;head->next=pro;//逆序建链p->next=pro;p=pro;//顺序建链 //p++; pro->next=NULL; }printf("\n");returnhead;}LinklistFCFS_scheduling(Linklisthead)//先到先服务算法函数{ Linklistp;Linklistq;//指向前一进程p=head->next;while(p)//初始化进程的完成时间、周转时间、带权周转时间,初值均赋为0 { p->FCFS_time.finish_time=0; p->FCFS_time.turnaround_time=0; p->FCFS_time.weigtharound_time=0;p=p->next; }p=q=head->next;p->FCFS_time.finish_time=p->arrive_time;//避免第一个进程到达时间不为0while(p) {if(p->arrive_time<=q->FCFS_time.finish_time)//下一进程已到达,在等待中 { p->FCFS_time.finish_time=(p->service_time)+(q->FCFS_time.finish_time);//服务时间p->FCFS_time.turnaround_time=(p->FCFS_time.finish_time)-(p->arrive_time);//周转时间 p->FCFS_time.weigtharound_time=(float)(p->FCFS_time.turnaround_time)/(p->service_time);//带权周转时间 } else { p->FCFS_time.finish_time=p->service_time+p->arrive_time;//服务时间p->FCFS_time.turnaround_time=(p->FCFS_time.finish_time)-(p->arrive_time);//周转时间 p->FCFS_time.weigtharound_time=(float)(p->FCFS_time.turnaround_time)/(p->service_time);//带权周转时间 } q=p; p=p->next; }p=head->next;printf("********************************FCFS********************************\n");//输出先到先服务调度后的进程信息 printf("\n"); printf("进程名称"); printf("到达时间");printf("服务时间");printf("优先级"); printf("完成时间"); printf("周转时间"); printf("带权周转时间");printf("\n");while(p) { printf("%c",p->name); printf("%d",p->arrive_time);printf("%d",p->service_time);printf("%d",p->priority); printf("%d",p->FCFS_time.finish_time);printf("%d",p->FCFS_time.turnaround_time);printf("%0.2f",p->FCFS_time.weigtharound_time);printf("\n"); p=p->next; }printf("\n");printf("**********************************************************************\n");printf("\n");returnhead;}LinklistSJF_scheduling(Linklisthead)//短作业优先算法{ Linklistp,r; Linklistq;//指向前一进程结点intnum=0;//记录进程个数intadd_flag=0;//进程完成服务个数 intservice_time_min; intarrive_time; intk; p=head->next;//首元结点while(p)//初始化进程的完成时间、周转时间、带权周转时间,初值均赋为0 { p->SJF_time.finish_time=0; p->SJF_time.turnaround_time=0; p->SJF_time.weigtharound_time=0; p->SJF_time.flag=0; ++num; q=p;p=p->next; }q->next=head->next;//将创建的进程队列变为循环队列p=head->next;q=p; p->SJF_time.finish_time=p->arrive_time+p->service_time;p->SJF_time.turnaround_time=(p->SJF_time.finish_time)-(p->arrive_time);//周转时间p->SJF_time.weigtharound_time=(float)(p->SJF_time.turnaround_time)/(p->service_time);//带权周转时间 q->SJF_time.finish_time=p->SJF_time.finish_time;p->SJF_time.flag=1; add_flag=1; p=p->next;do{ if(p->SJF_time.flag==1){p=p->next;} elseif((p->arrive_time)>(q->SJF_time.finish_time)) { service_time_min=p->service_time; arrive_time=p->arrive_time; while(p->arrive_time==arrive_time&&p->SJF_time.flag==0)//寻找最短的作业 { if((p->next->service_time)<(p->service_time)){service_time_min=p->next->service_time;p=p->next;} else{p=p->next;} } p=q->next; r=q; while(p->service_time!=service_time_min){p=p->next;}//指针指向最短作业 p->SJF_time.finish_time=p->arrive_time+p->service_time; p->SJF_time.flag=1;++add_flag; p->SJF_time.turnaround_time=(p->SJF_time.finish_time)-(p->arrive_time);//周转时间 p->SJF_time.weigtharound_time=(float)(p->SJF_time.turnaround_time)/(p->service_time);//带权周转时间q=p;p=r->next; } else { k=0; service_time_min=p->service_time; while(((p->arrive_time)<=(q->SJF_time.finish_time))&&k<=num)//寻找最短的作业 { if(p->SJF_time.flag==1){p=p->next;++k;} elseif((p->SJF_time.flag!=1)&&((p->service_time)<service_time_min)) {service_time_min=p->service_time; p=p->next;++k;} else{p=p->next;++k;} } p=q->next; r=q; while(p->service_time!=service_time_min){p=p->next;}//指针指向最短作业p->SJF_time.finish_time=q->SJF_time.finish_time+p->service_time;p->SJF_time.turnaround_time=(p->SJF_time.finish_time)-(p->arrive_time);//周转时间 p->SJF_time.weigtharound_time=(float)(p->SJF_time.turnaround_time)/(p->service_time);//带权周转时间p->SJF_time.flag=1;++add_flag; //q=p;p=p->next; q=p;p=r->next; } }while(add_flag!=num);for(p=head->next;num>0;num--)//断开循环队列 {q=p;p=p->next; } q->next=NULL;p=head->next;//指向链首,输出短作业调度后的进程信息printf("\n");printf("********************************SJF*********************************\n"); printf("\n"); printf("进程名称"); printf("到达时间");printf("服务时间");printf("优先级"); printf("完成时间"); printf("周转时间"); printf("带权周转时间");printf("\n");while(p) { printf("%c",p->name); printf("%d",p->arrive_time);printf("%d",p->service_time);printf("%d",p->priority); printf("%d",p->SJF_time.finish_time);printf("%d",p->SJF_time.turnaround_time);printf("%0.2f",p->SJF_time.weigtharound_time);printf("\n"); p=p->next; }printf("\n");printf("**********************************************************************\n");printf("\n");returnhead;}LinklistRR_scheduling(Linklisthead)//时间片轮转算法{ Linklistq;//指向前一进程结点Linklistp; intq_time;//时间片大小 intnum=0;//记录进程个数intadd_flag=0;//进程完成服务个数 printf("请输入时间片的大小:"); scanf("%d",&q_time);p=head->next;while(p)//初始化进程的完成时间、周转时间、带权周转时间,初值均赋为0 { p->RR_time.finish_time=0; p->RR_time.turnaround_time=0; p->RR_time.weigtharound_time=0; p->RR_time.flag_time=p->service_time; q=p; ++num;p=p->next; } q->next=head->next;//将创建的进程队列变为循环队列p=head->next;q->RR_time.finish_time=p->arrive_time; do{ /*printf("\n");printf("**************************************************************\n");printf("%c",p->name); printf("%d",p->arrive_time);printf("%d",p->service_time);printf("%d",p->priority); printf("%d",p->RR_time.finish_time);printf("\n");*/ if((p->RR_time.flag_time)>(q_time))//服务时间大于时间片 { p->RR_time.finish_time=(q->RR_time.finish_time)+(q_time);//累加完成时间p->RR_time.flag_time=(p->RR_time.flag_time)-(q_time); if((p->next->arrive_time)<=(p->RR_time.finish_time))//有进程等待 {q=p;p=p->next;} else//当前进程未完成,无进程等待,指针不向后移 {q=p;} }elseif((p->RR_time.flag_time)==0)//进程已经完成 { p=p->next; }else { p->RR_time.finish_time=(q->RR_time.finish_time)+(p->RR_time.flag_time);p->RR_time.flag_time=0;++add_flag;p->RR_time.turnaround_time=(p->RR_time.finish_time)-(p->arrive_time);//周转时间 p->RR_time.weigtharound_time=(float)(p->RR_time.turnaround_time)/(p->service_time);//带权周转时间if((p->next->arrive_time)<(p->RR_time.finish_time))//有进程等待 {q=p;p=p->next;} else//当前进程完成,无进程等待,指针向后移 //{q=p;q->RR_time.finish_time=p->next->
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第1课 文明的产生与早期发展 教学设计-2023-2024学年高一统编版2019必修中外历史纲要下册
- 2024年四川南充临江产业发展集团有限责任公司招聘22人笔试参考题库附带答案详解
- Unit6 Pronunciation-2a-2f 教学设计 2024-2025学年人教版英语七年级上册
- 2024年甘肃省高等职业教育分类考试招生中职升学考试公共基础试题数学试题
- 2024年12月2025年上海对外经贸大学辅导员公开招聘笔试历年典型考题(历年真题考点)解题思路附带答案详解
- 中学联盟浙江省绍兴县杨汛桥镇中学人教版八年级(历史与社会)上册教学设计 2-2 阿拉伯帝国与伊斯兰文明
- 第二单元第6课《互联网应用中的数据》-教学设计 2023-2024学年浙教版(2023)初中信息技术八年级上册
- 第二单元第7课一、《放大镜效果》教学设计 2023-2024学年人教版初中信息技术七年级下册
- 2025年集群通信系统(数字)合作协议书
- 2025年黑龙江幼儿师范高等专科学校单招职业技能测试题库完美版
- 运动康复机构跌倒风险管理措施
- 开学安全第一课主题班会课件
- 一年级珍惜粮食主题班会学习教案
- 残疾人的就业创业与自我发展
- 全套课件-建筑工程质量与安全管理
- 医院感染的中心静脉导管相关血流感染预防
- 新版《医疗器械经营质量管理规范》(2024)培训试题及答案
- 2025年人教版数学五年级下册教学计划(含进度表)
- DBJ33T 1286-2022 住宅工程质量常见问题控制标准
- 海岸动力学英文课件Coastal Hydrodynamics-复习
- 碳足迹研究-洞察分析
评论
0/150
提交评论