操作系统实验报告材料进程调度_第1页
操作系统实验报告材料进程调度_第2页
操作系统实验报告材料进程调度_第3页
操作系统实验报告材料进程调度_第4页
操作系统实验报告材料进程调度_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

wordword/word五邑大学实验报告五邑大学实验报告操作系统课程2016~2017年度第1学期实验题目:进程调度院系:计算机学院班级: 140801学号:3114002472某某:黄凯鑫任课教师:白明成绩评定:实验二题目:进程调度完成日期:2016年12月11日1、实验目的〔1〕设计一个有n个进程工行的进程调度程序。每个进程由一个进程控制块〔PCB〕表示。进程控制块通常应包含下述信息:进程名、进程优先数、进程需要运行的时间、占用CPU的时间以与进程的状态等,且可按调度算法的不同而增删。〔2〕调度程序应包含2~3种不同的调度算法,运行时可任意选一种,以利于各种算法的分析比拟。〔3〕系统应能显示或打印各进程状态和参数的变化情况,便于观察诸进程的调度过程2、实验内容〔1〕编制和调试示例给出的进程调度程序,并使其投入运行。〔2〕自行设计或改写一个进程调度程序,在相应机器上调试和运行该程序,其功能应该不亚于示例。〔3〕直观地评测各种调度算法的性能。3、算法设计算法:(1)优先数法。进程就绪链按优先数大小从高到低排列,链首进程首先投入运行。每过一个时间片,运行进程所需运行的时间片数减1,说明它已运行了一个时间片,优先数也减3,理由是该进程如果在一个时间片中完成不了,优先级应该降低一级。接着比拟现行进程和就绪链链首进程的优先数,如果仍是现行进程高或者一样,就让现行进程继续进展,否如此,调度就绪链链首进程投入运行。原运行进程再按其优先数大小插入就绪链,且改变它们对应的进程状态,直至所有进程都运行完各自的时间片数。(2)简单轮转法。进程就绪链按各进程进入的先后次序排列,进程每次占用处理机的轮转时间按其重要程度登入进程控制块中的轮转时间片数记录项〔相当于优先数法的优先数记录项位置〕。每过一个时间片,运行进程占用处理机的时间片数加1,然后比拟占用处理机的时间片数是否与该进程的轮转时间片数相等,假如相等说明已到达轮转时间,应将现运行进程排到就绪链末尾,调度链首进程占用处理机,且改变它们的进程状态,直至所有进程完成各自的时间片。实验源代码:#include<stdio.h>#include<dos.h>#include<stdlib.h>#include<conio.h>#include<iostream.h>#include<time.h>enumstate//进程的状态{Ready, Working, Finish};structpcb//PCB数据结构{ intpid; intpriority; intcputime; intneedtime; intround; stateprocess; pcb*next;};inttimepiece;pcb*get_process(){ //优先数算法--输入进程个数 intproc; pcb*q; pcb*t; pcb*p; inti=0; cout<<"InputProcessNumber(1-10):"; cin>>proc; while(proc<1||proc>10){ cout<<endl<<"IllegalInput!"<<endl<<endl<<"InputProcessNumber(1-10):"; cin>>proc; } //cout<<endl<<endl<<"StartScheduling!!!\n\n"; getch(); srand((unsigned)time(NULL));//初始化随机数种子发生器 while(i<proc){ q=(structpcb*)malloc(sizeof(pcb)); q->pid=rand()%10000; q->needtime=rand()%10+1; q->cputime=0; q->priority=rand()%100; q->process=Ready; q->next=NULL;//利用随机数生成进程信息 if(i==0) { p=q; t=q; } else { t->next=q; t=q; }//尾插法建立PCB节点 i++; }//while returnp;}voiddisplay(pcb*p){ //优先数算法结果输出 cout<<"ProcessID"<<""<<"Cputime"<<""<<"Needtime"<<""<<"Priority"<<""<<"State"<<endl; while(p){ cout<<""<<p->pid; cout<<"\t\t"; cout<<p->cputime; cout<<"\t"; cout<<p->needtime; cout<<"\t"; if(p->needtime==0) cout<<"Done"; else cout<<p->priority; cout<<"\t\t"; switch(p->process){ caseReady:cout<<"Ready"<<endl;break; caseWorking:cout<<"\b\b->Working<-"<<endl;break; caseFinish:cout<<"Finish"<<endl;break; } p=p->next; }}intprocess_finish(pcb*q)//判断是否所有进程都已完成,是如此返回1{ intbl=1; while(bl&&q){ bl=bl&&q->needtime==0; q=q->next; } returnbl;}voidcpuexe(pcb*q){//优先数算法模拟进程执行函数 pcb*t=q; inttp=-1; while(q){ if(q->process!=Finish) {//未完成的进程置Ready,完成的进程置Finish q->process=Ready; if(q->needtime==0) { q->process=Finish; } } if(tp<q->priority&&q->process!=Finish) {//找到下一个优先数最高且未完成的进程 tp=q->priority; t=q; } q=q->next; } if(t->needtime!=0){//修改正在执行的进程的信息,并置其状态为Working t->priority-=3; if(t->priority<0)t->priority=0; t->needtime--; t->process=Working; t->cputime++; }}voidpriority_cal(){ //优先数算法主控函数 pcb*p; system("cls"); p=get_process(); intcpu=0; charkey; system("cls"); cout<<"CPUTime:"<<cpu<<endl; display(p); cout<<endl; getch(); while(!process_finish(p)){//当不是所有进程都完成时不断执行进程并显示信息 cpu++; cout<<"CPUTime:"<<cpu<<endl; cpuexe(p); display(p); cout<<endl; key=getch(); if(key=='q')exit(0); } printf("Allprocessesarefinished!"); getch();}pcb*get_process_round(){ //时间片算法--输入进程个数与CPU时间片 intproc; pcb*q; pcb*t; pcb*p; inti=0; cout<<"InputProcessNumber(1-10):"; cin>>proc; while(proc<1||proc>10){ cout<<endl<<"Yourprocessisoutoforder,pleasetryagain!"<<endl<<endl<<"InputProcessNumber(1-10):"; cin>>proc; } cout<<"CPUTimePiece(1-5)?"; cin>>timepiece; while(timepiece<1||timepiece>5){ cout<<endl<<"IllegalInput!"<<endl<<endl<<"CPUTimePiece(1-5)?"; cin>>timepiece; } //cout<<endl<<endl<<"StartScheduling!!!\n\n"; getch(); srand((unsigned)time(NULL));//初始化随机数种子发生器 while(i<proc){ q=(structpcb*)malloc(sizeof(pcb));//利用随机数建立进程信息 q->pid=rand()%10000; q->needtime=rand()%10+1; q->cputime=0; q->round=0; q->process=Ready; q->next=NULL; if(i==0){//尾插法建立PCB节点p=q; t=q; } else{ t->next=q; t=q; } i++; }//while returnp;}voidcpu_round(pcb*p,pcb*q){ //时间片算法模拟进程执行函数 while(p){ if(p->needtime==0){//完成的进程置Finish,其它置Ready p->process=Finish; } if(p->process==Working){ p->process=Ready; } p=p->next; } q->cputime+=timepiece;//修改正在执行进程的信息,并置其状态为Working q->needtime-=timepiece; if(q->needtime<0){ q->needtime=0; } q->round++; q->process=Working;}pcb*get_next(pcb*k,pcb*head){ //得到下一个应执行的进程 pcb*t; t=k; do{ t=t->next; } while(t&&t->process==Finish); if(t==NULL){ t=head; while(t!=k&&t->process==Finish) { t=t->next; } } returnt;}voiddisplay_round(pcb*p){ //时间片算法输出结果 cout<<"ProcessID"<<""<<"Cputime"<<""<<"Needtime"<<""<<"Round"<<""<<"State"<<endl; while(p){ cout<<""<<p->pid; cout<<"\t\t"; cout<<p->cputime; cout<<"\t"; cout<<p->needtime; cout<<"\t"; cout<<p->round; cout<<"\t"; switch(p->process){ caseReady:cout<<"Ready"<<endl;break; caseWorking:cout<<"\b\b->Working<-"<<endl;break; caseFinish:cout<<"Finish"<<endl;break; } p=p->next; }}voidround_cal(){ pcb*p; pcb*r; system("cls"); p=get_process_round(); intcpu=0; charkey; system("cls"); cout<<"CPUTime:"<<cpu<<endl; display_round(p); cout<<endl; getch(); r=p; while(!process_finish(p)){ cpu+=timepiece; cpu_round(p,r); r=get_next(r,p); cout<<"CPUTime:"<<cpu<<endl; display_round(p); cout<<endl; key=getch(); if(key=='q')exit(0); }}voiddisplay_menu(){ cout<<"1Priority"<<endl; cout<<"2RoundRobin"<<endl; cout<<"3Exit"<<endl; cout<<"Choice:";}intmain(){ charkey; display_menu(); cin>>key;switch(key){ case'1':priority_cal();break;case'2':round_cal();break; case'3':exit(0);}return0;}4、运行与测试 P算法:输入进程数4以后,由sr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论