Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A)You need to share a resource A with three threads B C and D. Write a critical

ID: 3702924 • Letter: A

Question

A)You need to share a resource A with three threads B C and D. Write a critical section code for each of the threads and use semaphores to guarantee exclusive access by just one of the threads at a time to the resource A, which could be accesses by calling A().

B)Rewrite the critical section code of part A) to allow at most two of the three threads to access the resource at the same time.

C) Is the implementation used in parts A) and B) susceptible to Deadlock, starvation or priority inversion? Why/Why not?

Explanation / Answer

A)

Process accessing A in critical section: Each process gets access to A if s=1 and will wait if s=0. Whenever a process tries to enter CS, P() is called and before entering CS process decreases value of semaphore by 1 and right before finishing CS process increments value of semaphore.

s = 1;

P(s);

//CS

V(s);

P(semaphore s){

while(s==0);

s--;

}

V(semaphore s){

s++;

}

B) For allowing two processes to enter into CS together, initialize semaphore s=2

s = 2;

P(s);

//CS

V(s);

P(semaphore s){

while(s==0);

s--;

}

V(semaphore s){

s++;

}

C)

Deadlock: Yes, if two processes i & j are in CS for resources A & B and require access to B & A respectively then both will not leave CS and will wait indefinitely.

Starvation: Yes, if a process take really long time then it may starve a waiting process since there is no preemption of process from CS

Priority inversion: Yes, if a lower priority process enters into CS first then there is a possibility that a higher priority process will enter into wait cycle to wait for lower priority process.