Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Semaphores Monitors

Semaphore mutex = new


Semaphore (1);
Customer acquire and release once
Employee acquire and release twice

Transition diagrams and mutual exclusion

Mutex: at any point in time there is at least


one thread in the critical section
Absence of livelock
Abscence of livelock: if various threads try -------------------------------------
to enter the critical section, at least one of // prints a before f and f
them will succeed
// before c
Free from starvation: A thread trying to
enter its critical section will eventually be able import java.util.concurrent.Semaphore;
to
------------------------------------- Semaphore aBeforeF = new Semaphore(0);
Assume that the print command is atomic. Semaphore fBeforeC = new Semaphore(0);
Build the transition system and then exhibit
all possible paths of execution of the
Thread.start {
following program
print("A");
aBeforeF.release();
fBeforeC.acquire();
print("B");
print("C");
}
Thread.start {
aBeforeF.acquire();
print("E");
fBeforeC.release();
print("F");
print("G");
}
-------------------------------------

----
More Monitors More Semaphores
2. import java.util.concurrent.Semaphore;

int y = 0, z = 0;
Semaphore twoPossibilities = new
Semaphore(0);

Thread.start {
y = 1;
twoPossibilities.release();
z = 2;
}
Thread.start {
int x;
twoPossibilities.acquire();
x = y + z;
print(x)
}
-------------------------------------

-------------------------------------
3.
3.
-------------------------------------
Bar exercise with monitors

-------------------------------------
Train station
-------------------------------------
Produce = 1; consume = 0;
-------------------------------------
4.

-------------------------------------

You might also like