




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验一 进程调度【目的要求】用高级语言编写和调试一个进程调度程序,以加深对进程的概念及进程调度算法的理解。【准备知识】1基本概念(1)进程的概念。(2)进程的状态和进程控制块。(3)进程调度算法。2进程调度(1)进程的状态。进程状态的转换如图10.1所示。运行就绪阻塞进程因某事件(如等待I/O完成)变成阻塞状态某事件被解除(I/O完成)时间片已用完进程调度程序把处理机分配给进程图10.1(2)进程的结构PCB。进程都是由一系列操作(动作)所组成,通过这些操作来完成其任务。因此,不同的进程,其内部操作也不相同。在操作系统中,描述一个进程除了需要程序和私有数据外,最主要的
2、是需要一个与动态过程相联系的数据结构,该数据结构用来描述进程的外部特性(名字、状态等)以及与其他进程的联系(通信关系)等信息,该数据结构称为进程控制块(Process Control Block,PCB)。进程控制块(PCB)与进程一一对应,PCB中记录了系统所需的全部信息、用于描述进程情况所需的全部信息和控制进程运行所需的全部信息。因此,系统可以通过进程的PCB来对进程进行管理。【实验内容】设计一个有N个进程并行的进程调度程序。 进程调度算法:采用最高优先数优先的调度算法(即把处理机分配给优先数最高的进程)和先来先服务算法。每个进程由一个进程控制块(PCB)表示。进程控制块可以包含如下信息:
3、进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等。进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。进程的到达时间为进程输入的时间。进程的运行时间以时间片为单位进行计算。每个进程的状态可以是就绪W(Wait)、运行R(Run)或完成F(Finish)三种状态之一。就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。如果运行一个时间片后,进程的已占用 CPU时间已达到所需要的运行时间,则撤销该进程,如果运行一个时间片后进程的已占用CPU时间还未到达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把
4、它插入就绪队列等待CPU。每进行一次调度程序都打印一次运行进程、就绪队列以及各个进程的PCB,以便进行检查。重复以上过程,直到所有进程都完成为止。调度算法的流程图如图10.2所示。开始初始化PCB,输入进程信息各进程按优先数从高到低排列结束就绪队列空?YN就绪队列首进程投入运行时间片到,运行进程已占用CPU时间+1已到达运行进程已占用CPU时间已达到所需的运行时间进程完成撤销该进程使运行进程的优先数减1把运行进程插入就绪队列图10.2 算法流程图进程调度源程序如下:jinchengdiaodu.cpp #include "stdio.h" #include <stdl
5、ib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type) #define NULL 0 struct pcb /* 定义进程控制块PCB */ char name10; char state; int super; int ntime; int rtime; struct pcb* link; *ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ PCB *first, *secon
6、d; int insert=0; if(ready=NULL)|(p->super)>(ready->super) /*优先数最大者插入队首*/ p->link=ready; ready=p; else /* 进程比较优先数,插入适当的位置中*/ first=ready; second=first->link; while(second!=NULL) if(p->super)>(second->super) /*若插入进程比当前进程优先数大,*/ /*插入到当前进程前面*/ p->link=second; first->link=p;
7、 second=NULL; insert=1; else /* 插入进程优先数最低,则插入到队尾*/ first=first->link; second=second->link; if(insert=0) first->link=p; input() /* 建立进程控制块函数*/ int i,num; clrscr(); /*清屏*/ printf("n 请输入进程号?"); scanf("%d",&num); for(i=0;i<num;i+) printf("n 进程号No.%d:n"
8、,i); p=getpch(PCB); printf("n 输入进程名:"); scanf("%s",p->name); printf("n 输入进程优先数:"); scanf("%d",&p->super); printf("n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("n"); p->rtime=0;p->state='w' p->link
9、=NULL; sort(); /* 调用sort函数*/ int space() int l=0; PCB* pr=ready; while(pr!=NULL) l+; pr=pr->link; return(l); disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ printf("n qname t state t super t ndtime t runtime n"); printf("|%st",pr->name); printf("|%ct",pr->state); printf(&
10、quot;|%dt",pr->super); printf("|%dt",pr->ntime); printf("|%dt",pr->rtime); printf("n"); check() /* 建立进程查看函数 */ PCB* pr; printf("n * 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("n *当前就绪队列状态为:n"); /*显示就绪队列状态*/ while
11、(pr!=NULL) disp(pr); pr=pr->link; destroy() /*建立进程撤销函数(进程运行结束,撤销进程)*/ printf("n 进程 %s 已完成.n",p->name); free(p); running() /* 建立进程就绪函数(进程运行时间到,置就绪状态)*/ (p->rtime)+; if(p->rtime=p->ntime) destroy(); /* 调用destroy函数*/ else (p->super)-; p->state='w' sort(); /*调用sort
12、函数*/ main() /*主函数*/ int len,h=0; char ch; input(); len=space(); while(len!=0)&&(ready!=NULL) ch=getchar(); h+; printf("n The execute number:%d n",h); p=ready; ready=p->link; p->link=NULL; p->state='R' check(); running(); printf("n 按任一键继续."); ch=getchar();
13、 printf("nn 进程已经完成.n"); ch=getchar(); 实验二 作业调度【目的要求】用高级语言编写和调试一个或多个作业调度的模拟程序,以加深对作业调度算法的理解。 【准备知识】1基本概念(1)作业的概念。(2)作业调度的功能。(3)作业调度算法,调度性能的衡量。2作业调度(1)作业的状态。如图10.3所示。作业调度作业调度作业录入提交后备执行等待就绪运行完成图10.3 作业的状态变迁(2)作业调度算法。1)先来先服务算法是按照作业到来的先后次序进行调度的。2)短作业优先算法是比较作业申请中所指出的执行时间,选取执行时间最短的作业作为下一次服务的对象。【实
14、验内容】作业调度和进程调度结合在一起(1)作业流信息从指定文本文件(TXT文件)中读取。作业信息包括:作业号、进入系统时间、估计运行时间、优先级、内存需求量、磁带机需求量,都为整型。(2)作业调度算法:先来先服务;最短作业优先(二者选一);进程调度算法:先来先服务;基于优先级的算法(静态优先级)(二者选一)。(3)输出作业序列。格式:作业号 时间间隔 如:1 800-810 (/* 8:00-8:10 */) 2 810-900 1 900-930 平均周转时间:总的周转时间/作业总数(周转时间就是作业结束时间减去作业进入系统时间)。示例:#include <stdio.h> #i
15、nclude <string.h> #include <process.h> #include <stdlib.h> #include <conio.h> #define null 0 #define len sizeof(struct jnote) struct jcb int state; int num; int in; int run; int pri; int mem; int tape; job50; struct jnote int id; int in; int start; int run; int end; int pri;
16、int size; int tape; int *maddr; struct jnote *next; ; int rest=4,memory101,*mh=memory,logo=0,fid=0; struct jcb *p=job; struct jnote *jh=null,*rp=null,*jp=null; txt()/*从txt文件中读取作业流*/ FILE *fp; char pt; int i,space=0,j=0,data100,h,k,count; char str10; for(i=0;i<100;i+) datai=-1; for(i=0;i<20;i+)
17、 jobi.num=-1; jobi.tape=-1; jobi.state=-1; i=0; fp=fopen("job.txt","r+"); if(fp=NULL) printf("Cann't the filen"); exit(0); while(pt=getc(fp)!=EOF) if(pt>='0'&&pt<='9') stri=pt; i+; space=0; else if(pt=' '|pt='n') if(spac
18、e=1) continue; else stri='0' dataj=atoi(str); j+; i=0; space=1; for(h=0,k=0;datak!=-1;k+,h+) jobh.num=datak;k+; jobh.in=datak;k+; jobh.run=datak;k+; jobh.pri=datak;k+; jobh.mem=datak;k+; jobh.tape=datak; if(jobh-1.tape=-1) stri='0' jobh-1.tape=atoi(str); clrscr(); for(i=0;jobi.num!=-
19、1;i+); return(i); rpend(start,run) /* 计算进程的结束时间 */ int start, run; int end=0; int i=start%100+run; end=(start/100+i/60)*100+i%60; return(end); time_time(end,in)/*计算周转时间或计算剩余的运行时间*/ int in,end; int time; time=end/100*60+end%100-(in/100*60+in%100); return(time); int *m_pd(int size)/*内存判断*/ int *mp,*cp
20、; int i=0; mp=cp=mh; while(*mp!=-1) while(*cp=0) cp+;i+; if(i>=size) return(mp); while(*cp=1) cp+; mp=cp; return(null); zy_div_free(mp,msize,tape,h)/*资源分配与释放*/ int *mp,msize,tape,h; int *cp,i=msize; cp=mp; if(h=1) for(;i>0;i-) *cp=1; cp+; rest=rest-tape; return (1); if(h=2) for(;i>0;i-) *cp
21、=0;cp+; rest=rest+tape; return (2); selectrp(plogo,time)/*选择当前运行进程*/ int plogo,time; struct jnote *newj; struct jnote *temp; if(jh=null&&rp=null) p=job; for(;p->state=0;) p+; zy_div_free(mh,p->mem,p->tape,1); p->state=0; newj=(struct jnote *)malloc(len); rp=newj; rp->id=p->
22、num; rp->in=p->in; rp->start=p->in; rp->run=p->run; rp->end=0; rp->pri=p->pri;rp->size=p->mem; rp->tape=p->tape; rp->maddr=mh; rp->next=null; return (0); else if(jh!=null&&rp=null) rp=jh; jh=jh->next; rp->next=null; rp->start=time; if(plo
23、go=2) selectrp(plogo,time); else return (-1); else if(jh!=null&&rp!=null) if(plogo=2) if(logo=0) if(jh->pri>rp->pri) temp=jh; jh=jh->next; temp->next=rp; printf("%d : %d - %d n",rp->id,rp->start,temp->in); rp->run-=time_time(temp->in,rp->start); rp-
24、>start=0; rp=temp; rp->start=rp->in; return (0); if(fid=1) if(jh->pri>rp->pri) temp=jh; jh=jh->next; temp->next=rp; rp=temp; rp->start=time; selectrp(plogo,time); else rp->start=time; return (0); return (0); else rp->start=time; return (0); return (0); WORK(jlogo,plo
25、go,count) int jlogo,plogo,count; int k=count,sum=0,time,t;/*sum是周转时间之和*/ struct jnote *cp,*p1,*newj; int *mp; selectrp(plogo,0); while(k>0) p=job; do if(p->state=0) p+; while(p->state=-1&&p->num!=-1&&(logo=0&&p->in<rpend(rp-> start,rp->run)|(logo=1&
26、;&p->in<=rpend(rp->start,rp->run) mp=m_pd(p->mem); if(rest>=p->tape) t=1; else t=0; if(mp!=null&&t=1) zy_div_free(mp,p->mem,p->tape,1); p->state=0; newj=(struct jnote *)malloc(len); newj->id=p->num; newj->in=p->in; newj->start=0; newj->run=
27、p->run; newj->end=0; newj->pri=p->pri; newj->size=p->mem; newj->tape=p->tape; newj->maddr=mp; newj->next=null; if(jh=null) jh=newj; jp=jh; else if (jlogo=1) jp->next=newj;jp=newj; /*作业为FCFS*/ else /*作业为最短运行时间优先*/ jp=cp=jh; if(newj->run>=jp->run&&jp-&
28、gt;next!=null) cp=jp; jp=jp->next; else if(newj->run<jp->run) if(cp=jp) newj->next=jp; jh=newj; else cp->next=newj; newj->next=jp; else jp->next=newj; if (plogo=2) selectrp(plogo,0); p+; while(p->state=0); if (logo=0) zy_div_free(rp->maddr,rp->size,rp->tape,2); lo
29、go=1; else rp->end=rpend(rp->start,rp->run); /*此时计算结束时间*/ printf("%d : %d - %d n",rp->id,rp->start,rp->end); sum+=time_time(rp->end,rp->in);/*计算周转时间总和*/ if(k-1=0) p1=rp;rp=rp->next;free(p1); break; time=rp->end; p1=rp; rp=rp->next; free(p1); k-; fid=1; sele
30、ctrp(plogo,time); fid=0; logo=0; printf("The average time : %d n",sum/count); init() int i; for(i=0;jobi.num!=-1;i+) jobi.state=-1; logo=0;fid=0; main() int i,count; count=txt();/*返回作业总数*/ for(i=0;i<100;i+) memoryi=0;memoryi=-1;mh=memory;/*内存清0,处于 未分配状态,最后一个用于标识*/ printf("Job_Proce
31、ssnn"); printf("FCFS_FCFS :n"); WORK(1,1,count);/*作业FCFS, 进程FCFS*/ init(); printf("nSHORT_FCFS :n"); WORK(2,1,count);/*作业最短运行时间优先,进程FCFS*/ printf("nnPlease press keyboard to see the result of process is PRI "); getchar(); clrscr(); init(); printf("Job_Processn
32、"); printf("nFCFS_PRI :n"); WORK(1,2,count); init(); printf("nSHORT_PRI :n"); WORK(2,2,count); getchar(); 实验三 存储管理【目的要求】通过编写和调试存储管理的模拟程序以加深对存储管理方案的理解。熟悉虚存管理的各种页面淘汰算法。【准备知识】1基本概念(1)存储管理的基本功能。(2)分区存储的基本概念和分配方法。(3)分页存储的基本概念和页面置换算法。2存储管理(1)存储管理的功能:内存的分配和回收、地址重定位、内存保护和虚拟内存(2)页面置换
33、算法。1)OPT算法是从内存中移出以后不再使用的页面;如无这样的页面,则选择以后最长时间内不需要访问的页。2)FIFO算法总是先淘汰那些驻留在内存时间最长的页面,即先进入内存的页面先被置换掉。3)LRU算法是如果某一页被访问了,那么它很可能马上又被访问;反之,如果某一页很长时间没有被访问,那么最近也不太可能会被访问(3)缺页率。缺页率为某段时间内,缺页中断次数与页面走向次数之比。【实验内容】题目要求:(1)实现三种算法:先进先出;OPT;LRU。(2)页面序列从指定的文本文件(TXT文件)中取出。(3)输出:第一行:每次淘汰的页面号;第二行:显示缺页的总次数。示例:#include <s
34、tdio.h> #include <stdlib.h> #include <malloc.h> #define null 0 #define len sizeof(struct page) struct page int num; int tag; struct page *next; ; struct page *create(int n) /*建立分配的内存空间,并初始化,返回头结点*/ int count=1; struct page *p1,*p2,*head; head=p2=p1=(struct page *)malloc(len); p1->t
35、ag=-1;p1->num=-1; while(count<n) count+; p1=(struct page *)malloc(len); p1->tag=-1;p1->num=-1; p2->next=p1; p2=p1; p2->next=null; return(head); void FIFO(array,n) int array,n; int *p; struct page *cp,*dp,*head,*new; int count=0; head=create(n); p=array; while(*p!=-1) cp=dp=head; fo
36、r(;cp->num!=*p&&cp->next!=null;) cp=cp->next; if (cp->num=*p) printf(" ! " ); else count+; cp=head; for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag=-1) cp->num=*p; cp->tag=0; printf(" * "); else new=(struct page*)malloc(len
37、); new->num=*p; new->tag=0; new->next=null; cp->next=new; head=head->next; printf(" %d ",dp->num); free(dp); p+; printf("nQueye Zongshu : %d n",count); void LRU(array,n) int array,n; int count=0,*p=array; struct page *head,*cp,*dp,*rp,*new,*endp; head=create(n);
38、 while(*p!=-1) cp=dp=rp=endp=head; for(;endp->next!=null;) endp=endp->next; for(;cp->num!=*p&&cp->next!=null;) rp=cp;cp=cp->next; if(cp->num=*p) printf(" ! "); if(cp->next!=null) if(cp!=head) rp->next=cp->next; else head=head->next; endp->next=cp; c
39、p->next=null; else count+; cp=rp=head; for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag=-1) printf(" * "); cp->num=*p; cp->tag=0; else new=(struct page *)malloc(len); new->num=*p; new->tag=0; new->next=null; cp->next=new; dp=head; head=hea
40、d->next; printf(" %d ",dp->num); free(dp); p+; printf("nQueye Zongshu : %d n",count); OPT(array,n) int array,n; int *p,*q,count=0,i; struct page *head,*cp,*dp,*new; p=array; head=create(n); while(*p!=-1) cp=head; for(;cp->num!=*p&&cp->next!=null;) cp=cp->ne
41、xt; if(cp->num!=*p) count+; cp=head; for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag=-1) printf(" * "); cp->num=*p; cp->tag=0; else i=1;q=p;q+;cp=head; while(*q!=-1&&i<n) for(;*q!=cp->num&&cp->next!=null;) cp=cp->next; if(
42、*q=cp->num) cp->tag=1; i+; q+;cp=head; if(i=n) for(;cp->tag!=0;) cp=cp->next; printf(" %d ",cp->num); cp->num=*p; else cp=head; for(;cp->tag!=0;) cp=cp->next; if(cp=head) for(;cp->next!=null;) cp=cp->next; new=(struct page *)malloc(len); new->num=*p; new->tag=0; new->next=null; cp->next=new; dp=head; head=head->next; printf(" %d ",dp->num); free(dp); else printf(" %d ",cp->num); cp->num=*p; cp=h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 冠心病患者非心脏手术麻醉管理专家共识
- 甘肃省白银市育才中学2024届中考二模数学试题含解析
- 广东东莞中堂六校2024年中考数学全真模拟试卷含解析
- 25年公司员工安全培训考试试题【满分必刷】
- 2024-2025企业员工岗前安全培训考试试题及答案【易错题】
- 2025公司管理人员安全培训考试试题附完整答案(各地真题)
- 2025年工厂安全培训考试试题及答案【全优】
- 2025厂级安全培训考试试题(审定)
- 2025年新员工岗前安全培训考试试题(往年题考)
- 2025年中国瓷砖粘合剂行业市场占有率及投资前景预测分析报告
- 《精子战争》作者罗宾·贝克
- 收费站特情处理培训
- 《防癌抗癌专题》课件
- 【MOOC】考古发现与中国文化-浙江大学 中国大学慕课MOOC答案
- (PPAP)生产件批准作业指导书
- 常用动脉穿刺术小讲课护理课件
- 2024年高考真题-化学(天津卷) 含解析
- 房屋过户协议书范文五份
- 陶瓷工艺技术研究试题考核试卷
- 铲车维护保养管理制度
- 公共卫生工作人员绩效考核评价细则
评论
0/150
提交评论