




已阅读5页,还剩64页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
徐州工程学院管理学院实验报告实验课程名称 : 操作系统 实验地点: 南主楼七楼机房 2014 年 2 月至 2014 年 4 月 专 业 信息管理与信息系统 班 级 11信管2班 学生姓名 学 号 指导老师 刘一男 实验报告实验项目:分时系统模拟实验学时: 2实验日期:实验要求:实验内容:时间片大小为1,利用程序模拟A,B,C,D,E五个进程按时间片轮转的调度及执行过程并计算各进程的周转时间及带权周转时间。 进程 周转时间 带权周转时间 A 12 3 B 9 3 C 14 3.5 D 8 4 E 13 3.25完成顺序:BDACE修改时间片大小为2,利用程序模拟A,B,C,D,E五个进程按时间片轮转的调度及执行过程并计算各进程的周转时间及带权周转时间。 进程 周转时间 带权周转时间 A 8 2 B 12 4 C 13 3.25 D 7 3.5 E 13 3.25完成顺序:ADBCE修改时间片大小为4,利用程序模拟A,B,C,D,E五个进程按时间片轮转的调度及执行过程并计算各进程的周转时间及带权周转时间。进程 周转时间 带权周转时间 A 4 1 B 6 2 C 9 2.25 D 10 5 E 13 3.25完成顺序:ABCDE思考时间片的大小对调度算法产生什么影响?对计算机的性能产生什么影响?答:在时间片轮转算法中,时间片的大小对系统性能有很大的影响,如选择很小的时间片将有利于短作业,因为它能较快地完成,但会频繁地发生中断、进程上下文的切换,从而增加系统的开销,降低了CPU效率;反之,如选择太长的时间片,使得每个进程都能在一个时间片内完成,时间片轮转算法便退化为FCFS算法,无法满足交互式用户的需求。一个较为可取的大小是,时间片略大于一次典型的交互所需要的时间。这样可使大多数进程在一个时间片内完成。实验代码:#include stdio.h#include stdlib.h#include iostream.h#include cstdlib struct process char name; int arri_time;/arrived time int serv_time;/servered time int have_done_time;/已经运行时间 int done_time;/完成时间 int turn_around_time;/周转时间 float wei_turn_around_time;/带权周转时间 struct process * next; ;int time=-1;int time_slice=1;/时间片初始值为1Isempty(struct process* head)if (head-next=NULL)return true;else return false;void pushline(struct process* head,struct process* rn)if (rn=NULL)return;struct process* x;x=head;while (x-next!=NULL)x=x-next;x-next=rn;void popline(struct process* head,struct process* x)if(!Isempty(head)*x=head-next;head-next=(*x)-next;(*x)-next=NULL;void isnewcome(struct process* head,struct process* readyline)struct process* x;if (head-next=NULL)return;if (head-next-arri_time=time&head-next!=NULL)couttime time a new process arrivenext=NULL; int i; struct process* rightnow; struct process* p; /*struct process* n;*/ head=(struct process*)malloc(sizeof(process); if(!head) cout内存分配错误next=NULL; tail=head; while (1) newnode=(struct process*)malloc(sizeof(process); cout请输入进程名newnode-name; if(newnode-name=#) break; cout请输入进程到达时间newnode-arri_time; cout请输入进程服务时间newnode-serv_time; newnode-done_time=0; newnode-turn_around_time=0; newnode-wei_turn_around_time=0.0; newnode-have_done_time=0; / tail-next=newnode; newnode-next=NULL; /tail=newnode; pushline(head,newnode); p=head-next; while(p!=NULL) coutname进程next; ss: while(time200) time+; /cout当前TIMEtimeendl; isnewcome(head,readyline); if (time!=0) pushline(readyline,rightnow); i=time_slice; /couti=iendl; while (i!=0) /coutDANG QIANG TIMEtimehave_done_time+; rightnow-done_time=time; coutrun namehave_done_time=rightnow-serv_time) rightnow-turn_around_time=rightnow-done_time-rightnow-arri_time+1; rightnow-wei_turn_around_time=(float)rightnow-turn_around_time/rightnow-serv_time; cout*endlname have done!endl; coutturn_around_time:turn_around_timeendl; coutwei_turn_around_time:wei_turn_around_timeendl; cout*have_done_time+; rightnow-done_time=time; coutrun namehave_done_time=rightnow-serv_time) rightnow-turn_around_time=rightnow-done_time-rightnow-arri_time+1; rightnow-wei_turn_around_time=(float)rightnow-turn_around_time/rightnow-serv_time; cout*endlname have done!endl; coutturn_around_time:turn_around_timeendl; coutwei_turn_around_time:wei_turn_around_timeendl; cout*next; head=readyline-next; p=readyline; readyline=readyline-next; while (1) time_slice=1; while (time_slice!=0) time+; q=head-next; while (q!=NULL) if (q-arri_time=time) n=readyline;while (n-next!=NULL)n=n-next;n-next=q; /加入就绪队列 else q=q-next; /便利链表将到达进程加入就绪队列 time_slice-; p-have_done_time+; if(p-have_done_time=p-done_time) coutname完成endl; /运行当前进程 if(readyline=NULL) break; */ system(pause); 68实验项目:多道作业调度实验学时:2实验日期:实验要求:系统初始状态100K内存,5台磁带机,作业初始状态为资源要求用户名 作业名状态到达时间运行时间(小时)主存(K)磁带机AJOBAW9:000.25202BJOBBN9:200.35601CJOBCN9:300.15453DJOBDN9:350.2102EJOBEN9:450.1253截图记录程序运行每5分钟的系统资源的状态及各个作业的状态,简要文字说明各个作业所处状态的原因,分析作业调度采用何种调度算法实验内容:初始时间是9:00程序运行5分钟,时间到达9:05。系统资源数量为80, A、B、C、D、E作业状态为R、N、N、N、N。程序运行5分钟,时间到达9:10。系统资源数量为80, A、B、C、D、E作业状态为R、N、N、N、N。程序运行5分钟,时间到达9:15。系统资源数量为100, A、B、C、D、E作业状态为F、N、N、N、N。程序运行5分钟,时间到达9:20。系统资源数量为40, A、B、C、D、E作业状态为F、R、N、N、N。A已经完成程序运行5分钟,时间到达9:25。系统资源数量为40, A、B、C、D、E作业状态为F、R、N、N、N。程序运行5分钟,时间到达9:30。系统资源数量为80, A、B、C、D、E作业状态为F、R、W、N、N。程序运行5分钟,时间到达9:35。系统资源数量为30, A、B、C、D、E作业状态为F、R、W、R、N。程序运行5分钟,时间到达9:40。系统资源数量为30, A、B、C、D、E作业状态为F、R、W、R、N。程序运行5分钟,时间到达9:45。系统资源数量为45, A、B、C、D、E作业状态为F、F、R、R、W。程序运行5分钟,时间到达9:50。系统资源数量为65, A、B、C、D、E作业状态为F、F、F、R、R。程序运行5分钟,时间到达9:55。系统资源数量为75, A、B、C、D、E作业状态为F、F、F、F、R。程序运行5分钟,作业已完成。程序运行最后结果实验代码:#include#include#include#include#include#define getjcb() (JCB*)malloc(sizeof(JCB)typedef struct /资源的总量int memory;int tape;RESOURCE;typedef struct JCB /作业控制块char username20;/用户名char jobname10;/作业名char state;/作业状态char atime5;/到达时间float rtime;/运行时间RESOURCE resource;/资源数量struct JCB*link;JCB;RESOURCE source = 100,5;JCB *pjcb =getjcb();/作业链表头char nowtime5;/现在时间,初始时间为9:00FILE* ignore(FILE *fp)/忽略文件中的空白符if(feof(fp) return fp;char ch = fgetc(fp);while (!feof(fp) & (ch = | ch = )ch = fgetc(fp);/if(!feof(fp) return fp;fseek(fp, -1, SEEK_CUR);return fp;FILE* findchar(FILE *fp,char c)/在文件中找到一个字符的位置(读取文件时用)if(feof(fp) return fp;char ch = fgetc(fp);while (!feof(fp) & (ch != c)ch = fgetc(fp);fseek(fp, -1, SEEK_CUR);return fp;void destory()/释放链表所占的内存JCB *p = pjcb-link;while(pjcb)free(pjcb);pjcb = p;if(p)p = p-link;float stof(char *time)/把时间转化为浮点型数float h = 0, m = 0;int i = 0;while(timei != :)h = h*10 + timei - 0;i+;i+;while(timei != 0)m = m*10 + timei - 0;i+;return (h + m/60);char* ftos(double ftime)/把浮点型数值转化为时间int h,m;h = int(ftime);m = int(ftime-h)*60);sprintf(nowtime,%c:%c%c,h+0,int(m/10)+0,int(m%10)+0);return nowtime;float timesub(char *time1, char *time2)/两个时间相减,得到时间差return stof(time1) - stof(time2);void print()/打印输出JCB *p = pjcb-link;printf(现在时间是%sn,nowtime);printf(现在资源的数量%dtt%dn,source.memory,source.tape);printf(ttttttt 资源要求n);printf(用户名t作业名t状态t到达时间t运行时间(小时)t主存(K)t磁带机n);while(p)printf(%st%st%ct%sttt%.2ft%dt%dn, p-username, p-jobname, p-state, p-atime, p-rtime, p-resource.memory,p-resource.tape);p = p-link;void sendsource()/为作业分配资源JCB *p;p = pjcb-link;while(p)/为到达的作业调度if(p-state = W & source.memory - p-resource.memory =0 & source.tape - p-resource.tape =0)p-state = R;source.memory -= p-resource.memory;source.tape -= p-resource.tape;printf(n%st%s被调入内存n, p-username, p-jobname);p = p-link;void init()/初始化,读取文件中的作业信息FILE *fp;JCB *p= NULL,*q = pjcb ;if(fp = fopen(jobs.txt, r) = NULL)printf(Cannot open the file!);exit(1);rewind(fp);fp = findchar(fp, A);while (!feof(fp) p = getjcb();fscanf(fp, %s,p-username);fp = ignore(fp);fscanf(fp, %s,p-jobname);fp = ignore(fp);fscanf(fp, %c,&p-state);fp = ignore(fp);fscanf(fp, %s,p-atime);fp = ignore(fp);p-rtime = 0;/不初始化则会发生错误,?fscanf(fp, %f,&(p-rtime);fp = ignore(fp);fscanf(fp, %d,&p-resource.memory);fp = ignore(fp);fscanf(fp, %d,&p-resource.tape); fp = ignore(fp);q-link = p;q = p;p -link = NULL;sendsource();fclose(fp);int checkend()/检查是否所有的作业都已经运行完了JCB *p = pjcb -link;while(p)if(p -state != F)return 0;p = p-link;return 1;void run()/运行作业if(checkend()/检查是否所有的作业都已经运行完了printf(所有作业都已经完成.n);exit(0);JCB *p = pjcb -link;double time;while(p)/作业运行完毕释放资源time = stof(nowtime) - stof(p-atime);if(p -state = R & time = p-rtime)p-state = F;source.memory += p-resource.memory;source.tape += p-resource.tape;printf(n%st%s已经执行结束n, p-username, p-jobname);break;p = p-link;p = pjcb -link;while(p)/计算到达的作业if( strcmp(nowtime, p-atime) =0 & p-state = N)p-state = W;printf(n%st%s作业已到达n, p-username, p-jobname);p = p-link;sendsource();/为作业分配资源print();int main()char ch;double time =9.00;double step = float(5)/60+0.00001;ftos(9.0);init();dorun();puts(*n);puts(是否继续运行,每次运行5分钟Y/N。);puts(*n);ch = getche();time += step;ftos(time);while(toupper(ch) = Y);destory();return 0; system(pause); getch();实验项目:PV操作-生产者与消费者实验学时: 2 实验日期:实验要求:1. 由用户指定要产生的进程及其类别,存入就绪队列。2. 调度程序从就绪队列中提取一个就绪进程运行。如果申请的资源被阻塞则进入相应的等待队列,调度程序调度就绪队列中的下一个进程。进程运行结束时,会检查对应的等待队列,激活队列中的进程进入就绪队列。运行结束的进程进入over链表。重复这一过程直至就绪队列为空。3. 输入两个进程,分别为生产者和消费者,按照先生产后消费顺序输入,观察并记录运行结果;调整输入进程的顺序,观察并记录运行结果4. 输入多个进程,随机分配为生产者和消费者,按(1)两种进程数量相同、(2)生产者多于消费者、(3)生产者少于消费者三种情况,观察并记录运行结果5. 多次输入多个进程,随机分配为生产者和消费者,使缓冲区中产品最后都被消费完,观察并记录运行结果实验内容:输入两个进程,分别为生产者和消费者,按照先生产后消费顺序输入,观察并记录运行结果输入两个进程,分别为生产者和消费者,按照先生产后消费顺序输入,观察并记录运行结果输入多个进程,随机分配为生产者和消费者 (1)两种进程数量相同、观察并记录运行结果(2)生产者多于消费者、观察并记录运行结果(3)生产者少于消费者,观察并记录运行结果多次输入多个进程,随机分配为生产者和消费者,使缓冲区中产品最后都被消费完,观察并记录运行结果实验代码:#include stdio.h#include #include #include iostream.h#define NULL 0 #define OK 1#define ERROR 0#define buffersize 3#define getpch(type) (type*)malloc(sizeof(type) int productnum=0; /产品数量int processnum=0;/进程计数器int full=0; int empty=buffersize; / semaphorechar bufferbuffersize; / 缓冲区int bufferpoint=0; / 缓冲区指针struct pcb /* 定义进程控制块PCB */int flag; / flag=1 表示生产者; flag=2 表示消费者int numlabel;/进程编号char product;/产品char state;/进程状态struct pcb * processlink;*exe=NULL,*over=NULL; / over链表typedef struct pcb PCB;PCB* readyhead=NULL, * readytail=NULL; / 就绪队列PCB* consumerhead=NULL, * consumertail=NULL; / 消费者队列PCB* producerhead=NULL, * producertail=NULL; / 生产者队列int InitQueue (PCB* head,PCB* tail)/初始化队列 if(!head) exit(0); head-processlink=NULL; return OK;bool hasElement(PCB*pro)/判断队列是否为空if(pro-processlink=NULL)return false;else return true;void linkqueue(PCB* process,PCB* tail) / 把就绪队列里的进程放入生产者队列的尾(*tail)-processlink=process;(*tail)=process; return ;void freelink(PCB* linkhead)/清除队列PCB* p;while(linkhead!=NULL)p=linkhead;linkhead=linkhead-processlink;free(p);return ;PCB* getq(PCB* head,PCB* tail)/出队PCB* p;p=head-processlink;if(p!=NULL)head-processlink=p-processlink;p-processlink=NULL; if( head-processlink =NULL )(*tail)=head;elsereturn NULL;return p;void linklist(PCB* p,PCB* listhead)PCB* cursor=listhead;while(cursor-processlink!=NULL)cursor=cursor-processlink;cursor-processlink=p;int processproc()/给PCB分配内存。int i,f,num;char ch;PCB*p=NULL;coutendlnum;for(i=0;inum;i+)/产生相应的的进程:cout:) 输入1生产者进程endl; cout:) 输入2消费者进程endl; scanf(%d,&f); getchar();p=(PCB*)malloc(sizeof(PCB);if(!p)cout:) 内存分配失败flag=f;/进程标志,1为生产者,2为消费者processnum+;/进程计数器加1p-numlabel=processnum;/进程编号记为进程计数器p-state=w;/置为等待p-processlink=NULL;if(p-flag=1)/输入1为生产者进程;cout:) 您要产生的进程是生产者,它是第processnum个进程endl;cout:) 请输入您要该进程产生的字符endlproduct=ch;productnum+;cout:) 该进程产生的内容是productendl;else /输入2为消费者进程;cout:) 产生的进程是消费者,它是第numlabel个进程endl;linkqueue(p,&readytail); /并把这些进程放入就绪队列中。return true;bool waitempty()/ 如果缓冲区满,该进程进入生产者等待队列;if(empty=0)cout:) 进程numlabel缓冲区存数,该进程进入生产者等待队列endl; linkqueue(exe,&producertail);/缓冲区满,进程压入生产者等待队列队尾return false;elseempty-;/缓冲区未满,则进行生产操作return true;void signalempty()/将等待中的生产者进程进入就绪队列PCB* p;if(hasElement(producerhead)p=getq(producerhead,&producertail);linkqueue(p,&readytail);cout:) 等待中的生产者进程进入就绪队列,它的进程号为numlabelendl;empty+;bool waitfull()/linkqueue(exe,&consumertail); /if(full=0)/如果缓冲区空cout:) 进程numlabel缓冲区取数,缓冲区空,该进程进入消费者等待队列endl;linkqueue(exe,&consumertail);/进程进入消费者等待队列return false;elsefull-;return true;void signalfull()/将等待中的消费者进程进入就绪队列PCB* p;if(hasElement(consumerhead)p=getq(consumerhead,&consumertail);linkqueue(p,&readytail);cout:) 等待中的消费者进程进入就绪队列,它的进程号是numlabelendl;full+;void producerrun()/进程运行if(!waitempty()/缓冲区为空return;/缓冲区不为空,执行消费者进程cout:) 进程numlabel开始向缓冲区存数productproduct;/放入缓冲区bufferpoint+;/缓冲区指针后移cout:) 进程numlabel向缓冲区存数操作结束endl;signalfull();/等待的消费队列进入就绪队列linklist(exe,over);void comsuerrun()if(!waitfull()/缓冲区不为空return;cout:) 进程numlabel开始向缓冲区取数product=bufferbufferpoint-1;bufferpoint-;cout:) 进程numlabel向缓冲区取数操作结束,取数是productprocesslink;while(p!=NULL)printf(:) 进程%d,它是一个,p-numlabel);p-flag=1? cout生产者endl:cout消费者processlink;void main() char c,ch;bool element;int Flag=1;couttt* 欢迎光临 *processlink=NULL;while(Flag) if(!processproc()/给PCB分配空间 break;element=hasElement(readyhead);while(element)exe=getq(readyhead,&readytail);printf(:) 进程%d申请运行,它是一个,exe-numlabel);exe-flag=1?cout生产者endl:cout消费者flag=1)producerrun();elsecomsuerrun(); element=hasElement(readyhead); cout:) 就绪队列没有进程endl;if(hasElement(consumerhead) cout:) 消费者等待队列中有进程:endl;display(consumerhead);else if(hasElement(producerhead) cout:) 生产者等待队列中有进程:endl;display(producerhead);else cout:) 生产者等待队列中没有进程endl;cout:) 想继续吗?(y/n)endl;c=getchar();ch=getchar();if(c=y | c=Y)Flag=1;else if(c=n | c=N)Flag=0;elseFlag=0;cout:) 输入有误endl;实验项目:银行家算法实验学时: 2实验日期:实验
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 院内桌子改造方案范本
- 工业流量计安装施工方案
- 化学硕士题目题库及答案
- 2025标准合同范本:版权转让协议国际版
- 军需保管考试题目及答案
- 物业工种培训方案范本
- 旅游口号评选方案范本
- 停车场线路施工方案模板
- 断裂带隧道施工方案编制
- 2025湖南郴州市郴投集智文化旅游运营有限公司招聘模拟试卷参考答案详解
- 兵团面试题目及答案
- 2025贵州贵阳市投资控股集团房地产置业有限公司招聘12人考试参考题库及答案解析
- 免疫细胞治疗安全性评价-第1篇-洞察及研究
- 车间师带徒管理办法
- 桥梁工程监理工作实施方案
- 2025年秋期新教材部编人教版一年级上册道德与法治教学计划+进度表
- 服装辅料基础知识培训
- 医院门诊急诊统筹管理方案
- 国家事业单位招聘2025农业农村部国际交流服务中心招聘拟聘用人员笔试历年参考题库附带答案详解
- 2025年AI技术在项目管理中的应用洞察报告
- 胃肠外科医生进修汇报
评论
0/150
提交评论