CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  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.

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