Code Monkey home page Code Monkey logo

shift-sleeping_barber_problem's Introduction

SHIFT: Sleeping Barber Problem

Please use only the C++ standard library in your solution*, and make sure to provide compiling instructions if no build system is provided. You can use any combination of tools and operating systems to solve this problem, but it should be compilable on a standard Linux installation (i.e. with GCC). Additionally, your solution should make use of objected-oriented programming concepts.

Please make sure to also add sufficient comments to your code. Defining "sufficient" is up to you.

* E.g. you should use "std::thread" instead of POSIX threads (a.k.a. "pthreads").

Problem Description

This problem is about simulating a barber shop with the following characteristics. The barber shop has a waiting room with M seats and a separate room with a single barber chair where the barber works. If there are no waiting customers, nor a customer having a haircut, the barber takes a break and sleeps. The moment a customer arrives, s/he wakes the barber up so that they can have their haircut. If another customer arrives while the barber is working, s/he will either sit in the waiting room (if there are available seats) or leave (if no seats are available).

Implementation Details

The only (command line) input for your program will be the number M of seats in the waiting room. The barber needs to be implemented as a thread which runs indefinitely, always performing one of the 3 possible tasks: sleeping, waking up and working. The barber sleeps if there are no awaiting customers.

There must be a thread which is responsible for creating new threads from time to time to simulate new customers arriving in the barber shop. This thread should run an infinite loop creating new threads every 3 seconds. Each created thread need to receive an integer parameter (sequentially incremented) which will serve as the customer ID.

In the beginning of the program, the barber is sleeping and there are no customers in the shop. When a customer arrives, s/he wakes the barber up so that they can have their haircut. The haircut is to be simulated with a thread sleep of N seconds, where N is a random number between 1 and 5, inclusive (i.e. [1,5]). If a new customer (thread) arrives while the barber is busy, they should wait in one of the seats of the waiting room, if one is available, or leave if none is available. Leaving is simulated by simply terminating the execution of the thread.

Expected Output

The program must output the following given the current status of the program:


Case 1

There are no customers having a haircut and the barber is sleeping while waiting for customers.

Barber sleeping
Waiting room:

Case 2

The barber is cutting the hair of customer 12 and customers 13 to 16 are waiting.

Barber cutting the hair of customer 12
Waiting room: 13 14 15 16

Case 3

The barber is cutting the hair of customer 12 and customers 15, 16, 17, 19 and 21 are waiting (assuming there are only 5 seats in the waiting room) when customer 22 arrives and ends up leaving because there are no available seats.

Barber cutting the hair of customer 12
Waiting room: 15 16 17 19 21
Customer 22 leaving

Note: given the behavior of the program, customers 13, 14, 18 and 20 must have arrived when no seats were available.


The customer number is the argument given when creating the thread, as explained above.

If you have any specific questions, please contact me at [email protected].

Good luck!

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.