semaphores & Concurrent Programming
Note that this is actually C--, which is very close to C++. I have an assignment to code a Restroom problem using semaphores. Here are the details. The restroom is unisex. Only males or females can be in the restroom at a time. The restroom has a max capacity of 4 people. FIFO is not an issue for people waiting. I am having problems with keeping men out of the restroom when woman are in there and women out when men are in. I'm sure it's probably with my semamphores but i can't figure out which one.
Code:
const int Delayx = 60;
int i;
int restroom = 0;
int Menwaiting = 0;
int Womenwaiting = 0;
semaphore max_capacity;
semaphore woman;
semaphore man;
semaphore mutex;
void Delay(void)
{
int DelayTime;
DelayTime = random(Delayx);
for (i = 0; i<DelayTime; i++);
}
void Woman(void)
{
for(;;){
Womenwaiting++;
wait(woman);
if(restroom > 4){
wait(max_capacity);}
else{
//wait(woman);
wait(mutex);
cout << "A Woman has entered Restroom"<<endl;
cout << "People in the Restroom:" << restroom++ <<endl <<endl;
Womenwaiting--;
signal(mutex);
Delay();
wait(mutex);
cout << "A woman has exited Restroom"<<endl;
cout << "People in the Restroom:" << restroom-- <<endl<<endl;
signal(mutex);
if(Menwaiting > Womenwaiting){
signal(man);
}
else{
signal(woman);
}
signal(max_capacity);
//signal(man);
}
}
}
void Man(void)
{
for(;;){
Menwaiting++;
wait(man);
if(restroom > 4){
wait(max_capacity);}
//wait(man);
else{
wait(mutex);
cout <<"A Man has entered the Restroom"<<endl;
cout <<"People in the Restroom:" << restroom++ <<endl<<endl;
Menwaiting--;
signal(mutex);
Delay();
wait(mutex);
cout << "A man has exited the Restroom"<<endl;
cout <<"People in the Restroom:" << restroom-- <<endl<<endl;
signal(mutex);
if(Womenwaiting > Menwaiting){
signal(woman);
}
else{
signal(man);
}
signal(max_capacity);
//signal(woman);
}
}
}
void main()
{
initialsem(woman,1);
initialsem(man,1);
initialsem(max_capacity,1);
initialsem(mutex,1);
cobegin
{
Woman(); Woman(); Woman(); Woman(); Woman(); Man(); Man(); Man(); Man(); Man();
}
}
Re: semaphores & Concurrent Programming
Quote:
Originally Posted by
FeralDruidonWoW
Note that this is actually C--, which is very close to C++.
I'm not familiar with C--, but this appears to be a single threaded program. Hence, the use of semaphores is useless.
Re: semaphores & Concurrent Programming
our assignment was to use semaphores. This is going to be run with an interpreter called BACI. My main focus is understanding why I do not have mutual exclusion and why I never have more then 2 people in the bathroom at a time. I've updated my original post with the new code. I do not understand why men and women are entering the bathroom at the same time.
Re: semaphores & Concurrent Programming
Quote:
Originally Posted by
FeralDruidonWoW
our assignment was to use semaphores.
Then probably the idea is to use threads as well.
Quote:
Originally Posted by
FeralDruidonWoW
why I never have more then 2 people in the bathroom at a time. I've updated my original post with the new code. I do not understand why men and women are entering the bathroom at the same time.
[/quote]
If your code is single threaded then you should never have more than one person in the bathroom.
Re: semaphores & Concurrent Programming
Quote:
Originally Posted by
FeralDruidonWoW
Code:
semaphore max_capacity;
semaphore woman;
semaphore man;
semaphore mutex;
Hm, I don't know C--, but that doesn't look quite right to me. If all the conditions (access criteria) have their own keys and these keys are up for grasp for any client thread without any syncronization, then you would have a race condition. On the other hand, if both woman and man semaphores need to wait for one another like above, then you have a deadlock.
Probably the easiest way is to have each client thread access through the semaphore, and seperate 'mutual exclusion' of men and women out of the threading logic so that in this way, the real mutex object is the key to the tolilet like it is supposed to be