Click here to Skip to main content
Click here to Skip to main content

Simulation of Barber shop problem

, 9 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Simulation of Barber shop problem

Introduction

This article simulates the Barber shop scenario with out using multithreading. Below is the breif outline of the scenario:

Barber shop consists of a waiting room with 'n' chairs, and a barber room
with one barber chair,

1. if there are no customers to be served the barber goes to sleep.
2. If a customer enters the barber shop and all the chairs are occupied
then customer leaves shop.
3. If barber is busy but chairs are available then customer sits in one of the
free chairs.
4. If barber is asleep the customer wakes up barber.

Background

One should need to have basic C++ skills to understand the code. This simulation uses counter method instead of timer.

Using the code

Shown belows is the CBarberShop class which contains various member variables and methods.

Description of the member variables:

m_NoWaitingChairs: this variables always contains the no. of waiting chairs available at a given time.

m_NoCustomers: this variables always contains the no. of customers waiting at a given time.

MAX_CHAIRS: This const variable is fixed to 5 which means the available no. of waiting seats the shop.

m_BarberStatus: this boolen value is set true if the barber is busy cutting hair. If he is free the value is set to false.

IsRandEven: This variable is used in triggering new customer arrival. This variable is set to true if the random no. generated is even(means new customer arrived) or to false if the random no. generated is odd(no new customer).

Description of the member functions:

CBarberShip(): Constructor to initialize the member variables.

bool Barber_Status(): returns the barber status. true if the barber is busy. false if he is free.

void Wakeup(): This function is invoked by new customer to wakeup the barber who is sleeping.

void Check_For_New_Customer(): This is the heart of the program, which keeps on finding for new customer. The logic behind is: I am generating random numbers. If the number is even then it triggers a new customer is arrived. If the number is odd, no new customer.

void HairCut(): While the barber is cutting hair, this function is invoked.

void Ger_Customer_Seat_Status(): this function tells the available no.of waiting seats and the customers waiting at a given time.

void set_barber_status(): this function sets the barber status to true or false depending upon his status.

bool IsEven(int): returns the true if the no. passed to it is even else false.

 class CBarberShop
{
    private:

        int m_NoWaitingChairs,m_NoCustomers;
        const int MAX_CHAIRS;
        bool m_BarberStatus,IsRandEven;

    public:
        
        CBarberShop():MAX_CHAIRS(5)  //Constructor initializing variables
        {
            m_NoWaitingChairs=5;
            m_NoCustomers=0;
            m_BarberStatus=false;
        }
        
        bool Barber_Status()   //returns the barber's status. true=busy; false=free;
        {
        }
        void Wakeup()  // if the barber is sleeping customer wakes him up
        {
        }

        //constantly checks for new customer arriaval. If the random number is even, it triggers
        // new customer arrival. if random number is odd, no trigger.

        void Check_For_New_Customer() 
        {
        }
        //while barber doing haircut. decrease no.of customers and increase waiting chairs.
        void Haircut()
        {
        }

        //gets the current customer and seats info

        void Get_Customer_Seat_Status()
        {
        }

        // sets the barber status to true or false depending on the customers.

        void Set_Barber_Status(bool status)
        {
        }

        //returns no.of customers at any time
        int No_Customers()
        {
        }
        //retuns no. of waiting chairs availalbe at any time
        int No_WaitChairs_Available()
        {
        }

        bool IsEven(int x)
        {    
        }    

};

Here is the description of how this program works:

Barber is said to be done hair cut if the HairCut static variable reaches the value 5.
It is been incremented in the while loop. This replaces the timer method. Instead of allocating
some timer for haircut, I am allocating a counter for it. If the counter reaches the value 5, haircut is said to be done.

And the logic behind the new customer arrival is, I always generate a random number.
If the random no. is even, it means new customer is arrived. if odd, no new customer is arrived.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ananth Nag Sastry Ganti
Software Developer
United States United States
I am Ananth Nag Sastry Ganti, working as a software engineer at IBM T.J Watson Research Center, NY. I am working on Image processing and Video suviellence projects. My areas of interests include DSP, Image Processing.
 
Any advices or suggestions on my articles are always welcome.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberFerdous Ara Mostofa13-Dec-11 8:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 9 Jul 2008
Article Copyright 2008 by Ananth Nag Sastry Ganti
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid