项目作者: javaf

项目描述 :
The sleeping barber problem is a classic interprocess communication and synchronization problem between multiple operating system processes.
高级语言: Java
项目地址: git://github.com/javaf/sleeping-barber-problem.git
创建时间: 2020-08-16T13:29:06Z
项目社区:https://github.com/javaf/sleeping-barber-problem

开源协议:MIT License

下载


There is a barber sleeping in his shop. When a
customer comes, he checks if the barber is
sleeping and wakes him up. If there are no other
customers in the waiting room, the barber cuts
his hair. Else the customer takes a set in the
waiting room. But if there are no seats, he
leaves. Once a barber finishes cutting hair, he
checks if there are any customers in the waiting
room. If not, he goes back to sleep again. The
idea is the barber works only when a customer
arrives, and sleeps otherwise.

Course: Concurrent Data Structures, Monsoon 2020\
Taught by: Prof. Govindarajulu Regeti

  1. The barber is providing 24x7 service:
  2. 1. Sleeps until a customer wakes him up
  3. 2. He picks first customer in room (b.release)
  4. 3. One seat is freed up (seats++)
  5. 4. Cuts customer's hair (sleep 1s)
  6. ... Goes back to sleep
  1. Each customer requires a haircut:
  2. 1. Occupies a seat, if available
  3. 2. Wakes up the barber (c.release)
  4. 3. Waits for barber to indicate his turn
  5. ... His hair is cut.
  1. ## OUTPUT
  2. Starting barber (B) with 2 seats and 5 customers ...
  3. B: sleeping
  4. 0: checking seats
  5. B: got customer
  6. B: cutting hair
  7. 0: sat, seats=1
  8. 0: having hair cut
  9. 1: checking seats
  10. 1: sat, seats=1
  11. 2: checking seats
  12. 2: sat, seats=0
  13. 3: checking seats
  14. 3: no seats!
  15. B: cutting done
  16. B: sleeping
  17. B: got customer
  18. B: cutting hair
  19. 1: having hair cut
  20. 4: checking seats
  21. 4: sat, seats=0
  22. B: cutting done
  23. B: sleeping
  24. B: got customer
  25. B: cutting hair
  26. 2: having hair cut
  27. B: cutting done
  28. B: sleeping
  29. B: got customer
  30. B: cutting hair
  31. 4: having hair cut
  32. B: cutting done
  33. B: sleeping

See @wolfram77/sleeping-barber-problem#Main.java">Main.java for code, and repl.it for output.

references