Issue with the Sleeping Barber Problem

Hello,

I understand the description of the Sleeping Barber problem but with the code below, it seems that the output is not being generated correctly. I believe that the code is correct but it appears that the threads are running one at at time and not concurrently. Can anyone determine where I am going wrong in trying to implement this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>

using namespace std;

sem_t customerReady;
sem_t barberReady;
sem_t accessWRSeats;
unsigned int numberOfAvailableSeats = 0;
unsigned int numberOfCustomers = 0;

int HOURS = 1;
int amountTime = 0;

void *barber(void*){
    while(amountTime < HOURS*60){
   	 sem_wait(&customerReady);//Check to see if there are any customers ready and if not, sleep
   	 sem_wait(&accessWRSeats); //Wake up and and try to access the # of available seats and if not, sleep
   	 //Enter Critical Section
	 //If there are any customers in the waiting room
	 //If there aren't any customers in the waiting room
	
	cout << "\nBarber: number of customers: " << numberOfCustomers;
	
		numberOfCustomers--;
		numberOfAvailableSeats++;//Take a customer from the wait room to the barber chair
	//Cut hair here
   	 sem_post(&accessWRSeats);//Release lock on available seats; chairs are unlocked
	sem_post(&barberReady);//Finish cutting hair and is ready to cut again
	 amountTime++;
    }
	cout << "EXITING BARBER!" << endl;
    pthread_exit(NULL);    
}

void *customer(void *){
    while(amountTime < HOURS* 60){
   	 sem_wait(&accessWRSeats); //Request access to the number of available seats in the waiting room
  	   	 //If there are seats available, do not leave (wait)
   	 if(numberOfAvailableSeats > 0){
		numberOfCustomers++;//customer enters the barber shop
		cout << "\n\tCustomer: number of Customers: " << numberOfCustomers;

   		numberOfAvailableSeats--;//Sit down in a chair
		 
   		 sem_post(&customerReady);//Signal that there is a customer ready to be served
   		 sem_post(&accessWRSeats);//release lock on seats; lock not needed
   		 sem_wait(&barberReady);//Wait until the barber is ready
   	 }
	 //no free seats
	 else{
	     cout << "\nCustomer:no fucking seats , number of available seats is:" << numberOfAvailableSeats << endl;
		 	cout << "\n\tNumber of available seats:" << numberOfAvailableSeats;
			cout << "\tNumber of Customers:%d\n" << numberOfCustomers << endl;
		 sem_post(&accessWRSeats); //release the lock on the seats and leave without a haircut
	 }
	 amountTime++;
    }
    cout << "EXITING CUSTOMER!" << endl;

	pthread_exit(NULL);
}

//***********************************************************************************************************************
    /* NOTES
    //condition for barber
    1.) customer in the WR and takes him to the barber chair, freeing up one of the occupied WR seats
    2.) no customers in the WR and the barber goes back to his chair to sleep
    
    //conditions for WR
    I. checks to see what the barber is doing
   	 A. barber is sleeping
   		 -customer wakes up the barber and the barber cuts the hair
   	 B. barber is cutting someone's hair
   		 -then you must check the WR   	 
   			 1.) the number of available WR seats < to the number of customers (ex: seats=0, customers=1 or more)
   				 a. customer leaves
   			 2.) the number of available WR seats >= to the number of customers (ex: seats=2, customers=1,0)
   				 a. customer takes a seat in the WR and the number of WR seats decreases by one
    */    
//***********************************************************************************************************************
int main(int argc, char** argv){   
    //get the number of seats available from the command line
    if(argc == 2){
	 numberOfAvailableSeats = atoi(argv[1]);    
    }
    else {
   	 cout << "Please enter the number of seats you would like to simulate." << endl;
   	 cin >> numberOfAvailableSeats;
    }
	
	/*Semaphore initialization*/
	sem_init(&barberReady,0,1);
	sem_init(&customerReady,0,0);
	sem_init(&accessWRSeats,0,1);
    
    pthread_t barberThread;
    pthread_t customerThread;
    int rc1 = 0; int rc2 = 0;
    rc1 = pthread_create(&barberThread, NULL, barber, NULL);
    rc2 = pthread_create(&customerThread, NULL, customer, NULL);
	
	if(rc1 != 0)
		cout << "Unable to create barber thread." << endl;
	if(rc2 != 0)	
		cout << "Unable to create customer thread." << endl;
			
	/*Customer_thread first blocked*/
	rc2 = pthread_join(customerThread,NULL);
	rc1 = pthread_join(barberThread,NULL);	
	
	printf("rc1: %d, rc2: %d\n", rc1, rc2);
	cout << "EXITING PROGRAM" << endl;
	return 0;
}



This code generates the following output regardless of the number of chairs input to the program.

$bash: ./Barber 4

        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1
        Customer: number of Customers: 1
        Customer: number of Customers: 2
Barber: number of customers: 2
Barber: number of customers: 1EXITING BARBER!
        Customer: number of Customers: 1EXITING CUSTOMER!

rc1: 0, rc2: 0
EXITING PROGRAM




I'm compiling with following: g++ -g -Wall -lrt -o Barber main.cpp
Yes, it's likely that 'barber' consumes the whole processer time. You need to insert a sleep outside the locking.

After the 'barber' is finished there's no 'amountTime' left for the customer
Topic archived. No new replies allowed.