Lecture w7 03

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Chapter 6: Synchronization

Tools

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
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
(uninterruptible.)
• Test-and-Set instruction
• Compare-and-Swap instruction

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

Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = true;
return rv:
}
1. Executed atomically
2. Returns the original value of passed parameter
3. Set the new value of passed parameter to true

A thread uses it before entering a critical region. Only two cases:


1) Lock is being held (*target is TRUE): return TRUE and *target remains TRUE
2) Lock is NOT being held: return FALSE, and *target is set to TRUE
So either another thread is in the critical region so *target (TRUE) reflects what the value
should be; or "I" am entering this critical region now, so set *target to TRUE.

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Mutual Exclusion Solution using
test_and_set()
• Shared boolean variable lock, initialized to false
• Solution:
test_and_set code
do {
while (test_and_set(&lock))
boolean test_and_set (boolean *lock)
; /* do nothing */
{
boolean rv = *lock;
/* critical section */
*lock = true;
lock = false; return rv:
/* remainder section */ }
} while (true);

• if there is no process in the CS, then lock is false


• Now assume that P0 want to enter the CS executes while (test_and_set( false))
• this will set lock to true, and return false to the while loop, so loop exists and P0 enters
the CS
• While P0 in CS, P1 want to enter the CS, P1 executes while(test_and_set(true))
• lock remains true, and return true to the while loop, so P1 waits

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
test_and_set()/ Mutual Exclusion

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
test_and_set instruction
• The important characteristics of the test_and_set() instruction is that it is
executed atomically
• If two test_and_set() instructions are executed simultaneously, each on
different core, they will be executed sequentially in some arbitrary order
• Using the Boolean lock

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;

if (*value == expected)
*value = new_value;
return temp;
}

1. Executed atomically
2. Returns the original value of passed parameter value
3. Set the variable value the value of the passed parameter new_value
but only if *value == expected is true. That is, the swap takes place
only under this condition.

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Mutual Exclusion Solution using
compare_and_swap
• Shared integer lock initialized to 0;
• Solution:
while (true){
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */

/* critical section */

lock = 0;

/* remainder section */
}
• The global variable lock is initialized to 0
• The first process that invokes the compare_and_swap() will set
the lock to 1 and enters its critical section
• If another process call the compare_and_swap() it will find the
lock not equal to 0 so it will not be able to enter CS
• When the first process exits its critical section it will set the lock
to 0, after that the waiting process can enter the CS
Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Notes
• The compare_and_swap() satisfies the mutual-exclusion requirement
• It doesn’t satisfy the bounded-waiting requirement
• If the shared resource is locked it will not be made available to another
process until it is unlocked by the first process
• Next slide contains an algorithm that satisfies the bounded-waiting mutual-
exclusion

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Algorithm using test_and_set

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Mutual Exclusion using test_and_set

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Progress using test_and_set

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Bounded waiting using test_and_set

Example: we have 5
processes n = 5
i = 2 in CS,
cyclic order: 3, 4, 0, 1,
Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Bounded-waiting Mutual Exclusion
with compare-and-swap

while (true) {
waiting[i] = true; • Shared data:
key = 1;
while (waiting[i] && key == 1) • waiting [n]
key = compare_and_swap(&lock,0,1); • Where n is number of
waiting[i] = false; process
/* critical section */ • Boolean waiting[n]
j = (i + 1) % n; • and lock
while ((j != i) && !waiting[j]) • The elements in the waiting
j = (j + 1) % n; array are initialized to false,
if (j == i) and the lock variable is
lock = 0; initialized to false
else
waiting[j] = false;
/* remainder section */
}

Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Bounded-waiting Mutual Exclusion
with compare-and-swap

while (true) {
waiting[i] = true; • Mutual-exclusion requirement
key = 1;
while (waiting[i] && key == 1) • A process Pi can enter it critical
key = compare_and_swap(&lock,0,1); section only if waiting[i] ==false
waiting[i] = false; or key==0
/* critical section */ • The value of key can only
j = (i + 1) % n; become 0 if the
while ((j != i) && !waiting[j]) compare_and_swap is
j = (j + 1) % n; executed and lock is set to
if (j == i) 0
lock = 0;
else
waiting[j] = false;
/* remainder section */
}

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Bounded-waiting Mutual Exclusion
with compare-and-swap

while (true) {
waiting[i] = true; • Progress requirement is met
key = 1;
while (waiting[i] && key == 1)
key = compare_and_swap(&lock,0,1); • When a process exit its critical
waiting[i] = false; section either sets lock to 0 or
/* critical section */ sets waiting[j] to false
j = (i + 1) % n;
while ((j != i) && !waiting[j]) • Both allow any process in the
j = (j + 1) % n; waiting state to enter its critical
if (j == i) section to proceed
lock = 0;
else
waiting[j] = false;
/* remainder section */
}

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Bounded-waiting Mutual Exclusion
with compare-and-swap

while (true) {
waiting[i] = true; • Any process in the waiting
key = 1;
while (waiting[i] && key == 1) state will be able to enter its
key = compare_and_swap(&lock,0,1); critical section within n-1 turns
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = 0;
else
waiting[j] = false;
/* remainder section */
}

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

You might also like