理发师问题的实现

理发师问题:

  一个理发店由一个有几张椅子的等待室和一个放有一张理发椅的理发室组成。     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);
   }

}

点赞