【发布时间】:2015-01-13 00:23:07
【问题描述】:
我正在尝试使用 pthreads 和信号量在 C 中实现一个解决睡眠理发师问题的解决方案,只有部分要求是每个动作都必须在发生时打印出来,例如:
- 理发师睡着了
- 客户叫醒了理发师
- 顾客在等理发师
- 客户离开,因为没有空位(只是稍后随机返回)
- 客户正在理发
- 客户已离开商店
我一直在解决这个问题,只是遇到各种死锁或乱序打印语句。
问题的classical solution 不会在这里完全适用,因为它会导致每次都打印语句(例如“理发师睡着了”,然后是“客户已经叫醒理发师”),或者它会打印由于上下文切换(这可能是也可能不是真正的问题)而导致语句乱序。
我当前的伪代码解决方案是这样的:
int chairs = N # available chairs
bool is_sleeping = false
bool is_cutting = false
bool finished = false # All customers taken care of (Changed in main)
# Semaphores for read/write access
semaphore rw_chairs = 1
semaphore rw_sleeping = 1
semaphore rw_cutting = 1
semaphore barber_ready = 0 # Barber ready to cut hair
semaphore sleeping = 0 # Barber is sleeping
semaphore cutting = 0 # Barber cutting a customer's hair
def Barber():
while not finished:
wait(rw_chairs)
wait(rw_sleeping)
# If no chairs are being used
if chairs == N:
is_sleeping = true
print("Barber is sleeping!")
signal(rw_chairs) # Allow others to read/write
signal(rw_sleeping)
wait(sleeping) # Wait for customer to wake him up
signal(barber_ready) # Allow customers into the chair
print("Barber woke up!")
else:
signal(rw_signal)
chairs += 1
signal(barber_ready)
signal(rw_chairs)
# If the barber isn't done for the day, help the customer
if not finished:
print("Helping a customer")
# Wait a random amount of time
print("Finished helping a customer")
signal(cutting) # Let the customer leave after the hair cut
else:
print("Barber is done for the day!")
def Customer():
bool helped = false
while not helped:
wait(rw_chairs)
wait(rw_cutting)
if chairs == N and not is_cutting: # If the barber is free
wait(rw_sleeping)
if is_sleeping:
signal(sleeping) # Wake the barber up
is_sleeping = false
print("Customer has woken the barber up")
signal(rw_sleeping)
is_cutting = true
signal(rw_chairs)
signal(rw_cutting)
wait(barber_ready) # Wait for the barber to be ready
wait(cutting) # Wait for him to finish cutting hair
print("Customer has had his hair cut")
helped = true
else if chairs > 0:
chairs -= 1
print("Customer is waiting in a chair")
signal(rw_chairs)
signal(rw_cutting)
wait(barber_ready)
wait(cutting)
print("Customer has had his hair cut")
helped = true
else:
signal(rw_chairs)
signal(rw_office)
print("All chairs taken, will return later")
# Wait a random amount of time
print("Customer is leaving the barbershop")
当这没有陷入僵局时,使用 3 把椅子我得到以下输出:
Barber has fallen asleep
Customer is waiting in a chair
Customer is waiting in a chair
Customer is waiting in a chair
All chairs used, will return later
All chairs used, will return later
... (repeat the above line infinitely)
我很清楚理发师没有适当地让顾客进来——但即便如此,我也觉得我的结构完全错了。我认为我处理问题的方法有误,而且可能过于复杂。
如果这里有人对如何构建有效的解决方案有任何建议,或者对重组我当前的解决方案有任何建议,我们将不胜感激。或者,如果我在正确的轨道上,可能会朝着正确的方向推进。
谢谢!
【问题讨论】:
标签: c multithreading concurrency pthreads semaphore