理发师问题的实现


理发师问题:

  一个理发店由一个有几张椅子的等待室和一个放有一张理发椅的理发室组成。     1. 若没有要理发的顾客,则理发师去睡觉;     2. 若一顾客进入理发店,理发师正在为别人理发,且等待室有空椅子,则该顾客就找张椅子按顺序坐下;     3. 若一顾客进入理发店,理发师在睡觉,则叫醒理发师为该顾客理发;     4. 若一顾客进入理发店且所有椅子都被占用了,则该顾客就离开。


信号量mutex用来互斥;

信号量customers用来记录等候的顾客数量,并阻塞理发师进程;

信号量barbers用来记录理发师的状态,并阻塞顾客进程

变量 wait_customer用来记录等候的顾客的数量。


代码:

# -*- coding: utf-8 -*-


import threading
import time

customers = threading.Semaphore(0)
barbers = threading.Semaphore(0)
mutex = threading.Lock()
wait_customer = 0
CHAIRS = 5

def customer():
    print '... Customer''s process'
    global wait_customer
    global CHAIRS
    global customers,barbers,mutex

    time.sleep(1)
    mutex.acquire()
    if (wait_customer<CHAIRS):
        wait_customer+=1
        print '''Customers' number is ''',wait_customer
        customers.release()
        mutex.release()
        barbers.acquire()
    else:
        print 'Too many customers !'
        mutex.release()


def barber():
    print '... Barber''s process'
    global wait_customer
    global CHAIRS
    global customers,barbers,mutex

    while(1):
        if(wait_customer==0):
            print 'Barber: I am sleeping now...'
        time.sleep(1)
        customers.acquire()
        mutex.acquire()
        wait_customer-=1
        barbers.release()
        mutex.release()
        cut_hair()


def cut_hair():
    print 'Barber:  I am cutting the customer''s hair...'
    time.sleep(2)
    print 'Barber:  done.'

if __name__ == "__main__":
    t1 = threading.Thread(target=barber)
    t1.start()
    t1.join(1)

    while(1):
        try:
            time.sleep(0.5)
            t2 = threading.Thread(target=customer)
            t2.start()
            t2.join(1)
        except  Exception,e:
                print 'Program terminated .',e


再贴一份C++的代码:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/time.h>
#include <math.h>
#define CHAIRS 5
sem_t customers;
sem_t barbers;
pthread_mutex_t mutex;
int waiting = 0;
void *barber(void *arg);
void *customer(void *num);
void cut_hair(void);
double flat(void);
double normal(void);
double bursty(void);
int main()
{
   int i;
   pthread_t barber_t,customer_t;
   int error;
   error=pthread_create(&barber_t,NULL,barber,NULL);
   if(error!=0) {
      printf("pthread_create is not created.../n");
      return -1;
   }
   while(1) {
      usleep(30000);
      error=pthread_create(&customer_t,NULL,customer,NULL);
      if(error!=0) {
         printf("pthread_create is not created...\n");
         return -1;
      }
   }
}
void *barber(void *arg)
{
   while(1)
   {
      sem_wait(&customers);
      pthread_mutex_lock(&mutex);
      waiting = waiting -1;
      sem_post(&barbers);
      pthread_mutex_unlock(&mutex);
      cut_hair();
   }
}
void cut_hair(void)
{
   printf("  Barber:I am cutting the customer's hair...\n");
   usleep(100000);
   printf("  Barber:done.\n");
}
void *customer(void *num)
{
   pthread_mutex_lock(&mutex);
   if(waiting<CHAIRS)
   {
       waiting = waiting + 1;
       sem_post(&customers);
       pthread_mutex_unlock(&mutex);
       sem_wait(&barbers);
   }
   else
   {
      printf("  Waiter is too much...\n");
      pthread_mutex_unlock(&mutex);
   }

}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值