ch02.4 - Explain - Excercise - Synch
ch02.4 - Explain - Excercise - Synch
Process Synch
Operating System Concepts – 10th Edition 5a.2 Silberschatz, Galvin and Gagne ©2018
1
Critical-Section Problem (Cont.)
Requirements for solution to critical-section problem
Operating System Concepts – 10th Edition 5a.3 Silberschatz, Galvin and Gagne ©2018
Ex0
Consider the following C code for process P1 and P2. a=4, b=0, c=0
(initialization)
Operating System Concepts – 10th Edition 5a.4 Silberschatz, Galvin and Gagne ©2018
2
Ex1
Consider the methods used by processes P1 and P2 for accessing their
critical sections whenever needed, as given below. The initial values of
shared boolean variables S1 and S2 are randomly assigned.
Expl:
• P1 can enter CS only if S1 != S2, and P2 can enter CS only if S1 = S2. => Multual
• Suppose when s1=1 and s2=0 and P1 is not interested to enter into CS but p2
want to enter CS.
P2 is not able to enter CS in this as only when p1 finishes execution, then only p2 can enter
(then only s1 = s2 condition be satisfied).
Progress will not be satisfied when any process which is not interested to enter into the CS
will not allow other interested process to enter into the CS.
Operating System Concepts – 10th Edition 5a.5 Silberschatz, Galvin and Gagne ©2018
Ex3
Consider the following two-process synchronization solution
The shared variable, initialize: turn=0. Which one of the following is TRUE?
a. This is a correct two-process synchronization solution.
b. This solution violates mutual exclusion requirement.
c. This solution violates progress requirement.
d. This solution violates bounded wait requirement.
Expl: turn is changing only after CS for given processes => Mutual
• Suppose turn=0, P0 is not interested to enter into CS but P1 want to enter CS.
• P1 is not able to enter CS in this as only when p0 finishes execution, then only p1
can enter. So, progress requirement is not satisfied.
• P1 can go directly and P1 can go after P0 into critical section. So, Bounded waiting
is satisfied. Option (C) is correct
Operating System Concepts – 10th Edition 5a.6 Silberschatz, Galvin and Gagne ©2018
3
Ex3
Expl:
• Initially, the value of a counting semaphore S=10.
• Now 12 operations of wait() are performed => S= -2
• “x” operations of signal() were performed on this semaphore and final
value of counting semaphore = 7, i.e: x + (-2) = 7 => x = 9.
• So, option (B) is correct.
Operating System Concepts – 10th Edition 5a.7 Silberschatz, Galvin and Gagne ©2018
Ex5
At particular time, the counting semaphore is 10, it will become 7 after:
• (a) 3 operations of signal ();
• (b) 3 operations of wait ();
• (c) 5 operations of signal () và 2 operations of wait();
• (d) 2 operations of signal () và 5 operations of wait().
Which of the following option is correct?
• A. (b); B. (d); C. (b) and (d); D. None of these
Expl:
• Current value of the S= 10
• (a). after 3 operations of signal ();, S= 10+3 = 13
• (b). after 3 operations of wait(); S= 10-3 = 7
• (c) after 5 operations of signal() và 2 operations of wait (); S=10 +5-2 = 13
• (d) 2 operations of signal () và 5 operations of wait(), S = 10 + 2 - 5 = 7.
• Hence option (C) is correct.
Operating System Concepts – 10th Edition 5a.8 Silberschatz, Galvin and Gagne ©2018
4
Ex6
The following program consists of 3 concurrent processes and 3 binary
semaphores.The semaphores are initialized as S0 = 1, S1 = 0, S2 = 0.
A At least twice
B Exactly twice
C Exactly thrice
D Exactly once
Operating System Concepts – 10th Edition 5a.9 Silberschatz, Galvin and Gagne ©2018
Ex6
Consider the following threads, T1, T2, and T3 executing on a single
processor, synchronized using three binary semaphore variables, S1, S2, and
S3, operated upon using standard wait() and signal(). The threads can be
context switched in any order and at any time.
Which initialization of the semaphores would print the sequence
BCABCABCA….?
a. S1 = 1; S2 = 1; S3 = 1
b. S1 = 1; S2 = 1; S3 = 0
c. S1 = 1; S2 = 0; S3 = 0
d. S1 = 0; S2 = 1; S3 = 1
Expl:
• if S1 = 1, S2 = 0, S3 = 0, T2 execute wait(S1); T1, T3 block. Print “B”. Signal(S3)
• S3=1, T1 execute wait(S3); T2 (enter time 2), T3 block. Print”C”. Signal(S2)
• S2=1, T3 execute wait(S2); T2, T1(enter time 2) block. Print”A”. Signal(S1)
• Continue…
• option C is the correct answer
Operating System Concepts – 10th Edition 5a.10 Silberschatz, Galvin and Gagne ©2018