0% found this document useful (0 votes)
38 views5 pages

ch02.4 - Explain - Excercise - Synch

The document discusses process synchronization techniques including the critical section problem and its requirements, solutions like Peterson's algorithm and hardware support. It also contains examples explaining concepts like semaphores, mutual exclusion and progress.

Uploaded by

22110187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

ch02.4 - Explain - Excercise - Synch

The document discusses process synchronization techniques including the critical section problem and its requirements, solutions like Peterson's algorithm and hardware support. It also contains examples explaining concepts like semaphores, mutual exclusion and progress.

Uploaded by

22110187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 2: Process Management

2.4. Process Synch - Excercise

GV: Nguyễn Thị Thanh Vân


Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

Process Synch

 The Critical-Section Problem


 Solution
• Software Solution 1
• Peterson’s Algorithm
• Hardware support for synchronization
 Memory barriers
 Special hardware instructions
– Test-and-Set instruction
– Compare-and-Swap instruction
• Mutex lock
• Semaphores
• Monitors

Operating System Concepts – 10th Edition 5a.2 Silberschatz, Galvin and Gagne ©2018

1
Critical-Section Problem (Cont.)
Requirements for solution to critical-section problem

1. Mutual Exclusion - If process Pi is executing in its CS, then no other


processes can be executing in their critical sections
(Không có hai tiến trình cùng ở trong miền critical-section cùng lúc)
2. Progress - If no process is executing in its CS and there exist some
processes that wish to enter their CS, then the selection of the process
that will enter the critical section next cannot be postponed indefinitely
(Một tiến trình tạm dừng bên ngoài miền critical-section không được ngăn cản các
tiến trình khác vào miền critical-section)
3. Bounded Waiting - A bound must exist on the number of times that
other processes are allowed to enter their CSs after a process has made
a request to enter its CS and before that request is granted
(Không có tiến trình nào phải chờ vô hạn để được vào miền critical-section)
• Assume that each process executes at a nonzero speed
• No assumption concerning relative speed of the n processes

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)

 If the processes P1 and P2 executes concurrently (shared variables a, b


and c), which of the following cannot be the value of ‘c’ after both
processes complete?
a. 4
b. 7
c. 10
d. 13

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.

 Which one of the following statements describes


the properties achieved?
• A. Mutual exclusion but not progress
• B. Progress but not mutual exclusion
• C. Neither mutual exclusion nor progress
• D. Both mutual exclusion and progress

 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

 At a particular time of computation, the value of a counting semaphore is


10. Then 12 operations of wait() and “x” operations of signal() were
performed on this semaphore. If the final value of semaphore is 7, x will be:
a. 8
b. 9
c. 10
d. 11

 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

 How many times will process P0 print '0'?


 Expl:
• S0=1, P0 run:
 wait(S0) => S0--, print ‘0”;
 realease(S1)=> In P1: wait(S1) (do S1=0) hết bị chờ; realease(S0)=> S0++
 maybe P0 run with S0=1
 realease(S2)=> In P2: wait(S2) (do S2=0) hết bị chờ; realease(S0)=> S0++
• S0=2, P0 run: wait(S0)=> S0--, print ‘0’,
• Order (p0 -> p1 -> p2 -> p0): max 2 times print ‘0’
• Order (p0 -> p1 -> p0 -> p2->p0): max 3 times print ‘0’ => At least twice: A is correct

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

You might also like