考研複習(12)-理髮師問題

來源:互聯網
上載者:User

 //理髮店裡有一位理髮師,一把理髮椅和三把等候椅,編寫程式協調顧客和理髮師
int count=0;
//記錄理髮店裡的顧客數
semaphore barber_chair=1;
// 理髮椅一張
semaphore wait_chair=3;
//等待椅子三張
semaphore mutex=1;
//互斥地訪問count的訊號量
semaphore ready=0;
//準備好的可以理髮的數量當成理髮師所需要的資源
semaphore finish=0;
//理髮師完成理髮後發給顧客的訊號量作為顧客的資源
Barber()
{
 while(1)
 {
 wait(ready);
 barbering;
 signal(finish);
 //告訴理髮椅子上的顧客理髮已經完成
 }
}
Customer()
{
 while(1)
 {
 wait(mutex);
 if(count>=3)
        {
         signal(mutex);
          leaving;
         }
 else{
          count++;
           signal(mutex);
       }
 wait(wait_chair);
 wait(barber_chair);
 signal(wait_chair);
 signal(ready);
 be barbered;
 wait(finish);
 //等待理髮師剪完
 signal(barber_chair);
 //離開理髮椅子
 wait(mutex);
 count--;
 //更新店裡的顧客數
 signal(mutex);
 }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.