课程设计实验报告书--操作系统课程设计.docx

上传人:李司机 文档编号:6685864 上传时间:2023-12-27 格式:DOCX 页数:15 大小:88.61KB
返回 下载 相关 举报
课程设计实验报告书--操作系统课程设计.docx_第1页
第1页 / 共15页
课程设计实验报告书--操作系统课程设计.docx_第2页
第2页 / 共15页
课程设计实验报告书--操作系统课程设计.docx_第3页
第3页 / 共15页
课程设计实验报告书--操作系统课程设计.docx_第4页
第4页 / 共15页
课程设计实验报告书--操作系统课程设计.docx_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《课程设计实验报告书--操作系统课程设计.docx》由会员分享,可在线阅读,更多相关《课程设计实验报告书--操作系统课程设计.docx(15页珍藏版)》请在三一办公上搜索。

1、课程设计实验报告书科目:操作系统课程设计实验一:银行家算法1 .目的和要求银行家算法是一种避免死锁的重要方法,本实验要求用高级语言编写和调试一个简单的银行家算法。加深了解有关资源申请、避免死锁等概念,并体会了解死锁和避免死锁的具体实施方式。2 .实验内容1) .设计进程对各类资源最大申请表示及初值确定。2) .设定系统提供资源初始状况。3) .设定每次某个进程对各类资源的申请表示。4) .编制程序,依据银行家算法,决定其申请是否得到满足。3 .实验代码voidmain()(intmaxneed53=7,5,3,3,2,2,9,0,2,2,2,2,4,3,3;intallocation3=0,1

2、,0,2,0,0,3,0,2,2,1,1,0,0,2;intreq3;inti,j,k,1,c=0,count=0;intneed53=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;intresult5=-l,-1,-1,-1,-1;intwork3=3,3,2;printf(z,AllSources:nABCn1057n);printf(z,Avai!ableSources:nABCn332n);printf(z,Everyprocessmaxneedsources:nABCn);for(i=0;i5;i+)(printf(,P%d:z,i+l);for(j=0;j3;j+)(

3、printfC%dmaxneedij);needij=maxneedij-allocationij;printfnz,);for(1=0;15;1+)for(k=0;k5;k+)if(resuItk=-l&neeclk0=work0Mneedk1=work1Mneedk2zk+l);if(count=5)printf(z,11Itissafe!n);elseprintf(,11Itisdangerous,n,z);work0=3;work1=3;work2=2;printf(zzPleaseinputPlrequestsources:nz,);scanf(z,%d,%d,%d”,&req0,&

4、reql,&req2);if(req0=need00&req1=need01&req2=need02)printf(,Therequestisreasonable,11z,);elseprintf(,Therequestisbeyondneed.n);if(req0=work0&reql=work1&req2=work2)(work0=work0-req0;work1=work1-req1;work2=work2-req2;need00=need00-req0;need0l=need0lreql;need02=need02-req2;alIocation00=allocation00+req0

5、;allocation0l=allocation0l+reql;allocation02=allocation02+req2;/printf(z,%d%d%dz,work0,work1,work2);for(k=0;k5;k+)(resultk=-1;for(1=0;15;1+)for(k=0;k5;k+)(if(resu11k=-l&needk0=work0Mneedk1=work1&needk2,z,k+l);if(c=5)printf(zz11Itissafe!nz,);elseprintf(z,11Itisdangerous,nz,);4 .实验结果5 .结果分析Maxneed-req

6、uest的值是执行这个进程所还需要的资源,用need来表示WOrk数组表示分配的序列,如果need二WOrk表示这个进程可以被运行,就把进程所对应的result的值改成1,如果result中所有值都为L就生出一个正确的序列。实验二:时间片轮转算法1 .目的和要求加深对时间片大小不一样,影响处理机的开销的理解。2 .实验类容时间分配的多,执行的任务数量就大。所需要轮转的次数就少。用循环次数来模拟时间片,每个进程用一定大小的数字来表示,每当循环的时间片值变成。时,就切换进程,对应的数字就减去时间片大小的数字。没执行一次进程,就输出这个进程。假定进程是固定的几个,只需在创建一个进程队列,进程按fcf

7、s方式出对,和进对,直到进程中最大的数字的值变为0。我们用一个值Time来记录所有进程参加循环的次数,这个值如果越大,则表示处理机调度次数越多。选取不同的时间片,对于相同的几个进程,最后得出不同的程序执行时间Time,直观的知道不同的时间片,可能性能不一样。3 .实验代码T/*时间片轮转法进行CPU调度算法*/ftincludeftincludeftincludedefineN10定义最大进程数defineTIME2定义时间片大小typedefstructpcbcharid10;进程标识数intarrivetime;到达时间intruntime;进程已经占用的CPU时间intneedtime;

8、进程还需要的时间charstate12;进程运行状态:Waitorruningstructpcb*next;pcb,*PCB;PCBhead;设置全局变量用来修改就绪队列PCBtail;intCoUnt=0;记录就绪队列中进程数voidCreatProcess()创建进程PCBp,q;进程的头尾指针都有intnum;记录要创建的进程数inti,j;intarriveN;head=tai1=(PCB)malloc(sizeof(pcb);head-next=NULL;p=head;Printf(输入你要创建的进程数:);scanf(,%dz,&num);count=num;printf(*按照进

9、程到达时间从小到大创建就绪队列*n);初始对其排序来创建就绪队列for(i=l;inext=(PCB)malloc(sizeof(pcb);p=p-next;tail=p;Printf(输入进程%d的标示符:,i);SCanfp-id);Printf(输入进程%d的到达时间:”,i);scanf(%c,-arrivetime);Printf(输入进程%d己占用的cpu时间:i);scanf-runtime);Printf(输入进程%d还需要的cpu时间:i);scanf-needtime);Printf(输入进程%d当前状态:(run或者Wait):,i);SCanf(%s,p-state);

10、)tail-next=p-next=NULL;voidRR_RunProcess()运行进程,简单轮转法RoundRobinPCBp,q,temp;p=head-next;while(1)if(head-next=NULL)(Printf(此时就绪队列中已无进程!n);return;)else(while(p)if(p-needtime0)&!(strcmp(p-state,z,wait,)Printf(进程s开始,n”,p-id);strcpy(p-state,z,run,z);p-runtime+=TIME;p-needtime-=TIME;if(p-needtimeneedtime=O;

11、temp=p;把该时间片内运行完的进程存到临时temp中把temp接到链表尾部,销毁P;if(temp-needtimeO)/把该时间片内运行完的进程接到就绪队列的尾部if(countl)head-next=temp-next;tail-next=temp;tail=tail-next;strcpy(tail-state,wait);tail-next=NULL;elseif(count=。当只有一个进程等待时,分开讨论head-next=temp;tail=temp;strcpy(tail-state,z/wait/);tail-next=NULL;)if(temp-needtime=O)销

12、毁就绪队列中已经结束的进程CoUnt-;此时就绪队列中进程数减1Printf(进程s结束.n”,p-id);head-next=temp-next;free(temp);撤销就绪队列中已经结束的进程p=head-next;)voidmain()printf(*进程的初始状态!*n);CreatProcess();printf(*11tt程序运行结果如下:nn);printf(*n);RR_RunProcess();简单轮转法RoundRobin4实验结果-IU ects2Debg2.H - O卷 U u t 八 S 一 al八1CA Xt或Un UurI4 *4群皿( uur*a可 P pb可

13、 P p TA nA T TA n TA T n +入入入入入入入入入入E 程tiE 进On无C W 己to E 中y E 列ke F始始始始始束始束甜Oy f开开开开开结开结瓶a * ab ab a abbs F 程程程 付es 4注芯此芯芯注芯注此Pr TA TA K TA bTA、TA bTA、T TA TA5.结果分析这里的时间片是2。进程a需要5个处理机时间,需要3个时间片。进程b需要6个处理机时间,需要时间片3个。a、b进程交替执行,当a结束后,只剩下b进程了,当b进程执行完了时,所有进程都执行完毕。实验三:静态优先级调度算法1 .实验目的和要求进程是操作系统最重要的概念之一,进程

14、调度又是操作系统核心的重要类容。通过该实验,加深对静态抢占式优先级调度算法的理解。2 .实验类容创建进程,设置优先级。设置进程的状态(就绪,执行,阻塞)之一。设置信号量。编写程序。3 .实验代码通过按序分配资源预防死锁的初始化程序Pt=psorted-next;psorted=pt;)elseap-next-pt-next;)ap=pt;pt=pt-next;)if(psorted-next=NULL)break;getchar();)structPCB*SortList(PCB*HL)structPCB*SL;SL=(structPCB*)malloc(sizeof(structPCB);S

15、L=NULL;struct PCB* while (r != (struct PCB* struct PCB* struct PCB*r = HL; NULL)t = r-next;cp - SL;ap = NULL;while(cp!=NULL)if(r-p_prioritycp-p_priority)break;elseap=cp;cp=cp-next;)if(ap=NULL)r-next=SL;SL=r;)else(r-next=cp;ap-next-r;)r=t;)returnSL;)/HANDLEh_mutex_chopsticksMAX_PHILOSOPHERS;/互斥体数组,每根

