Professional Documents
Culture Documents
EXERCISE 1
Process P:
while (true) {
waitForNewRequest();
if(found){
hit+=1;
val=hit;
Respond();
Given the above code, identify the critical section of the code and solve the problem with semaphore.
Suppose hit=100, run the code (with semaphore) with 2 instances of P, i.e., P1, P2, P1. When P1 runs
hit+=1; then P1 runs out of time, and P2 also wants to run hit+=1; Run step by step for P1 and P2
Solution:
while (true) {
waitForNewRequest();
if(found){
Respond();
}
With semaphore:
while (true) {
if(found){
wait(mutex);
hit+=1;
val=hit;
signal(mutex);
P1:
waiting
P1
2
EXERCISE 2
Suppose the total slots in the buffer is 10. There are three processes Reader 1 (R1), Reader 2 (R2) and
Writer (W).
Run steps by steps of the three processes with following order: R1, R2, W, W, R1
Write
Write
3
Signal(mutex) -> mutex=1
Read