课程设计实验报告书-操作系统课程设计_第1页
课程设计实验报告书-操作系统课程设计_第2页
课程设计实验报告书-操作系统课程设计_第3页
课程设计实验报告书-操作系统课程设计_第4页
课程设计实验报告书-操作系统课程设计_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

课程设计实验报告书科目:操作系统课程设计

实验一:银行家算法目的和要求银行家算法是一种避免死锁的重要方法,本实验要求用高级语言编写和调试一个简单的银行家算法。加深了解有关资源申请、避免死锁等概念,并体会了解死锁和避免死锁的具体实施方式。实验内容1).设计进程对各类资源最大申请表示及初值确定。2).设定系统提供资源初始状况。3).设定每次某个进程对各类资源的申请表示。4).编制程序,依据银行家算法,决定其申请是否得到满足。实验代码 voidmain(){intmaxneed[5][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};intallocation[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};intreq[3];inti,j,k,l,c=0,count=0;intneed[5][3]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};intresult[5]={-1,-1,-1,-1,-1};intwork[3]={3,3,2};printf("AllSources:\nABC\n1057\n");printf("AvailableSources:\nABC\n332\n");printf("Everyprocessmaxneedsources:\nABC\n");for(i=0;i<5;i++){printf("P%d:",i+1);for(j=0;j<3;j++) {printf("%d",maxneed[i][j]);need[i][j]=maxneed[i][j]-allocation[i][j]; }printf("\n");}for(l=0;l<5;l++)for(k=0;k<5;k++) {if(result[k]==-1&&need[k][0]<=work[0]&&need[k][1]<=work[1]&&need[k][2]<=work[2]) {work[0]=work[0]+allocation[k][0];work[1]=work[1]+allocation[k][1];work[2]=work[2]+allocation[k][2];result[k]=l;count++;printf("P%d->",k+1); } }if(count==5)printf("\nItissafe!\n");elseprintf("\nItisdangerous.\n"); work[0]=3; work[1]=3; work[2]=2;printf("PleaseinputP1requestsources:\n");scanf("%d,%d,%d",&req[0],&req[1],&req[2]); if(req[0]<=need[0][0]&&req[1]<=need[0][1]&&req[2]<=need[0][2]) printf("Therequestisreasonable.\n"); else printf("Therequestisbeyondneed.\n"); if(req[0]<=work[0]&&req[1]<=work[1]&&req[2]<=work[2]) { work[0]=work[0]-req[0]; work[1]=work[1]-req[1]; work[2]=work[2]-req[2]; need[0][0]=need[0][0]-req[0]; need[0][1]=need[0][1]-req[1]; need[0][2]=need[0][2]-req[2];allocation[0][0]=allocation[0][0]+req[0]; allocation[0][1]=allocation[0][1]+req[1]; allocation[0][2]=allocation[0][2]+req[2]; } //printf("%d%d%d",work[0],work[1],work[2]); for(k=0;k<5;k++) {result[k]=-1; } for(l=0;l<5;l++)for(k=0;k<5;k++) {if(result[k]==-1&&need[k][0]<=work[0]&&need[k][1]<=work[1]&&need[k][2]<=work[2]) {work[0]=work[0]+allocation[k][0];work[1]=work[1]+allocation[k][1];work[2]=work[2]+allocation[k][2];result[k]=l;c++;printf("P%d->",k+1); } }if(c==5)printf("\nItissafe!\n");elseprintf("\nItisdangerous.\n");}实验结果结果分析Maxneed-request的值是执行这个进程所还需要的资源,用need来表示Work数组表示分配的序列,如果need<=work表示这个进程可以被运行,就把进程所对应的result的值改成1,如果result中所有值都为1,就生出一个正确的序列。实验二:时间片轮转算法目的和要求加深对时间片大小不一样,影响处理机的开销的理解。实验类容时间分配的多,执行的任务数量就大。所需要轮转的次数就少。用循环次数来模拟时间片,每个进程用一定大小的数字来表示,每当循环的时间片值变成0时,就切换进程,对应的数字就减去时间片大小的数字。没执行一次进程,就输出这个进程。假定进程是固定的几个,只需在创建一个进程队列,进程按fcfs方式出对,和进对,直到进程中最大的数字的值变为0。我们用一个值Time来记录所有进程参加循环的次数,这个值如果越大,则表示处理机调度次数越多。选取不同的时间片,对于相同的几个进程,最后得出不同的程序执行时间Time,直观的知道不同的时间片,可能性能不一样。实验代码T/*****时间片轮转法进行CPU调度算法********/#include<stdio.h>#include<malloc.h>#include<string.h>#defineN10//定义最大进程数#defineTIME2//定义时间片大小typedefstructpcb{charid[10];//进程标识数intarrivetime;//到达时间intruntime;//进程已经占用的cpu时间intneedtime;//进程还需要的时间charstate[12];//进程运行状态:waitorruningstructpcb*next;}pcb,*PCB;PCBhead;//设置全局变量用来修改就绪队列PCBtail;intcount=0;//记录就绪队列中进程数voidCreatProcess(){//创建进程PCBp,q;//进程的头尾指针都有intnum;//记录要创建的进程数inti,j;intarrive[N];head=tail=(PCB)malloc(sizeof(pcb));head->next=NULL;p=head;printf("输入你要创建的进程数:");scanf("%d",&num);count=num;printf("********按照进程到达时间从小到大创建就绪队列******\n");//初始对其排序来创建就绪队列for(i=1;i<=num;i++){p->next=(PCB)malloc(sizeof(pcb));p=p->next;tail=p;printf("输入进程%d的标示符:",i);scanf("%s",p->id);printf("输入进程%d的到达时间:",i);scanf("%d",&p->arrivetime);printf("输入进程%d已占用的cpu时间:",i);scanf("%d",&p->runtime);printf("输入进程%d还需要的cpu时间:",i);scanf("%d",&p->needtime);printf("输入进程%d当前状态:(run或者wait):",i);scanf("%s",p->state);}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->needtime>0)&&!(strcmp(p->state,"wait"))){printf("进程%s开始,\n",p->id);strcpy(p->state,"run");p->runtime+=TIME;p->needtime-=TIME;if(p->needtime<0)p->needtime=0;}temp=p;//把该时间片内运行完的进程存到临时temp中//把temp接到链表尾部,销毁P;if(temp->needtime>0){//把该时间片内运行完的进程接到就绪队列的尾部if(count>1){head->next=temp->next;tail->next=temp;tail=tail->next;strcpy(tail->state,"wait");tail->next=NULL;}elseif(count==1){//当只有一个进程等待时,分开讨论head->next=temp;tail=temp;strcpy(tail->state,"wait");tail->next=NULL;}}if(temp->needtime==0){//销毁就绪队列中已经结束的进程count--;//此时就绪队列中进程数减1printf("进程%s结束.\n",p->id);head->next=temp->next;free(temp);//撤销就绪队列中已经结束的进程}p=head->next;}}}}voidmain(){printf("**************进程的初始状态!**************\n");CreatProcess();printf("*******************************************\n\t\t程序运行结果如下:\n\n");printf("*******************************************\n");RR_RunProcess();//简单轮转法RoundRobin}4实验结果5.结果分析这里的时间片是2。进程a需要5个处理机时间,需要3个时间片。进程b需要6个处理机时间,需要时间片3个。a、b进程交替执行,当a结束后,只剩下b进程了,当b进程执行完了时,所有进程都执行完毕。实验三:静态优先级调度算法实验目的和要求进程是操作系统最重要的概念之一,进程调度又是操作系统核心的重要类容。通过该实验,加深对静态抢占式优先级调度算法的理解。实验类容创建进程,设置优先级。设置进程的状态(就绪,执行,阻塞)之一。设置信号量。编写程序。实验代码//通过按序分配资源预防死锁的初始化程序{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));SL=NULL;structPCB*r=HL;while(r!=NULL){structPCB*t=r->next;structPCB*cp=SL;structPCB*ap=NULL;while(cp!=NULL){if(r->p_priority>cp->p_priority)break;else{ap=cp;cp=cp->next;}}if(ap==NULL){r->next=SL;SL=r;}else{r->next=cp;ap->next=r;}r=t;}returnSL;}//HANDLEh_mutex_chopsticks[MAX_PHILOSOPHERS];//互斥体数组,每根筷子需要一个互斥体intthread_number[MAX_PHILOSOPHERS]={1,2,3};//定义死锁的个数//会产生死锁的哲学家线程intdeadlock_philosopher(LPVOIDdata){intphilosopher_number=*(int*)(data);//哲学家编号for(;;){srand((unsigned)time(NULL)*(philosopher_number+1));Sleep(DELAY);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",(ZERO+philosopher_number));WaitForSingleObject(h_mutex_chopsticks[philosopher_number],INFINITE);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"gotchopstick",(ZERO+philosopher_number));Sleep(DELAY/4);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS));WaitForSingleObject(h_mutex_chopsticks[((1+philosopher_number)%MAX_PHILOSOPHERS)],INFINITE);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"gotchopstick",(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS));printf("%s%c%s\n","Philosopher",ZERO+philosopher_number,"iseating.");Sleep(DELAY);ReleaseMutex(h_mutex_chopsticks[philosopher_number]);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"releasedchopstick",ZERO+philosopher_number);ReleaseMutex(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS]);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"releasedchopstick",(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS));Sleep(DELAY);}//endforreturn0;}//死锁时的初始化程序voiddeadlock(){charchoice;inti=0;HANDLEh_thread[MAX_PHILOSOPHERS];printf("可能出现死锁的哲学家就餐问题\n");for(i=0;i<MAX_PHILOSOPHERS;i++){h_mutex_chopsticks[i]=CreateMutex(NULL,FALSE,NULL);};for(i=0;i<MAX_PHILOSOPHERS;i++){h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_philosopher),&thread_number[i],0,NULL);};WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);do{choice=(char)getch();}while(choice!='2');system("cls");deadlock();printf("\nPressanykeytoreturntomainmenu.");getch();system("cls");}//通过按序分配资源预防死锁的哲学家线程intordered_allocation_philosopher(LPVOIDdata){intphilosopher_number=*(int*)(data);for(;;){srand((unsigned)time(NULL)*(philosopher_number+1));Sleep(DELAY);if(philosopher_number==MAX_PHILOSOPHERS-1){printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS));WaitForSingleObject(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS],INFINITE);Sleep(DELAY/4);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",ZERO+philosopher_number);WaitForSingleObject(h_mutex_chopsticks[philosopher_number],INFINITE);}else{printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",ZERO+philosopher_number);WaitForSingleObject(h_mutex_chopsticks[philosopher_number],INFINITE);Sleep(DELAY/4);printf("%s%c%s%c\n","Philosopher",ZERO+philosopher_number,"iswaitingchopstick",ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);WaitForSingleObject(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS],INFINITE);}printf("%s%c%s\n","Philosopher

温馨提示

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

评论

0/150

提交评论