操作系統(tǒng)哲學(xué)家就餐問題課程設(shè)計(jì)C語言_第1頁
操作系統(tǒng)哲學(xué)家就餐問題課程設(shè)計(jì)C語言_第2頁
操作系統(tǒng)哲學(xué)家就餐問題課程設(shè)計(jì)C語言_第3頁
操作系統(tǒng)哲學(xué)家就餐問題課程設(shè)計(jì)C語言_第4頁
操作系統(tǒng)哲學(xué)家就餐問題課程設(shè)計(jì)C語言_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

題目:用多線程同步方法解決哲學(xué)家就餐問題(Dining-PhilosophersProblem)初始條件:1.操作系統(tǒng):Linux2.程序設(shè)計(jì)語言:C語言3.共有5個(gè)哲學(xué)家需用餐。只許4個(gè)哲學(xué)家入席且桌上有5支筷子。要求完成的主要任務(wù):(包括課程設(shè)計(jì)工作量及其技術(shù)要求,以及說明書撰寫等具體要求)1.技術(shù)要求:1) 為每個(gè)哲學(xué)家產(chǎn)生一個(gè)線程,設(shè)計(jì)正確的同步算法2) 每個(gè)哲學(xué)家取得一雙筷子開始用餐后,即時(shí)顯示“Dining??和該哲學(xué)家的自定義標(biāo)識(shí)符以及餐桌上所有幾位哲學(xué)家標(biāo)識(shí)符及其所坐的位置。3) 設(shè)定共有5個(gè)哲學(xué)家需用餐。每位用餐耗時(shí)10秒鐘以上。4) 多個(gè)哲學(xué)家須共享操作函數(shù)代碼。2.設(shè)計(jì)說明書內(nèi)容要求:1) 設(shè)計(jì)題目與要求2) 總的設(shè)計(jì)思想及系統(tǒng)平臺(tái)、語言、工具等。3) 數(shù)據(jù)結(jié)構(gòu)與模塊說明(功能與流程圖)4) 給出用戶名、源程序名、目標(biāo)程序名和源程序及其運(yùn)行結(jié)果。(要注明存儲(chǔ)各個(gè)程序及其運(yùn)行結(jié)果的Linux主機(jī)IP地址和目錄。)5)運(yùn)行結(jié)果與運(yùn)行情況(提示:(1)連續(xù)存儲(chǔ)區(qū)可用數(shù)組實(shí)現(xiàn)。(2) 編譯命令可用: cc(3) 多線程編程方法參見附件。)3.調(diào)試報(bào)告:1) 調(diào)試記錄2) 自我評(píng)析和總結(jié)上機(jī)時(shí)間安排:-Ipthread-o目標(biāo)文件名源文件名18周一?五08:0-12:00指導(dǎo)教師簽名:年月日系主任(或責(zé)任教師)簽名:年月日用多線程同步方法解決哲學(xué)家就餐問題(Dining-PhilosophersProblem).設(shè)計(jì)題目與要求1.1設(shè)計(jì)題目描述:用多線程同步方法解決哲學(xué)家就餐問題(Dining-PhilosophersProblem)1.2要求:1) 為每個(gè)哲學(xué)家產(chǎn)生一個(gè)線程,設(shè)計(jì)正確的同步算法2) 每個(gè)哲學(xué)家取得一雙筷子開始用餐后,即時(shí)顯示“Dining…”和該哲學(xué)家的自定義標(biāo)識(shí)符以及餐桌上所有幾位哲學(xué)家標(biāo)識(shí)符及其所坐的位置。3) 設(shè)定共有5個(gè)哲學(xué)家需用餐。每位用餐耗時(shí)10秒鐘以上。4) 多個(gè)哲學(xué)家須共享操作函數(shù)代碼。.總體設(shè)計(jì)思想及系統(tǒng)平臺(tái)、語言、工具2.1總體設(shè)計(jì)思想哲學(xué)家就餐問題,即共有5個(gè)哲學(xué)家繞一個(gè)圓桌做在5個(gè)位置上,他們每2個(gè)人中間有一只筷子,共5只筷子,只有當(dāng)每個(gè)哲學(xué)家取得他左右兩邊的筷子時(shí),哲學(xué)家才能開始就餐,其它時(shí)間,哲學(xué)家只能思考或等待筷子。為避免哲學(xué)家互相等待對(duì)方的筷子發(fā)生死鎖,本次課程設(shè)計(jì)要求只許4個(gè)哲學(xué)家入席,以保證至少有一個(gè)哲學(xué)家能夠進(jìn)餐。本課程設(shè)計(jì)將room作為信號(hào)量,將其初始化為4,以保證只允許4個(gè)哲學(xué)家同時(shí)入席就餐,這樣就能保證至少有一個(gè)哲學(xué)家可以就餐。針對(duì)每個(gè)哲學(xué)家,通過共享操作函數(shù)代碼,分別建立5個(gè)線程,以實(shí)現(xiàn)同步哲學(xué)家就餐,而申請(qǐng)進(jìn)入餐廳的哲學(xué)家進(jìn)入room的等待隊(duì)列,根據(jù)FIFO的原則,總會(huì)進(jìn)入到餐廳就餐,因此不會(huì)出現(xiàn)餓死和死鎖的現(xiàn)象,針對(duì)5只筷子分別設(shè)置了5個(gè)互斥信號(hào)量,以保證每只筷子每次只能被取得一次。2.2系統(tǒng)平臺(tái)、語言及工具(1) 操作系統(tǒng):Linux(2) 程序設(shè)計(jì)語言:C語言(3) 工具:編輯工具Vi、編譯器gcc3.數(shù)據(jù)結(jié)構(gòu)與模塊說明線程創(chuàng)建函數(shù)pthread_create聲明如下:#include<pthread.h>intpthread_create(pthread_t*thread,pthread_attr_t*attr,Void*(*start_routine)(void*),void*arg);等待其它線程結(jié)束函數(shù)pthread_join聲明如下:#include<pthread.h>intpthread_join(pthread_tth,void*thread_return);信號(hào)量的數(shù)據(jù)類型為結(jié)構(gòu)sem_t,它本質(zhì)上是一個(gè)長整型的數(shù)。初始化信號(hào)量函數(shù)sem_init聲明如下:#include<semaphore.h>sem_init(sem_t*sem,intpshared,unsignedintvalue);增加信號(hào)量值函數(shù)sem_post聲明如下:#include<semaphore.h>Sem_post(sem_t*sem);減少信號(hào)量值函數(shù)sem_wait聲明如下#include<semaphore.h>Sem_wait(sem_t*sem);主要數(shù)據(jù)結(jié)構(gòu)聲明:#defineNUMBERS5 //將哲學(xué)家人數(shù)NUMBERS定義為5sem_tchopstics[NUMBERS] 〃定義5只筷子的互斥信號(hào)量chopsticssem_troom //定義避免死鎖的同步信號(hào)量room線程共享函數(shù)偽代碼:void*Share(inti)(think();wait(room); //請(qǐng)求入席進(jìn)餐wait(chopstick[i]); //請(qǐng)求左手邊的筷子wait(chopstick[(i+1)%5]); //請(qǐng)求右手邊的筷子eat();signal(chopstick[i]); //釋放左手邊的筷子signal(chopstick[(i+1)%5]); //釋放右手邊的筷子signal(room); //退出席位釋放信號(hào)量chairs}.用戶名、源程序名、目標(biāo)程序名和源程序4.1用戶名、源程序名、目標(biāo)程序名用戶名:源程序名:目標(biāo)程序名:4.2源程序#include<stdio.h>#include<unistd.h>#include<pthread.h>#include<semaphore.h>#defineNUMBERS5sem_tchopstics[NUMBERS];sem_troom;sem_tmutex;intflag[NUMBERS]=(0,0,0,0,0};intchairs[NUMBERS]=(0,1,2,3,4};inti,j;void*Share(intthreadid);intmain()(interror;pthread_tthreads[NUMBERS];for(i=0;i<NUMBERS;i++)(sem_init(&chopstics[i],0,1);}sem_init(&room,0,NUMBERS-1);sem_init(&mutex,0,1);for(i=0;i<NUMBERS;i++)(error=pthread_create(&threads[i],NULL,(void*)Share,(void*)i);if(error)(printf("ERROR:threadcreatefailed!!!");//exit(-1);}}for(i=0;i<NUMBERS;i++)(pthread_join(threads[i],NULL);}}void*Share(intthreadid)(inti=threadid;sem_wait(&room);flag[i]=1;sem_wait(&chopstics[i]);printf("philosopher%dgetchopstics%d\n”,i,i);sem_wait(&chopstics[(i+1)%NUMBERS]);printf("philosopher%dgetchopstics%d\n”,i,(i+1)%NUMBERS);sem_wait(&mutex);printf("\n*********************************************\n");printf(〃Dining...\n〃);printf("philosopher:%d,onchairs%dandeating\n”,i,chairs[i]);for(j=0;j<NUMBERS;j++)(if((j!=i)&&flag[j])printf("philosopher%d,onchairs%dandhungry\n”,j,chairs[j]);}sleep(3);sem_post(&mutex);sem_post(&chopstics[i]);sem_post(&chopstics[(i+1)%NUMBERS]);printf("philosopher%disfullandputdownchopstics%dand%dandleft\n",i,i,(i+1)%NUMBERS);printf("*********************************************\n\n");flag[i]=0;sem_post(&room);}.運(yùn)行結(jié)果.調(diào)試記錄將寫好的代碼進(jìn)行編譯,出現(xiàn)如下錯(cuò)誤提示:

