Professional Documents
Culture Documents
Mohamed Abdelrahman Anwar - 20011634 - Sheet 5
Mohamed Abdelrahman Anwar - 20011634 - Sheet 5
Mohamed Abdelrahman Anwar - 20011634 - Sheet 5
Operating Systems
Sheet 5
Concurrency: Mutual Exclusion and
Synchronization
5.4 SEMAPHORES
6. One difference is in second definition s.count initial value
expresses number of processes that can be on the ready list,
when s.count is negative it gives number of processes waiting,
this information cannot be obtained from the first definition
but both definitions are equivalent in functionality.
General questions
7.
a. The lower bound on the final value of tally is n, which is
the number of times that tally is incremented by each
process. Each process runs a loop that increments tally n
times, so the total number of increments is 2n. Therefore,
The upper bound on the final value of tally is 2n.
b. Allowing an arbitrary number of processes to execute in
parallel under the assumptions of part (a) would increase
the upper bound on the final value of tally. Specifically, if
there are k processes executing in parallel, then each
process would increment tally n times, resulting in a total
of kn increments. Therefore, the upper bound on the final
value of tally would be kn, which is greater than the upper
bound of 2n in part (a).
However, the lower bound on the final value of tally would
remain the same, since each process still increments tally n
times. Therefore, the range of final values of tally would be [n,
kn], where k is the number of processes executing in parallel.
The exact final value of tally would depend on the relative
speeds of the processes and the order in which they execute.
8.
The proposed solution is an implementation of the Peterson's
algorithm for two processes, which uses a shared turn variable
and per-process blocked flags to enforce mutual exclusion.
However, there is a problem with the inner while loop:
while (blocked[1-id]) /* do nothing */;
int turn;
while (true) {
blocked[id] = true;
turn = id;
blocked[id] = false;
yield();
void main() {
blocked[0] = false;
blocked[1] = false;
turn = 0;