CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    49

    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();
    	}
    
    }
    Last edited by FeralDruidonWoW; October 3rd, 2010 at 10:52 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: semaphores & Concurrent Programming

    Quote Originally Posted by FeralDruidonWoW View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Mar 2009
    Posts
    49

    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.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: semaphores & Concurrent Programming

    Quote Originally Posted by FeralDruidonWoW View Post
    our assignment was to use semaphores.
    Then probably the idea is to use threads as well.
    Quote Originally Posted by FeralDruidonWoW View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: semaphores & Concurrent Programming

    Quote Originally Posted by FeralDruidonWoW View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured