

下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、用多線程同步方法解決睡眠理發(fā)師問題(Sleeping-Barber Problem)1操作系統(tǒng): Linux2程序設(shè)計(jì)語言:C 語言3設(shè)有 1 個(gè)理發(fā)師 5 把椅子(另外還有一把理發(fā)椅),幾把椅子可用連續(xù)存儲(chǔ) 單元.1 技術(shù)要求:1)為每個(gè)理發(fā)師顧客產(chǎn)生一個(gè)線程,設(shè)計(jì)正確的同步算法2)每個(gè)顧客進(jìn)入理發(fā)室后,即時(shí)顯示“Entered ” 及其線程自定義標(biāo)識,還 同時(shí)顯示理發(fā)室共有幾名顧客及其所坐的位置。3)至少有 10 個(gè)顧客,每人理發(fā)至少 3 秒鐘。4)多個(gè)顧客須共享操作函數(shù)代碼。提示:(1) 連續(xù)存儲(chǔ)區(qū)可用數(shù)組實(shí)現(xiàn)。(2) 編譯命令可用: gcc -lpthread -o 目標(biāo)文件名 源文件
2、名(3) 多線程編程方法參見附件。)詳細(xì)代碼#include #include #include #include #include #include #include #include #define n 5time_t end_time;sem_t mutex, customers, barbers;int count = 0;int chair 5 = -1, -1, -1, -1, -1 ;void barber(void *arg)while (time (NULL) 0)while (count 0)sem_wait(&customers); sem_wait(&m
3、utex);count-;printf (the barber is cutting hair, count is : %dn, count); sem_post(&mutex);sem_post(&barbers);sleep(3);void customer (void *arg)int i ,id= 0, num=0;while (time (NULL) end_time)sem_wait(&mutex);if (count n)count+;num= count % 5;num+;printf(customer entered:the customer %s c
4、omes in and sits at %d the chair count is:%dn, (char *)arg, num, count);sem_post(&mutex); sem_post(&customers);sem_wait(&barbers);elsesem_post(&mutex);sleep(2);int main (int argc, char *argv)pthread_t id1, id2, id3, id4, id5, id6, id7, id8, id9, id10, id11; int ret= 0;int i;end_time
5、= time (NULL) + 30;sem_init (&mutex, 0, 1);ret = sem_init (&barbers, 0, 1);for (i =0;i5;i+)chair i = -1;if (0!= ret)perror(sem init);ret= pthread_create (&id11, NULL, (void*)barber,id11); if (0!= ret)perror(create barber);ret = pthread_create ( &id1,NULL,(void*)customer, id1); if (0!
6、= ret)perror(create customers);ret = pthread_create(&id2, NULL, (void*)customer, id2); if (0!=ret)perror(create customers);ret = pthread_create(&id3, NULL, (void*)customer, id3); if (0!=ret) perror(create customers);ret = pthread_create(&id4, NULL, (void*)customer, id4); if (0!=ret)perro
7、r(create customers);ret = pthread_create(&id5, NULL, (void*)customer, id5);if(0!=ret)perror(create custmers);ret = pthread_create(&id6, NULL, (void*)customer, id6); if (0!= ret) perror(create customers);ret = pthread_create ( &id7,NULL,(void*)customer, id7); if (0!= ret)perror(create cus
8、tomers);ret = pthread_create(&id8, NULL, (void*)customer, id8); if (0!=ret) perror(create customers);ret = pthread_create(&id9, NULL, (void*)customer, id9); if (0!=ret)perror(create customers);ret = pthread_create(&id10, NULL, (void*)customer, id10); if (0!=ret) perror(create customers);
9、pthread_join(id1, NULL);pthread_join(id2, NULL);pthread_join(id3, NULL); pthread_join(id4, NULL);pthread_join(id5, NULL); pthread_join(id6, NULL);pthread_join(id7, NULL);pthread_join(id8, NULL);pthread_join(id9, NULL);pthread_join(id10, NULL);pthread_join(id11, NULL); exit(O);運(yùn)行結(jié)果如下CU5tffiflr曲GU5tff
10、Wr 141的祚innd14林5t7thq- CMlr側(cè)1Eustoncr snterDd: the匚ustoracr idl2匚 口 l!5andsitsatJthQchaiITcount15:2tu自ton打Wtiftd: tbi tudtonr ld3andat4thethalr19:3CUftmr entF9E|!:tlN SUfitWMr UMCflies 1ftandat乎thechtlrCWItIS;!custonar antirad! the custonar Ld5 the- &arber iicuttijnq hHlrPcauntcoins jnis : 4-nnr
11、lnt1thachfilrccjnr15!custctwr entered;the custmer ldl tlw hrlw itcutting hair. CQuntcoves in15:4andUt5at1hechairCW14rthp rnar antfirarl: the cnstonar LdS lhe barberis vuttljng halrPcaunE匚DIQS flis : 4-nnriit5nt1thachairVOUflF n:1custowr entered:the custcner ld3 tn bcirtiqr Itcutting何山 count tha-刼 hqis cuttning haire匚DUnt The* barb61 i tut Clhg hdlr , thebarber ishair, touetth hfirbar is cutting hi.countco鹽imIE : 4 is :步1A;215 : 1 1$ ! Bandsitsat1hechairceufiilSr5pig嗣Mg卯心蟲top:(史
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖北省煙草專賣局(公司)真題2024
- 昆明市公安局勤務(wù)輔警招聘筆試真題2024
- 2025版中建工地安全文明標(biāo)準(zhǔn)化觀摩手冊
- 2025年英語六級6月試題
- 論杜威對西方傳統(tǒng)哲學(xué)中二元論思維的批判與超越
- 區(qū)域性廢棄物資源化處理工藝與設(shè)備選擇
- 業(yè)財(cái)融合視角下農(nóng)業(yè)副產(chǎn)品的全生命周期管理
- 高中語文和外語通跨學(xué)科教學(xué)中的互動(dòng)式課堂設(shè)計(jì)
- 2025至2030年中國豬光面獵裝女裙行業(yè)投資前景及策略咨詢報(bào)告
- 2025至2030年中國煉油三劑行業(yè)投資前景及策略咨詢報(bào)告
- 刺繡介紹教學(xué)課件
- 北京稅務(wù)籌劃課件
- 肥料施用與作物營養(yǎng)診斷考核試卷
- 內(nèi)燃機(jī)技術(shù)協(xié)議書
- 數(shù)字智慧方案數(shù)字鄉(xiāng)村信息化建設(shè)及精細(xì)化治理平臺(tái)建設(shè)方案
- 知識產(chǎn)權(quán)國際保護(hù)課件
- 2024年隴南市事業(yè)單位專業(yè)技術(shù)人才引進(jìn)筆試真題
- 2025年海南省中考模擬語文試題(含答案)
- 法律文化-形考作業(yè)1-國開(ZJ)-參考資料
- 2025年山東省德州市樂陵市中考一模生物學(xué)試題(含答案)
- 2025遼寧沈陽水務(wù)集團(tuán)有限公司招聘32人筆試參考題庫附帶答案詳解
評論
0/150
提交評論