下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精品文檔用多線程同步方法解決睡眠理發(fā)師問(wèn)題(Sleeping-Barber Problem)1 操作系統(tǒng):Linux2 程序設(shè)計(jì)語(yǔ)言: C語(yǔ)言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í),還同時(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) 多線程編程方法參見(jiàn)附件。)詳細(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)。1歡迎下載精品文檔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 comes in and s
4、its 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);。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 = time (NULL) + 30;se
5、m_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!= ret)perror(create customers);ret = pt
6、hread_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);。3歡迎下載精品文檔if (0!=ret)perror(create customers);ret = pthread_create(&id5
7、, 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 customers);ret = pthread_create(&id8, NULL, (void*)customer, id
8、8);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);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
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版?zhèn)€性化定制門(mén)窗安裝與綠色建材供應(yīng)合同2篇
- 二零二五版木地板工程進(jìn)度與成本管理合同4篇
- 二零二五年度游戲角色形象授權(quán)合同4篇
- 二零二五年度嬰幼兒奶粉安全風(fēng)險(xiǎn)評(píng)估與管理體系建設(shè)合同4篇
- 二零二五年度城市綠化景觀提升項(xiàng)目種植合同3篇
- 二零二五年度影視MV拍攝與藝人肖像權(quán)授權(quán)合同
- 二零二五年度木材貿(mào)易代理與倉(cāng)儲(chǔ)管理合同3篇
- 二零二五年度人防工程防雷接地檢測(cè)合同2篇
- 二零二四年度信用證項(xiàng)下跨境貿(mào)易融資合同模板3篇
- 二零二四年度液化氣供應(yīng)與綜合能源服務(wù)合同范本3篇
- 2024-2025學(xué)年山東省濰坊市高一上冊(cè)1月期末考試數(shù)學(xué)檢測(cè)試題(附解析)
- 江蘇省揚(yáng)州市蔣王小學(xué)2023~2024年五年級(jí)上學(xué)期英語(yǔ)期末試卷(含答案無(wú)聽(tīng)力原文無(wú)音頻)
- 數(shù)學(xué)-湖南省新高考教學(xué)教研聯(lián)盟(長(zhǎng)郡二十校聯(lián)盟)2024-2025學(xué)年2025屆高三上學(xué)期第一次預(yù)熱演練試題和答案
- 決勝中層:中層管理者的九項(xiàng)修煉-記錄
- 幼兒園人民幣啟蒙教育方案
- 臨床藥師進(jìn)修匯報(bào)課件
- 軍事理論(2024年版)學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 《無(wú)人機(jī)法律法規(guī)知識(shí)》課件-第1章 民用航空法概述
- 政治丨廣東省2025屆高中畢業(yè)班8月第一次調(diào)研考試廣東一調(diào)政治試卷及答案
- 網(wǎng)絡(luò)設(shè)備安裝與調(diào)試(華為eNSP模擬器)整套教學(xué)課件
- 銀行卡凍結(jié)怎么寫(xiě)申請(qǐng)書(shū)
評(píng)論
0/150
提交評(píng)論