1.c:(.text+0x37):undefinedreferencetoseminit'1.c:(.text+0x6a):undefinedreferencetoseminit'1.c:(.text+0x86):undefinedreferencetoseminit'1.c:(.text+0xc8):undefinedreferencetopthread_create'1.c:(.text+0x108):undefinedreferencetopthread_join'/tmp/ccq8XD3O.o:InfunctionShare':1.c:(.text+0x13f):undefinedreferencetosemwait'1.c:(.text+0x160):undefinedreferencetosemwait'1.c:(.text+0x1b0):undefinedreferencetosemwait'1.c:(.text+0x1f7):undefinedreferencetosemwait'1.c:(.text+0x2ad):undefinedreferencetosem_post'1.c:(.text+0x2c0):undefinedreferencetosem_post'1.c:(.text+0x2f5):undefinedreferencetosem_post'1.c:(.text+0x35d):undefinedreferencetosem_post'statuscollect2:ldreturned1exit檢查發(fā)現(xiàn),pthread庫不是Linux系統(tǒng)默認(rèn)的庫,連接時(shí)需要使用庫libpthread.a,所以在使用pthread_create創(chuàng)建線程時(shí),在編譯中要加-lpthread參數(shù):gcc-lpthread-o11.c1.c:(.text+0x37):undefinedreferencetoseminit'1.c:(.text+0x6a):undefinedreferencetoseminit'1.c:(.text+0x86):undefinedreferencetoseminit'1.c:(.text+0xc8):undefinedreferencetopthread_create'1.c:(.text+0x108):undefinedreferencetopthread_join'/tmp/ccq8XD3O.o:InfunctionShare':1.c:(.text+0x13f):undefinedreferencetosemwait'1.c:(.text+0x160):undefinedreferencetosemwait'1.c:(.text+0x1b0):undefinedreferencetosemwait'1.c:(.text+0x1f7):undefinedreferencetosemwait'1.c:(.text+0x2ad):undefinedreferencetosem_post'1.c:(.text+0x2c0):undefinedreferencetosem_post'1.c:(.text+0x2f5):undefinedreferencetosem_post'1.c:(.text+0x35d):undefinedreferencetosem_post'status重新編譯代碼,出現(xiàn)如下錯(cuò)誤提示:1.c:9:error:invalidinitializer1.c:10:1.c:Infunction‘main’error:invalidinitializer1.c:10:1.c:Infunction‘main’1.c:35:1.c:Infunction‘Share’:1.c:35:1.c:Infunction‘Share’:warning:incompatibleimplicitdeclarationofbuilt-infunction‘exit’1.c:48:1.c:71:error:expected’before‘sem_post’1.c:48:1.c:71:error:expected’before‘sem_post’error:incompatibletypeswhenassigningtotype‘sem_t’fromtype‘int’1.c:76:error:incompatibletypeswhenassigningttype‘sem_t’fromtype‘int’仔細(xì)查看代碼,發(fā)是對(duì)信號(hào)燈初始不能在定義的時(shí)候直接初始化,改用sem_init()函數(shù)對(duì)room,mutex和flag[]進(jìn)行初始化,去掉exit()函數(shù),找到缺少‘;’的地方,添加‘;’重新編譯、連接、運(yùn)行程序,出現(xiàn)如下錯(cuò)誤提示:1.c:Infunction‘Share’:1.c:48:error:incompatibletypeswhenassigningtotype‘sem_t’fromtype‘int’1.c:76:error:incompatibletypeswhenassigningtotype‘sem_t’fromtype‘int’查看代碼,發(fā)現(xiàn)對(duì)信號(hào)燈flag口使用出錯(cuò),將flag[i]=1改為sem_wait(&flag[i]);flag[i]=0改為sem_post(&flag[i])重新編譯、連接、運(yùn)行程序,沒有錯(cuò)誤提示,但是結(jié)果有誤:philosopher0getchopstics0I,mphilosopher0getchopstics0程序程出現(xiàn)死鎖,無法正常進(jìn)行,查看代碼,發(fā)現(xiàn)不應(yīng)該把flag口設(shè)置為信號(hào)改正后,重新編譯、連接,運(yùn)行程序,得到正確結(jié)果TOC\o"1-5"\h\zphilosopher 0 get chopstics 0philosopher 1 get chopstics 1philosopher 2 get chopstics 2philosopher 3 get chopstics 3philosopher 3 get chopstics 4*********************************************Dining...philosopher:3,onchairs3andeatingphilosopher 0 ,on chairs 0 and hungryphilosopher 1 ,on chairs 1 and hungryphilosopher 2 ,on chairs 2 and hungryphilosopher3isfullandputdownchopstics3and4andleft*********************************************philosopher2getchopstics3*********************************************Dining...philosopher:2,onchairs2andeatingphilosopher0 ,on chairs 0 and hungryphilosopher1 ,on chairs 1 and hungryphilosopher4 ,on chairs 4 and hungryphilosopher4 get chopstics 4philosopher2isfullandputdownchopstics2and3andleft*********************************************philosopher1getchopstics2*********************************************Dining...philosopher:1,onchairs1andeatingphilosopher0,onchairs0andhungryphilosopher4,onchairs4andhungryphilosopher1isfullandputdownchopstics1and2andleft*********************************************philosopher0getchopstics1*********************************************Dining...philosopher:0,onchairs0andeatingphilosopher4,onchairs4andhungryphilosopher0isfullandputdownchopstics0and1andleft*********************************************philosopher4getchopstics0*********************************

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論