Consider the following code executed by N processes, and where a and b are local variables that can be initialized by individual processes with arbitrary values. Conversely, s1, s2, s3, and s4 are semaphores initialized to 1 (i.e. used as mutexes).
semaphore s1 = 1;
semaphore s2 = 1;
semaphore s3 = 1;
semaphore s4 = 1;
O: repeat:
1: if a > 0
2: wait s1;
else
4: wait s2;
5:
6: b += 1;
7: wait s3;
8:
9: if b < 0 && a <= 0
10: wait s1;
11: else if b = 0 && a > 0
12: wait s2;
13: else
14: wait s4;
15:
16: a += 1;
17: signal s4;
18: signal s3;
19: signal s2;
20: signal s1;
21: forever
a) Is it possible to have a deadlock when N = 1? If so, which values of a and b lead to the deadlock? Otherwise, motivate why it is not possible to have a deadlock.
b) Is it possible to have a deadlock when N = 2? If so, which values of a and b lead to the deadlock, and what interleaving of execution between the two processes leads to the deadlock? Otherwise, motivate why it is not possible to have a deadlock.
c) Keeping N = 2, would it be possible to avoid deadlocks by changing the initialization value of only one of the semaphores used in the code fragment? Specify what semaphore initialization you would change and its new value.
d) Considering the unmodified code fragment when N = 3, is it true that the increment of a at line 16 always happens in a mutually exclusive way among all the processes? Motivate your answer.