Monitor Implementation Using Semaphores

Viewed 40

So i'm facing a Problem Understanding Monitor Implementation Using Semaphores , i Understod that Conditions of monitor is used for Synchronization problems not the Critical Section one , Variables

semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next_count = 0;

Each function F will be replaced by

wait(mutex);

...
body of F
...

if (next_count > 0)
signal(next);
else
signal(mutex);

Mutual exclusion within a monitor is ensured

i Can't understand that implementation well . why should i signal another process if it's waiting and not Just signal my self ?! .

and i can't understand the x.wait() and x.signal() codes at all

x.wait:

x_count++;
if (next_count > 0)
    signal(next);
else
    signal(mutex);
wait(x sem);
x_count--;

x.signal():

if (x_count > 0) {
    next_count++;
    signal(x_sem);
    wait(next);
    next_count--;
}

i'm trying to understand this topic i searched so much but it still isn't clear

0 Answers
Related