Due: March 16
a.
Give an implementation of binary semaphores using general semaphores.
Hint. Some of you may be tempted to write code like
... signal(s); if (s > 1) s = 1; ...
but this would be incorrect. The only operations allowed on semaphores (outside of initialization) are signal and wait.
b. Consider the following implementation of general semaphore s using binary semaphores.
gensem s = initval is replaced with
struct s { int count = 0; int s = initval; binsem mutex = 1; binsem block = 0; }
wait(s) is replaced with
wait(s.mutex); if (s.s == 0) { s.count++; signal(s.mutex); wait(s.block); } else { s.s--; signal(s.mutex); }
signal(s) is replaced with
wait(s.mutex); if (s.count == 0) { s.s++; signal(s.mutex); } else { s.count--; signal(s.block); signal(s.mutex); }
This implementation suffers from a liveness problem (that is, there is a sequence of events that could leave a process waiting forever). Trace an execution which ends with s>0, count=0, and a process waiting on the semaphore block.
Available A
B
C
D
1
5
2
0
Max
Allocation
A
B
C
D
A
B
C
D
P0
0
0
1
2
0
0
1
2
P1
1
7
5
0
1
0
0
0
P2
2
3
5
6
1
3
5
4
P3
0
6
5
2
0
6
3
2
P4
0
6
5
6
0
0
1
4
Answer the following questions using the banker's algorithm:
- What is the content of the matrix Need?
- Is the system in a safe state? (If so, show a safe sequence; if not, be specific about the violating conditions.)
- If a request from process P1 arrives for (0, 4, 2, 0), can the request be granted immediately? (Be sure and justify your answer.)