課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書-操作系統(tǒng)課程設(shè)計(jì)_第1頁(yè)
課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書-操作系統(tǒng)課程設(shè)計(jì)_第2頁(yè)
課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書-操作系統(tǒng)課程設(shè)計(jì)_第3頁(yè)
課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書-操作系統(tǒng)課程設(shè)計(jì)_第4頁(yè)
課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書-操作系統(tǒng)課程設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告書科目:操作系統(tǒng)課程設(shè)計(jì)

實(shí)驗(yàn)一:銀行家算法目的和要求銀行家算法是一種避免死鎖的重要方法,本實(shí)驗(yàn)要求用高級(jí)語(yǔ)言編寫和調(diào)試一個(gè)簡(jiǎn)單的銀行家算法。加深了解有關(guān)資源申請(qǐng)、避免死鎖等概念,并體會(huì)了解死鎖和避免死鎖的具體實(shí)施方式。實(shí)驗(yàn)內(nèi)容1).設(shè)計(jì)進(jìn)程對(duì)各類資源最大申請(qǐng)表示及初值確定。2).設(shè)定系統(tǒng)提供資源初始狀況。3).設(shè)定每次某個(gè)進(jìn)程對(duì)各類資源的申請(qǐng)表示。4).編制程序,依據(jù)銀行家算法,決定其申請(qǐng)是否得到滿足。實(shí)驗(yàn)代碼 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");}實(shí)驗(yàn)結(jié)果結(jié)果分析Maxneed-request的值是執(zhí)行這個(gè)進(jìn)程所還需要的資源,用need來(lái)表示W(wǎng)ork數(shù)組表示分配的序列,如果need<=work表示這個(gè)進(jìn)程可以被運(yùn)行,就把進(jìn)程所對(duì)應(yīng)的result的值改成1,如果result中所有值都為1,就生出一個(gè)正確的序列。實(shí)驗(yàn)二:時(shí)間片輪轉(zhuǎn)算法目的和要求加深對(duì)時(shí)間片大小不一樣,影響處理機(jī)的開銷的理解。實(shí)驗(yàn)類容時(shí)間分配的多,執(zhí)行的任務(wù)數(shù)量就大。所需要輪轉(zhuǎn)的次數(shù)就少。用循環(huán)次數(shù)來(lái)模擬時(shí)間片,每個(gè)進(jìn)程用一定大小的數(shù)字來(lái)表示,每當(dāng)循環(huán)的時(shí)間片值變成0時(shí),就切換進(jìn)程,對(duì)應(yīng)的數(shù)字就減去時(shí)間片大小的數(shù)字。沒(méi)執(zhí)行一次進(jìn)程,就輸出這個(gè)進(jìn)程。假定進(jìn)程是固定的幾個(gè),只需在創(chuàng)建一個(gè)進(jìn)程隊(duì)列,進(jìn)程按fcfs方式出對(duì),和進(jìn)對(duì),直到進(jìn)程中最大的數(shù)字的值變?yōu)?。我們用一個(gè)值Time來(lái)記錄所有進(jìn)程參加循環(huán)的次數(shù),這個(gè)值如果越大,則表示處理機(jī)調(diào)度次數(shù)越多。選取不同的時(shí)間片,對(duì)于相同的幾個(gè)進(jìn)程,最后得出不同的程序執(zhí)行時(shí)間Time,直觀的知道不同的時(shí)間片,可能性能不一樣。實(shí)驗(yàn)代碼T/*****時(shí)間片輪轉(zhuǎn)法進(jìn)行CPU調(diào)度算法********/#include<stdio.h>#include<malloc.h>#include<string.h>#defineN10//定義最大進(jìn)程數(shù)#defineTIME2//定義時(shí)間片大小typedefstructpcb{charid[10];//進(jìn)程標(biāo)識(shí)數(shù)intarrivetime;//到達(dá)時(shí)間intruntime;//進(jìn)程已經(jīng)占用的cpu時(shí)間intneedtime;//進(jìn)程還需要的時(shí)間charstate[12];//進(jìn)程運(yùn)行狀態(tài):waitorruningstructpcb*next;}pcb,*PCB;PCBhead;//設(shè)置全局變量用來(lái)修改就緒隊(duì)列PCBtail;intcount=0;//記錄就緒隊(duì)列中進(jìn)程數(shù)voidCreatProcess(){//創(chuàng)建進(jìn)程PCBp,q;//進(jìn)程的頭尾指針都有intnum;//記錄要?jiǎng)?chuàng)建的進(jìn)程數(shù)inti,j;intarrive[N];head=tail=(PCB)malloc(sizeof(pcb));head->next=NULL;p=head;printf("輸入你要?jiǎng)?chuàng)建的進(jìn)程數(shù):");scanf("%d",&num);count=num;printf("********按照進(jìn)程到達(dá)時(shí)間從小到大創(chuàng)建就緒隊(duì)列******\n");//初始對(duì)其排序來(lái)創(chuàng)建就緒隊(duì)列for(i=1;i<=num;i++){p->next=(PCB)malloc(sizeof(pcb));p=p->next;tail=p;printf("輸入進(jìn)程%d的標(biāo)示符:",i);scanf("%s",p->id);printf("輸入進(jìn)程%d的到達(dá)時(shí)間:",i);scanf("%d",&p->arrivetime);printf("輸入進(jìn)程%d已占用的cpu時(shí)間:",i);scanf("%d",&p->runtime);printf("輸入進(jìn)程%d還需要的cpu時(shí)間:",i);scanf("%d",&p->needtime);printf("輸入進(jìn)程%d當(dāng)前狀態(tài):(run或者wait):",i);scanf("%s",p->state);}tail->next=p->next=NULL;}voidRR_RunProcess(){//運(yùn)行進(jìn)程,簡(jiǎn)單輪轉(zhuǎn)法RoundRobinPCBp,q,temp;p=head->next;while(1){if(head->next==NULL){printf("此時(shí)就緒隊(duì)列中已無(wú)進(jìn)程!\n");return;}else{while(p){if((p->needtime>0)&&!(strcmp(p->state,"wait"))){printf("進(jìn)程%s開始,\n",p->id);strcpy(p->state,"run");p->runtime+=TIME;p->needtime-=TIME;if(p->needtime<0)p->needtime=0;}temp=p;//把該時(shí)間片內(nèi)運(yùn)行完的進(jìn)程存到臨時(shí)temp中//把temp接到鏈表尾部,銷毀P;if(temp->needtime>0){//把該時(shí)間片內(nèi)運(yùn)行完的進(jìn)程接到就緒隊(duì)列的尾部if(count>1){head->next=temp->next;tail->next=temp;tail=tail->next;strcpy(tail->state,"wait");tail->next=NULL;}elseif(count==1){//當(dāng)只有一個(gè)進(jìn)程等待時(shí),分開討論head->next=temp;tail=temp;strcpy(tail->state,"wait");tail->next=NULL;}}if(temp->needtime==0){//銷毀就緒隊(duì)列中已經(jīng)結(jié)束的進(jìn)程count--;//此時(shí)就緒隊(duì)列中進(jìn)程數(shù)減1printf("進(jìn)程%s結(jié)束.\n",p->id);head->next=temp->next;free(temp);//撤銷就緒隊(duì)列中已經(jīng)結(jié)束的進(jìn)程}p=head->next;}}}}voidmain(){printf("**************進(jìn)程的初始狀態(tài)!**************\n");CreatProcess();printf("*******************************************\n\t\t程序運(yùn)行結(jié)果如下:\n\n");printf("*******************************************\n");RR_RunProcess();//簡(jiǎn)單輪轉(zhuǎn)法RoundRobin}4實(shí)驗(yàn)結(jié)果5.結(jié)果分析這里的時(shí)間片是2。進(jìn)程a需要5個(gè)處理機(jī)時(shí)間,需要3個(gè)時(shí)間片。進(jìn)程b需要6個(gè)處理機(jī)時(shí)間,需要時(shí)間片3個(gè)。a、b進(jìn)程交替執(zhí)行,當(dāng)a結(jié)束后,只剩下b進(jìn)程了,當(dāng)b進(jìn)程執(zhí)行完了時(shí),所有進(jìn)程都執(zhí)行完畢。實(shí)驗(yàn)三:靜態(tài)優(yōu)先級(jí)調(diào)度算法實(shí)驗(yàn)?zāi)康暮鸵筮M(jìn)程是操作系統(tǒng)最重要的概念之一,進(jìn)程調(diào)度又是操作系統(tǒng)核心的重要類容。通過(guò)該實(shí)驗(yàn),加深對(duì)靜態(tài)搶占式優(yōu)先級(jí)調(diào)度算法的理解。實(shí)驗(yàn)類容創(chuàng)建進(jìn)程,設(shè)置優(yōu)先級(jí)。設(shè)置進(jìn)程的狀態(tài)(就緒,執(zhí)行,阻塞)之一。設(shè)置信號(hào)量。編寫程序。實(shí)驗(yàn)代碼//通過(guò)按序分配資源預(yù)防死鎖的初始化程序{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];//互斥體數(shù)組,每根筷子需要一個(gè)互斥體intthread_number[MAX_PHILOSOPHERS]={1,2,3};//定義死鎖的個(gè)數(shù)//會(huì)產(chǎn)生死鎖的哲學(xué)家線程intdeadlock_philosopher(LPVOIDdata){intphilosopher_number=*(int*)(data);//哲學(xué)家編號(hào)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;}//死鎖時(shí)的初始化程序voiddeadlock(){charchoice;inti=0;HANDLEh_thread[MAX_PHILOSOPHERS];printf("可能出現(xiàn)死鎖的哲學(xué)家就餐問(wèn)題\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");}//通過(guò)按序分配資源預(yù)防死鎖的哲學(xué)家線程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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論