16、筷子需要一个互斥体intthread_numberMAX_PHILOSOPHERS=1,2,3);/定义死锁的个数/会产生死锁的哲学家线程intdeadlock_phiIosopher(LPVOIDdata)intphilosopher-number=*(int*)(data);/哲学家编号for(;)(srand(unsigned)time(NULL)*(philosopher_number+1);Sleep(DELAY);printf(%s%c%s%cn,PhiIosopher”,ZERO+philosopher_number,iswaitingchopstick”,(ZERO+philo

17、sopher_number);WaitForSingleObject(h_mutex_chopsticksphilosopher_number,INFINITE);printf(zz%s%c%s%cnzPhilosopher”,ZERO+philosopher_number,“gotchopstick,(ZER(Hphilosophejnumber);Sleep(DELAY/4);printf(zz%s%c%s%cnzPhilosopher”,ZERO+philosopher_number,“iswaitingchopstick,(ZERO+(1+philosopher_number)%MAX

18、_PHILOSOPHERS);WaitForSingleObject(h_mutex_chopsticks(l+philosopher_number)%MAX_PHiLosophers),INFINITE);printf(zz%s%c%s%cnzPhilosopher”,ZERO+philosopher_number,“gotchopstick,(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);printf(zz%s%c%snz*PhiIosopher”,ZERO+phiIosopheJnUmber,“iseating.);Sleep(DELAY);

19、ReleaseMutex(h_mutex_chopsticksphilosopher-number);printf(%s%c%s%cn,PhiIosopher,ZERO+philosopher_number,releasedchopstick,ZERO+philosopher_number);Re1easeMutex(h_mutex_chopsticks(1+philosopher_number)%MAX_Philosophers);printf(%s%c%s%cn,PhiIosopher”,ZERO+philosopher_number,releasedchopstick,z,(ZERO+(

20、lphilosopher_number)%MAX_PHILOSOPHERS);Sleep(DELAY);)/endforreturnO;)/死锁时的初始化程序voiddeadlock()(charchoice;inti=0;HANDLEh_threadMAX_PHILOSOPHERS;printf(可能出现死锁的哲学家就餐问题n);for(i=o;imax_phiLosophers;i+)(h_mutex_chopsticksi=CreateMutex(NULL,FALSE,NULL););for(i=o;imax_phiLosophers;i+)(h_threadiI=CreateThrea

21、d(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_phiIosopher),&thread_numberi,O,NULL);;WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);do(choice=(char)getch();)while(choice!=2);system(z*clsz*);deadlock();Printf(nPressanykeytoreturntomainmenu.);getch();system(z*clsz*);)/通过按序分配资源预防死锁的哲学家线程intorder

22、ed_alIocation_phiIosopher(LPVOIDdata)(intphilosopher-number=*(int*)(data);for(;)(srand(unsigned)time(NULL)*(philosopher_number+1);Sleep(DELAY);if(philosopher_number=MAX_PHILOSOPHERS-1)printf(zz%s%c%s%cnzPhilosopher”,ZERO+philosopher_number,“iswaitingchopstick,(ZERO+(1+philosopher_number)%MAX_PHILOSO

23、PHERS);WaitForSingleObject(h_mutex_chopsticks(lphilosopher_number)%MAX_PHILOSOPHERS,INFINITE);Sleep(DELAY/4);printf(zz%s%c%s%cnzPhilosopher,ZERO+philosopher_number,iswaitingchopstick”,ZER(Hphilosophejnumber);WaitForSingleObject(h_mutex_chopsticksphilosopher_number,INFINITE);)elseprintf(zz%s%c%s%cnzP

24、hilosopher,ZERO+philosopher_number,iswaitingchopstick”,ZER(Hphilosophejnumber);WaitForSingleObject(h_mutex_chopsticksphilosopher_number,INFINITE);Sleep(DELAY/4);printf(zz%s%c%s%cnzPhilosopher,ZERO+philosopher_number,iswaitingchopstick,ZERO+(1+phi1osopher_number)%MAX_PHILOSOPHERS);WaitForSingleObject

25、(h_mutex_chopsticks(l+philosopher_number)%MAX_PHILOSOPHERS,INFINITE);)printf(zz%s%c%snzz,“Philosopher,ZERO+philosopher_number,“iseating,z);Sleep(DELAY);printf(zz%s%c%s%cnzPhilosopher,ZERO+philosopher_number,isreleasingchopstick”,ZER(Hphilosophejnumber);ReleaseMutex(h_mutex_chopsticksphilosopher-numb

26、er);printf(%s%c%s%cn,PhiIosopher”,ZERO+philosopher_number,isreleasingchopstick,z,ZERO+(lphilosopher_number)%MAX_PHILOSOPHERS);ReleaseMutex(h_mutex_chopsticks(l+philosopher_number)%MAX_Philosophers);Sleep(DELAY);)/endforreturnO;)voidorderedallocation()charchoice;inti=0;HANDLEh_threadMAX_PHILOSOPHERS;

27、printf(可能出现死锁的哲学家就餐问题nzz);for(i=0;iMAX_PHILOSOPHERS;i+)h_mutex_chopsticksi=CreateMutex(NULL,FALSE,NULL););for(i=;iMAX_PHiLosophers;i+)h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ordered_allocation_phiIosopher),&thread_numberi,O,NULL););WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE

28、,-1);do(choice=(char)getch();)while(choice!=,2,);SyStem(cis);deadlock();Printf(nPressanykeytoreturntomainmenu.);getch();SyStem(cis);4 .实验结果SHC:Windowssystem32cmd.exe进程数:3a 3 司一t :纺wl,z 程先达要 进优到箫进程名:b优先级:2到达时间:2需要运行时间:5进程名:C优先级:4到达时间:3需要运行时间:2请输入一个时间片的时间:2进程名 优先级到达时间a bl运行时间352己用CPU时间OOO状态 ready ready ready进程名优先级到达时间运行时间己用CPU时间状态进程名 优先级到达时间 运行时间状态 finish ready readyreadyreadyready己用CPU时间2OO5 .结果分析进程在执行时,先执行优先级高的,对进程到达的时间不做考虑。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号