1,966 views

2 Answers

3 votes
3 votes
Do verify my solution. I have based my solution on the READERS-WRITERS problem (First).

semaphore pond ; // binary. mutual exclusion to the pond.

semaphore tiger; // for updating the tiger-count;

semaphore elephant; // for updating the elephant-count;

int tigercount = 0 ;

int elephantcount = 0 ;

 

tiger() {

     P(tiger);

     tigercount++ ;

     if (tigercount == 1) { // if the first tiger comes to the pond, he will reserve the pond for tigers.

          P (pond);

      }

      V(tiger);

      // Drink Water

      P(tiger);

      tigercount-- ;

      if (tigercount == 0) { // if it is the last tiger, then leave the pond for elephants.

      V(pond);

    }

    V(tiger);

}

 

The elephant process will be similar.
0 votes
0 votes

Don't know if this is right or not. Please check.

Two variables: t = 0 and e = 0

For tiger:

P(mutex);

while(e != 0);

t++;

e=0;

V(mutex);

if(t != 0 && e == 0)

        drink();

t--;

For elephant:

P(mutex);

while(t != 0);

e++;

t=0;

V(mutex);

if(e != 0 && t == 0)

        drink();

e--;

Related questions

0 votes
0 votes
1 answer
2
Mrityudoot asked Jan 27
178 views
Can a counting semaphore acquire a negative value?S = 2;15 P operations done, should the semaphore be 0 or -13
0 votes
0 votes
1 answer
3
N3314nch41 asked Sep 10, 2023
341 views
How to approach synchronization (specifically semaphore) question, there size are really intimidating and i’m unable to decode the code written? What to do??