Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:15:36+00:00 2026-06-16T05:15:36+00:00

hi there i’m implementing a multi-thread program which is called the sleeping barber problem.

  • 0

hi there i’m implementing a multi-thread program which is called the sleeping barber problem. The issue is there is one barber shop with N chairs and if there are no any customers barber will fall asleep, whenever a customer comes into the shop barber will wake up and start to cut off customer’s hair. i use condition variables rather than semaphores. the thing is i couldnt have the output which i expect.

    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    #include <time.h>

    #define TRUE 1

    pthread_mutex_t mutex;
    pthread_cond_t fill;

    int counter;

    int* buffer;
    int size;

    void* producer(void*);
    void* consumer(void*);

    void initialize_variables();
    void insert(void);
    void remove_item(void);

    int main(int argc, char *argv[]) {

        int random_num;
        int num=0;
        pthread_t barber;

        srand( time(NULL) );

        if(argc!=2){
            printf("Not enough parameters\n");
            exit(0);
        }

        size=atoi(argv[1]);

        initialize_variables();

        pthread_create(&barber,NULL,consumer,NULL);

        while(TRUE){

            pthread_t customer;
            pthread_create(&customer,NULL,producer,NULL);

        }

        printf("Exit the program\n");
        return 0;
    }

    void initialize_variables(){

        int i;

        if(! (buffer=(int*)malloc(size*sizeof(int))) ){
            printf("Error while allocating memory for buffer\n");
            exit(1);
        }

        for(i=0; i<size; i++)
            buffer[i]=0;

        pthread_mutex_init(&mutex,NULL);

        pthread_cond_init(&empty, NULL);
        pthread_cond_init(&fill, NULL);

        counter = 0;
    }

  void* producer(void* arg){

    pthread_mutex_lock(&mutex);

        while(counter==size)
            pthread_cond_wait(&empty,&mutex);

        printf("One Customer has arrived and sit on the  #%d chair\n", counter);
        insert();
        sleep(1);
        pthread_cond_signal(&fill);

    pthread_mutex_unlock(&mutex);

    pthread_exit(NULL);
}

void* consumer(void* arg){

    while(TRUE){

        pthread_mutex_lock(&mutex);

            while(counter==0){
                printf("Barber is sleeping now\n");
                pthread_cond_wait(&fill,&mutex);
            }

            remove_item();
            sleep(1);
            pthread_cond_broadcast(&empty);

        pthread_mutex_unlock(&mutex);

    }
}

void insert(void){

    buffer[counter]=1;
    counter++;
}

void remove_item(void){

    buffer[counter]=0;
    printf("The barber is started to cut the hair of the customer at chair #%d\n", counter);
    counter--;
}

Moreover, when i start to program output should print "Barber is sleeping now" first, however main thread starts run producer thread first and then when counter is equal to zero it prints "Barber is sleeping now" can you help me why the sequence starts wrongly ?. and an another issue is main thread fills up whole the buffer and starts to count down (which refers that barber is going to start his work after shop is full. here is an example of output which will make clear for you. and assume that buffer size is 10.

One Customer has arrived and sit on the  #0 chair
One Customer has arrived and sit on the  #1 chair
One Customer has arrived and sit on the  #2 chair
One Customer has arrived and sit on the  #3 chair
One Customer has arrived and sit on the  #4 chair
One Customer has arrived and sit on the  #5 chair
One Customer has arrived and sit on the  #6 chair
One Customer has arrived and sit on the  #7 chair
One Customer has arrived and sit on the  #8 chair
One Customer has arrived and sit on the  #9 chair
The barber is started to cut the hair of the customer at chair #10
The barber is started to cut the hair of the customer at chair #9
The barber is started to cut the hair of the customer at chair #8
The barber is started to cut the hair of the customer at chair #7
The barber is started to cut the hair of the customer at chair #6
The barber is started to cut the hair of the customer at chair #5
The barber is started to cut the hair of the customer at chair #4
The barber is started to cut the hair of the customer at chair #3
The barber is started to cut the hair of the customer at chair #2
The barber is started to cut the hair of the customer at chair #1
Barber is sleeping now
One Customer has arrived and sit on the  #0 chair

i will be appreciated for every reply and thanks anyway

EDIT: first code which post was not working sorry for that and i updated with the true one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:15:37+00:00Added an answer on June 16, 2026 at 5:15 am

    You can’t assume nothing regarding the order of which threads will perform.
    This is unexpected behaviour and it’s not deterministic.
    If you need assurance that one thread starts before the other. Use a mutex or a semaphore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There are nice SO question and answers about this issue, but these options didn't
There are two table s : one is the master and one the detail
There is some unknown problem with my facebook app running on heroku Problem is
There was an issue in getting an global data into an BroadCast receiver Class,
There's a column on one of my tables that's being updated by various INSERT/DELETE
There can only be one IDENTITY column per table Why is it so? Take
There was a thread on this in comp.lang.javascript recently where victory was announced but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.