[go: up one dir, main page]

0% found this document useful (0 votes)
112 views2 pages

Midterm Cheat Sheet

The document discusses semaphores and monitors in concurrent programming. It provides examples of using semaphores to control thread execution order and access to shared resources. Specifically, it shows how semaphores can be used to ensure that: 1) Thread A prints before Thread B 2) Threads accessing a shared variable do so without race conditions 3) A producer-consumer problem can be solved to prevent overproduction

Uploaded by

spop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views2 pages

Midterm Cheat Sheet

The document discusses semaphores and monitors in concurrent programming. It provides examples of using semaphores to control thread execution order and access to shared resources. Specifically, it shows how semaphores can be used to ensure that: 1) Thread A prints before Thread B 2) Threads accessing a shared variable do so without race conditions 3) A producer-consumer problem can be solved to prevent overproduction

Uploaded by

spop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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