Module 4 - Synchronization Tools
Module 4 - Synchronization Tools
26/12/2021
College of Computing and Informatics
OPERATING SYSTEMS
Operating Systems
Module 4
Peterson’s Solution
Mutex Locks
Semaphores
WEEKLY LEARNING OUTCOMES
flag[i] = true;
turn = j;
while (flag[j] && turn = = j)
;
/* critical section */
flag[i] = false;
/* remainder section */
}
CORRECTNESS OF PETERSON’S SOLUTION
Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
PETERSON’S SOLUTION
Although useful for demonstrating an algorithm, Peterson’s Solution is not
guaranteed to work on modern architectures.
To improve performance, processors and/or compilers may reorder operations
that have no dependencies
Understanding why it will not work is useful for better understanding race
conditions.
For single-threaded this is ok as the result will always be the same.
For multithreaded the reordering may produce inconsistent or unexpected
results!
SYNCHRONIZATION HARDWARE
Many systems provide hardware support for implementing the critical section
code.
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
We will look at three forms of hardware support:
1. Hardware instructions
2. Atomic variables
HARDWARE INSTRUCTIONS
Special hardware instructions that allow us to either test-and-modify the content
of a word, or to swap the contents of two words atomically (uninterruptedly.)
Test-and-Set instruction
Compare-and-Swap instruction
The test_and_set Instruction
Definition
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = true;
return rv:
}
Properties
Executed atomically
Returns the original value of passed parameter
Set the new value of passed parameter to true
Solution Using test_and_set()
Shared boolean variable lock, initialized to false
Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
critical section
release lock
remainder section
}
SEMAPHORE
Synchronization tool that provides more sophisticated ways (than Mutex locks) for processes to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
SEMAPHORE
Counting semaphore – integer value can range over an unrestricted domain
Binary semaphore – integer value can range only between 0 and 1
Same as a mutex lock
Can implement a counting semaphore S as a binary semaphore
With semaphores we can solve various synchronization problems
SEMAPHORE EXAMPLE
Solution to the CS Problem
Create a semaphore “mutex” initialized to 1
wait(mutex);
CS
signal(mutex);
Consider P1 and P2 that with two statements S1 and S2 and the requirement that S1 to happen
before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
SEMAPHORE IMPLEMENTATION
Must guarantee that no two processes can execute the wait() and signal() on the
same semaphore at the same time
Thus, the implementation becomes the critical section problem where the wait
and signal code are placed in the critical section
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore
this is not a good solution
SEMAPHORE IMPLEMENTATION WITH NO BUSY WAIT
With each semaphore there is an associated waiting queue
Each entry in a waiting queue has two data items:
Value (of type integer)
Pointer to next record in the list
Two operations:
block – place the process invoking the operation on the appropriate waiting
queue
wakeup – remove one of processes in the waiting queue and place it in the ready
queue
Main Reference
Chapter 6: Synchronization Tools
(Operating System Concepts by Silberschatz, Abraham, et al. 10th ed.,
ISBN: 978-1-119-32091-3, 2018)
Additional References
Chapter 2.3
Chapter 2.5
(Modern Operating Systems by Andrew S. Tanenbaum and Herbert Bos. 4th
ed., ISBN-10: 0-13-359162-X, ISBN-13: 978-0-13-359162-0, 2015)
sentation is mainly dependent on the textbook: Operating System Concepts by Silberschatz, Abrah
Thank You