《操作系统》实验报告看就没错(共27页).doc
精选优质文档-倾情为你奉上操作系统实验报告时间:2013年11月19日2013年12月11日地点:科技楼423室班级:计科班学号:姓名: 电话:上交时间:2013年12月11日注意:所有程序都应有一定的注释说明,用VC实现,附上实验结果。实验1:进程管理要求:编写程序,模拟实现创建新的进程;查看运行进程;换出某个进程;撤销某个进程。提示:1、进程状态简单处理为:0为不在内存,1为在内存,2为阻塞,3为挂起。2、撤销进程指将进程的状态从运行变为阻塞。3、程序的结构可以处理为在主函数中用switch语句调用各种表示进程管理功能的函数。#include "iostream.h"#include "windows.h"/#define N 3typedef structint ID;int PRIORITY;int CPUTIME;int ALLTIME;int STARTBLOCK;int BLOCKTIME;int STATE;/0-运行 1-阻塞 2-就绪 3-结束 4-未到达int REACH;int TIME;PROCESS;void textcolor (int color) SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color );void main()int i,time,max,l,l1,time1,flag=0,total=0,N,server10,sum=0;PROCESS pro10;textcolor(13);cout<<"注意:本程序中状态代表如下"<<endl<<"0-运行 1-阻塞 2-就绪 3-结束 4-未到达"<<endl<<endl;textcolor(15);cout<<"请输入进程数:"cin>>N;cout<<"请设置时间片长度:"cin>>time;cout<<"请输入各进程初始状态:"<<endl;cout<<"ID PRIORITY REACH ALLTIME STARTBLOCK BLOCKTIME"<<endl;for(i=0;i<N;i+)proi.CPUTIME=0;proi.TIME=0;cin>>proi.ID>>proi.PRIORITY>>proi.REACH;cin>>proi.ALLTIME>>proi.STARTBLOCK>>proi.BLOCKTIME;serveri=proi.ALLTIME;if(proi.REACH=0) proi.STATE=0;else proi.STATE=4;docout<<endl<<"当前时刻为:"<<total;textcolor(12);cout<<endl<<"=各进程状态为="<<endl;textcolor(15);cout<<"ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE"<<endl;for(i=0;i<N;i+)cout<<proi.ID<<" "<<proi.PRIORITY<<" "<<proi.CPUTIME<<" "cout<<proi.ALLTIME<<" "<<proi.STARTBLOCK<<" "<<proi.BLOCKTIME<<" "<<proi.STATE;cout<<endl;total+=time;for(i=0;i<N;i+)if(proi.STATE=4&&proi.REACH<total)proi.STATE=1;for(i=0;i<N;i+)time1=proi.ALLTIME;if(proi.STATE=0)if(proi.ALLTIME<=time)/proi.CPUTIME+=time1;proi.ALLTIME=0;proi.STATE=3;proi.TIME=total-time+time1;else/proi.CPUTIME+=time;proi.ALLTIME-=time;proi.STARTBLOCK-;if(proi.STARTBLOCK=0)proi.STATE=1;proi.BLOCKTIME=time1;proi.STARTBLOCK=time1;proi.PRIORITY-=3;proi.TIME=total;if(proi.STATE=1)proi.BLOCKTIME-;if(proi.BLOCKTIME=0) proi.STATE=2;proi.TIME=total;if(proi.STATE=2)/proi.CPUTIME+=time;proi.PRIORITY+;proi.TIME=total;max=-100;l1=-1;l=-1;for(i=0;i<N;i+)if(proi.PRIORITY>max&&(proi.STATE=0|proi.STATE=2)l=i;max=proi.PRIORITY;if(proi.STATE=0) l1=i;if(l!=-1&&l!=l1) prol.STATE=0;if(l1!=-1) prol1.STATE=2;flag=0;for(i=0;i<N;i+)if(proi.STATE!=3)flag=1;break;if(flag=0) break;while(1);cout<<endl<<"当前时刻:"<<total;textcolor(12);cout<<endl<<"=各进程状态为="<<endl;textcolor(15);cout<<"ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE"<<endl;for(i=0;i<N;i+)cout<<proi.ID<<" "<<proi.PRIORITY<<" "<<proi.CPUTIME<<" "cout<<proi.ALLTIME<<" "<<proi.STARTBLOCK<<" "<<proi.BLOCKTIME<<" "<<proi.STATE;cout<<endl;cout<<endl<<"各进程运行结束!"<<endl;cout<<"进程号 到达时间 结束时间 周转时间 带权周转时间"<<endl;textcolor(10);for(i=0;i<N;i+)cout<<" "<<proi.ID<<" "<<proi.REACH<<" "<<proi.TIME<<" "<<proi.TIME-proi.REACH<<" "<<(float)(proi.TIME-proi.REACH)/serveri<<endl;sum+=proi.TIME-proi.REACH;cout<<"平均周转时间为:"<<(float)sum/N<<endl;textcolor(15);实验2:进程调度要求:1、 设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转调度算法。2、 建立进程就绪队列。3、 编制两种进程调度算法:优先权调度(实现动态优先级)和时间片轮转调度。提示:1、 假设利用两种算法对五个进程进行调度,每个进程有运行、就绪、阻塞三种状态,初始状态为就绪态。2、 为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初值由用户给定。3、 在优先权调度算法中,优先数可以先取50,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。在时间片轮转调度算法中,采用固定时间片,即每执行一次进程,该进程的执行时间片数为已执行了2个单位,这时CPU时间片数加2,进程还需要的时间片数减2,并将该进程排列到就绪队列的队尾。4、 对于优先数一致的情况,采用FIFO策略解决。#include<iostream>#include<string>using namespace std;class Processpublic: string ProcessName; / 进程名字 int Time; / 进程需要时间 int leval; / 进程优先级 int LeftTime; / 进程运行一段时间后还需要的时间;/void Copy ( Process proc1, Process proc2); / 把proc2赋值给proc1void Sort( Process pr, int size) ; / 此排序后按优先级从大到小排列void sort1(Process pr, int size) ; / 此排序后按需要的cpu时间从小到大排列void Fcfs( Process pr, int num, int Timepice); / 先来先服务算法void TimeTurn( Process process, int num, int Timepice); / 时间片轮转算法void Priority( Process process, int num, int Timepice); / 优先级算法/void main() int TimePice; int num; cout<<" *创建进程*n"<<endl; cout<<" 输入进程个数:" cin>>num; const int Size =30; Process processSize ; for( int i=0; i< num; i+) string name; int CpuTime; int Leval; cout<<"n 输入第 "<< i+1<<" 个进程的名字、 运行时间和优先级 :"<<endl; cin>> name; cin>> CpuTime>> Leval; processi.ProcessName =name; processi.Time =CpuTime; processi.leval =Leval; cout<<endl; int a; cout<<endl; cout<<" *输入此进程时间片大小:*: " cin>>TimePice; cout<<"n *选择调度算法:*n"<<endl; cout<<"*"<<endl; cout<<"* 1: FCFS 2: 时间片轮换 3: 优先级调度 4: 最短作业优先 *"<<endl; cout<<"*"<<endl; cin>> a; for ( int k=0;k<num;k+) processk.LeftTime=processk.Time ;/对进程剩余时间初始化 cout<<" / 说明: 在本程序所列进程信息中, 优先级一项是指进程运行后的优先级 ! " cout<<endl; cout<<endl; if(a=1) Fcfs(process,num,TimePice); else if(a=2) TimeTurn( process, num, TimePice);else if(a=3) Sort( process, num); Priority( process , num, TimePice); else / 最短作业算法,先按时间从小到到排序,再调用Fcfs算法即可 sort1(process,num); Fcfs(process,num,TimePice); void Copy ( Process proc1, Process proc2) proc1.leval =proc2.leval ; proc1.ProcessName =proc2.ProcessName ; proc1.Time =proc2.Time ;void Sort( Process pr, int size) /以进程优先级高低排序 for( int i=1;i<size;i+) Process temp; temp = pri; int j=i; while(j>0 && temp.leval<prj-1.leval) prj = prj-1; j-; prj = temp; for( int d=size-1;d>size/2;d-) Process temp; temp=pr d; pr d = pr size-d-1; pr size-d-1=temp; void sort1 ( Process pr, int size) / 以进程时间从低到高排序/ 直接插入排序 for( int i=1;i<size;i+) Process temp; temp = pri; int j=i; while(j>0 && temp.Time < prj-1.Time ) prj = prj-1; j-; prj = temp; / 先来先服务算法的实现void Fcfs( Process process, int num, int Timepice) while(true) if(num=0) cout<<" 所有进程都已经执行完毕 !n"<<endl; exit(1); if(process0.LeftTime<=0) process0.LeftTime=0; cout<<" 进程 "<<process0.ProcessName<< " 已经执行完毕 !n"<<endl; for (int i=0;i<num;i+) processi=processi+1; num-; else if(processnum-1.LeftTime<=0) processnum-1.LeftTime=0; cout<<" 进程 "<<processnum-1.ProcessName<< " 已经执行完毕 !n"<<endl; num-; else cout<<endl; process0.LeftTime=process0.LeftTime- Timepice; if(process0.LeftTime<=0) process0.LeftTime=0; process0.leval =process0.leval-1; cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<process0.ProcessName <<" "<<process0.Time <<" " cout<<process0.LeftTime <<" "<<process0.leval<<" 运行"<<endl; for(int s=1;s<num;s+) if(processs.LeftTime<=0) processs.LeftTime=0;cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<processs.ProcessName <<" "<<processs.Time <<" " cout<<processs.LeftTime <<" "<<processs.leval<<" 等待 "<<endl; ; cout<<endl; system(" pause"); cout<<endl; / 时间片轮转调度算法实现void TimeTurn( Process process, int num, int Timepice) while(true) if(num=0) cout<<" 所有进程都已经执行完毕 !n"<<endl; exit(1); if(process0.LeftTime<=0) process0.LeftTime=0; cout<<" 进程 "<<process0.ProcessName<< " 已经执行完毕 !n"<<endl; for (int i=0;i<num;i+) processi=processi+1; num-; if( processnum-1.LeftTime <=0 ) processnum-1.LeftTime=0; cout<<" 进程 " << processnum-1.ProcessName <<" 已经执行完毕! n"<<endl; num-; else if(process0.LeftTime > 0) cout<<endl; /输出正在运行的进程 process0.LeftTime=process0.LeftTime- Timepice; if(process0.LeftTime<0) process0.LeftTime=0; process0.leval =process0.leval-1; cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<process0.ProcessName <<" "<<process0.Time <<" " cout<<process0.LeftTime <<" "<<process0.leval<<" 运行"<<endl; for(int s=1;s<num;s+) if(processs.LeftTime<0) processs.LeftTime=0;cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<processs.ProcessName <<" "<<processs.Time <<" " cout<<processs.LeftTime <<" "<<processs.leval; if(s=1) cout<<" 就绪 "<<endl; else cout<<" 等待 "<<endl; Process temp; temp = process0; for( int j=0;j<num;j+) processj = processj+1; processnum-1 = temp; else system(" pause"); cout<<endl; system(" pause"); cout<<endl; / 优先级调度算法的实现void Priority( Process process, int num, int Timepice) while( true) if(num=0) cout<< "所有进程都已经执行完毕!n"<<endl; exit(1); if(process0.LeftTime<=0) process0.LeftTime=0; cout<<" 进程 " << process0.ProcessName <<" 已经执行完毕! n"<<endl; for( int m=0;m<num;m+) processm = processm+1; /一个进程执行完毕后从数组中删除 num-; / 此时进程数目减少一个 if( num!=1 && processnum-1.LeftTime <=0 ) processnum-1.LeftTime=0; cout<<" 进程 " << processnum-1.ProcessName <<" 已经执行完毕! n"<<endl; num-; if(process0.LeftTime > 0) cout<<endl; /输出正在运行的进程 process0.LeftTime=process0.LeftTime- Timepice; if(process0.LeftTime<0)process0.LeftTime=0; process0.leval =process0.leval-1;cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<process0.ProcessName <<" "<<process0.Time <<" " cout<<process0.LeftTime <<" "<<process0.leval<<" 运行"<<endl; / 输出其他进程 for(int s=1;s<num;s+) if(processs.LeftTime<0)processs.LeftTime=0;cout<<"进程名字 "<<"共需占用CPU时间 "<<" 还需要占用时间 "<<" 优先级 "<<" 状态 "<<endl; cout<<" "<<processs.ProcessName <<" "<<processs.Time <<" " cout<<processs.LeftTime <<" "<<processs.leval ; if(s=1) cout<<" 就绪 "<<endl; else cout<<" 等待 "<<endl; / else Sort(process, num); cout<<endl; system(" pause"); cout<<endl; / while实验3:银行家算法要求:编制模拟银行家算法的程序,并以下面给出的例子验证所编写的程序的正确性。某系统有A、B、C、D4类资源共5个进程(P0、P1、P2、P3、P4)共享,各进程对资源的需求和分配情况如下表所示:进程已占资源最大需求数ABCDABCDP000000012P110001750P213542356P306320652P400140656现在系统中A、B、C、D4类资源分别还剩1、5、2、0个,请按银行家算法回答下列问题:1)现在系统是否处于安全状态?2)如果现在进程P1提出需要(0、4、2、0)个资源的请求,系统能否满足它的请求?提示:1、 假设进程P提出请求Requesti,则银行家算法按如下步骤进行判断。Step1:如果Requesti<=Needi,则转向Step2;否则,出错。Step2:如果Requesti<=Availablei,则转向Step3;否则,出错。Step3:系统试探分配相关资源,修改相关数据: Availablei= Availablei-Requesti, Allocationi=Allocationi+Requesti, Needi=Needi-RequestiStep4:系统执行安全性检查,如安全,则分配成立;否则试探性分配资源作废,系统恢复原状,进程进入等待状态。2、 利用安全性检查算法检查根据银行家算法进行资源分配后系统状态是否处于安全状态。具体算法如下:Step1:设置两个工作向量work=Available,finish=false;Step2:从进程集合中找到一个满足下述条件的进程; finish=false, Need<=work若能找到该进程,则执行Step3,否则,执行Step4。Step3:假设上述找到的进程获得资源,可顺利执行,直至完成,从而释放资源,做如下修改。 work=work+Allocation, finish=true, goto Step2;Step4:如果所有进程的finish=true,则表示该系统安全;否则系统不安全。#include<iostream.h>#include<string.h>#include<stdio.h>#define False 0#define True 1int Max100100=0;/各进程所需各类资源的最大需求int Avaliable100=0;/系统可用资源char name100=0;/资源的名称int Allocation100100=0;/系统已分配资源int Need100100=0;/还需要资源int Request100=0;/请求资源向量int temp100=0;/存放安全序列int Work100=0;/存放系统可提供资源int M=100;/作业的最大数为100int N=100;/资源的最大数为100void showdata()/显示资源矩阵 int i,j; cout<<"系统目前可用的资源Avaliable:"<<endl; for(i=0;i<N;i+) cout<<namei<<" " cout<<endl; for (j=0;j<N;j+) cout<<Avaliablej<<" "/输出分配资源 cout<<endl; cout<<" Max Allocation Need"<<endl; cout<<"进程名 " for(j=0;j<3;j+) for(i=0;i<N;i+) cout<<namei<<" " cout<<" " cout<<endl; for(i=0;i<M;i+) cout<<" "<<i<<" " for(j=0;j<N;j+) cout<<Maxij<<" " cout<<" " for(j=0;j<N;j+) cout<<Allocationij<<" " cout<<" " for(j=0;j<N;j+) cout<<Needij<<" " cout<<endl; int changdata(int i)/进行资源分配 int j;for (j=0;j<M;j+) Avaliablej=Avaliablej-Requestj; Allocationij=Allocationij+Requestj; Needij=Needij-Requestj;return 1;int safe()/安全性算法int i,k=0,m,apply,Finish100=0;int j;int flag=0;Work0=Avaliable0;Work1=Avaliable1;Work2=Avaliable2;for(i=0;i<M;i+) apply=0; for(j=0;j<N;j+) if (Finishi=False&&Needij<=Workj) apply+; if(apply=N) for(m=0;m<N;m+) Workm=Workm+Allocationim;/变分配数 Finishi=True; tempk=i; i=-1; k+;