Code Monkey home page Code Monkey logo

sleeping-barber-problem's Introduction

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

The barber is providing 24x7 service:
1. Sleeps until a customer wakes him up
2. He picks first customer in room (b.release)
3. One seat is freed up (seats++)
4. Cuts customer's hair (sleep 1s)
... Goes back to sleep
Each customer requires a haircut:
1. Occupies a seat, if available
2. Wakes up the barber (c.release)
3. Waits for barber to indicate his turn
... His hair is cut.
## OUTPUT
Starting barber (B) with 2 seats and 5 customers ...
B: sleeping
0: checking seats
B: got customer
B: cutting hair
0: sat, seats=1
0: having hair cut
1: checking seats
1: sat, seats=1
2: checking seats
2: sat, seats=0
3: checking seats
3: no seats!
B: cutting done
B: sleeping
B: got customer
B: cutting hair
1: having hair cut
4: checking seats
4: sat, seats=0
B: cutting done
B: sleeping
B: got customer
B: cutting hair
2: having hair cut
B: cutting done
B: sleeping
B: got customer
B: cutting hair
4: having hair cut
B: cutting done
B: sleeping

See Main.java for code, and repl.it for output.

references

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.