0% found this document useful (0 votes)
8 views38 pages

Chapter 6: Synchronization Tools: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts 10 Edition

Chapter 6 discusses synchronization tools in operating systems, focusing on the critical-section problem and solutions like mutex locks and semaphores. It outlines the importance of maintaining data consistency during concurrent process execution and describes various attempts to solve the critical-section problem, including Peterson's solution. The chapter also evaluates the effectiveness of these synchronization mechanisms in different contention scenarios.

Uploaded by

sknlilzawaj
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)
8 views38 pages

Chapter 6: Synchronization Tools: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts 10 Edition

Chapter 6 discusses synchronization tools in operating systems, focusing on the critical-section problem and solutions like mutex locks and semaphores. It outlines the importance of maintaining data consistency during concurrent process execution and describes various attempts to solve the critical-section problem, including Peterson's solution. The chapter also evaluates the effectiveness of these synchronization mechanisms in different contention scenarios.

Uploaded by

sknlilzawaj
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/ 38

Chapter 6: Synchronization

Tools

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 6: Synchronization Tools
Background
The Critical-Section Problem
Peterson’s Solution
Hardware Support for Synchronization
Mutex Locks
Semaphores
Liveness
Evaluation

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

Describe the critical-section problem and illustrate a race


condition
Demonstrate how mutex locks, semaphores can be used to
solve the critical section problem
Evaluate tools that solve the critical-section problem in low-.
Moderate-, and high-contention scenarios

Operating System Concepts – 10th Edition 6.3 Silberschatz, Galvin and Gagne ©2018
Background
Processes can execute concurrently
May be interrupted at any time, partially completing
execution
Concurrent access to shared data may result in data
inconsistency
Maintaining data consistency requires mechanisms to ensure
the orderly execution of cooperating processes
Illustration of the problem:
Suppose that we wanted to provide a solution to the
consumer-producer problem that fills all the buffers. We can
do so by having an integer counter that keeps track of the
number of full buffers. Initially, counter is set to 0. It is
incremented by the producer after it produces a new buffer
and is decremented by the consumer after it consumes a
buffer.

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

while (true) {
/* produce an item in next produced */

while (counter == BUFFER_SIZE)


; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}

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

while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}

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

counter++ could be implemented as


register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2

Consider this execution interleaving with “count = 5” initially:


S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 – 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 }
S5: consumer execute counter = register2 {counter = 4}

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

Race condition: a situation when several processes


access and manipulate the same data concurrently
and the outcome of the execution depends on the
particular order in which the access takes place.
Informally, a critical section is a code segment that
accesses shared variables and has to be executed
as an atomic action.
The critical section problem refers to the problem of
how to ensure that at most one process is executing
its critical section at a given time.
Important: Critical sections in different threads are
not necessarily the same code segment!

Operating System Concepts – 10th Edition 6.8 Silberschatz, Galvin and Gagne ©2018
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables,
updating table, writing file, etc
When one process in critical section, no other may
be in its critical section
Critical section problem is to design protocol to solve
this
Each process must ask permission to enter critical
section in entry section, may follow critical section with
exit section, then remainder section

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

General structure of process Pi


It is also assumed that (1) after a thread enters a critical section, it will
eventually exit the critical section; (2) a thread may terminate in the
non-critical section.

Operating System Concepts – 10th Edition 6.10 Silberschatz, Galvin and Gagne ©2018
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections
2. Progress - If no process is executing in its critical section and
there exist some processes that wish to enter their critical
section, then the selection of the processes that will enter the
critical section next cannot be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of
times that other processes are allowed to enter their critical
sections after a process has made a request to enter its critical
section and before that request is granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the n
processes

Operating System Concepts – 10th Edition 6.11 Silberschatz, Galvin and Gagne ©2018
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or non-
preemptive
Preemptive – allows preemption of process when running
in kernel mode
Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
Essentially free of race conditions in kernel mode

Operating System Concepts – 10th Edition 6.12 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 1)
The global variable turn is used to indicate the next process to enter the
critical section. The initial value of turn can be 1 or 2.
Shared
int turn = 2;
Process 1 (P1) Process 2 (P2)

while (true) { while (true) {


while (turn == 2) ; (1) while (turn == 1) ; (1)
critical section (2) critical section (2)
turn = 2; (3) turn = 1; (3)
Other code (4) Other code (4)
} }

Source: W6 L3 Software solutions for critical sections


Operating System Concepts – 10th Edition 6.13 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 1 FAIL)
Comments
P1 P2
P2 exits the while loop
P2 enters the critical section
turn is set to 1
P2 terminates in non-critical section

P1 exits the while loop


P1 enters the critical section
turn is set to 2
P1 executes non-critical section
P1 repeats (1) forever

Operating System Concepts – 10th Edition 6.14 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 2)

Shared
P2_inside=false;
P1_inside=false

Process 1 (P1) Process 2 (P2)


while (true) { while (true) {
while (P2_inside == true) ; (1) while (P1_inside == true) ; (1)
P1_inside = true; (2) P2_inside = true; (2)
critical section (3) critical section (3)
P1_inside=false; (4) P2_inside=false; (4)
Other code (5) Other code (5)
} }

Source: W6 L3 Software solutions for critical sections


Operating System Concepts – 10th Edition 6.15 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 2 FAIL)
P1 P2
Comments
(1)
Context switch P1 exits the while loop
(1) P2 exits the while loop
(2)
(3) P2 sets P2_inside to true
Context switch P2 enters the critical section
(2)
(3) P1 sets P1_inside to true
P1 enters the critical section at the same
time with P2!!

Operating System Concepts – 10th Edition 6.16 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 3)

Shared
P1_wants_to_enter=false;
P2_wants_to_enter=false

Process 1 (P1) Process 2 (P2)


while (true) { while (true) {
P1_wants_to_enter=true; (1) P2_wants_to_enter=true; (1)
while (P2_wants_to_enter == true) ; (2) while (P1_wants_to_enter == true) ; (2)
critical section (3) critical section (3)
P1_wants_to_enter=false; (4) P2_wants_to_enter=false; (4)
Other code (5) Other code (5)
} }

Source: W6 L3 Software solutions for critical sections


Operating System Concepts – 10th Edition 6.17 Silberschatz, Galvin and Gagne ©2018
Software Solution (Attempt 3 FAIL)
P1 P2
Comments

(1)
Context switch P1_wants_to_enter is set to true
(1) P2_wants_to_enter is set to true
Context switch

(2) P1 loops forever


Context switch
(2) P2 loops forever

Operating System Concepts – 10th Edition 6.18 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
Not guaranteed to work on modern architectures! (But good
algorithmic description of solving the problem)
Two process solution
Assume that the load and store machine-language
instructions are atomic; that is, cannot be interrupted
The two processes share two variables:
int turn;
boolean flag[2]

The variable turn indicates whose turn it is to enter the critical


section
The flag array is used to indicate if a process is ready to enter
the critical section. flag[i] = true implies that process Pi is
ready!

Operating System Concepts – 10th Edition 6.19 Silberschatz, Galvin and Gagne ©2018
Algorithm for Process Pi

while (true){
flag[i] = true; (1)
turn = j; (2)
while (flag[j] && turn = = j) (3)
;

/* critical section */ (4)

flag[i] = false; (5)

/* remainder section */ (6)

Operating System Concepts – 10th Edition 6.20 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution (Cont.)
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

Operating System Concepts – 10th Edition 6.21 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
Pi Pj
Comments
Flag[i] is set to true

Flag[j] is set to true


turn is set to i
Pj enters the while loop

turn is set to Pj
Pi enters the while loop

Pj exits while loop


Pj enters the critical section
Flag[j] is set to false
Pj executes the non-critical section
Flag[j] is set to true
turn is set to i
Pj enters while loop

Pi exits while loop


Pi enters critical section
Flag[i] is set to false
Pi executes the non-critical section

Pj exits the while loop


Pj enters critical section
Operating System Concepts – 10th Edition 6.22 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
Although useful for demonstrating an algorithm, Peterson’s Solution is not
guaranteed to work on modern architectures.
Understanding why it will not work is also useful for better understanding
race conditions.
To improve performance, processors and/or compilers may reorder
operations that have no dependencies.
For single-threaded this is ok as the result will always be the same.
For multithreaded the reordering may produce inconsistent or unexpected
results!

Operating System Concepts – 10th Edition 6.23 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
Two threads share the data:

boolean flag = false;


int x = 0;
Thread 1 performs

while (!flag)
;
print x
Thread 2 performs

x = 100;
flag = true

What is the expected output?

Operating System Concepts – 10th Edition 6.24 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
100 is the expected output.
However, the operations for Thread 2 may be reordered:

flag = true;
x = 100;
If this occurs, the output may be 0!
The effects of instruction reordering in Peterson’s Solution

This allows both processes to be in their critical section at the same time!

Operating System Concepts – 10th Edition 6.25 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 6.26 Silberschatz, Galvin and Gagne ©2018
Solution to Critical-section Problem Using Locks

while (true) {
acquire lock

critical section

release lock

remainder section
}

Operating System Concepts – 10th Edition 6.27 Silberschatz, Galvin and Gagne ©2018
Mutex Locks
OS designers build software tools to solve critical section
problem
Simplest is mutex lock
Protect a critical section by first acquire() a lock then
release() the lock
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Atomic = non-interruptable.

But this solution requires busy waiting


This lock therefore called a spinlock

Operating System Concepts – 10th Edition 6.28 Silberschatz, Galvin and Gagne ©2018
Mutex Lock Definitions
acquire() {
while (!available)
; /* busy wait */
available = false;
}

release() {
available = true;
}

These two functions must be implemented atomically.

Operating System Concepts – 10th Edition 6.29 Silberschatz, Galvin and Gagne ©2018
Semaphore
Synchronization tool that provides more sophisticated ways for process 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++;
}

Operating System Concepts – 10th Edition 6.30 Silberschatz, Galvin and Gagne ©2018
Semaphore Usage
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 solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Can implement a counting semaphore S as a binary semaphore

Operating System Concepts – 10th Edition 6.31 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 6.32 Silberschatz, Galvin and Gagne ©2018
Semaphore Implementation with no Busy waiting

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
typedef struct {
int value;
struct process *list;
} semaphore;

Operating System Concepts – 10th Edition 6.33 Silberschatz, Galvin and Gagne ©2018
Implementation with no Busy waiting (Cont.)

wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}

signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}

Operating System Concepts – 10th Edition 6.34 Silberschatz, Galvin and Gagne ©2018
Problems with Semaphores

Incorrect use of semaphore operations:

signal (mutex) …. wait (mutex)

wait (mutex) … wait (mutex)

Omitting of wait (mutex) and/or signal (mutex)

These – and others – are examples of what can occur when


sempahores and other synchronization tools are used
incorrectly.

Operating System Concepts – 10th Edition 6.35 Silberschatz, Galvin and Gagne ©2018
Liveness
Processes may have to wait indefinitely while trying to acquire a
synchronization tool such as a mutex lock or semaphore.
Waiting indefinitely violates the progress and bounded-waiting criteria
discussed at the beginning of this chapter.
Liveness refers to a set of properties that a system must satisfy to ensure
processes make progress.
Indefinite waiting is an example of a liveness failure.

Operating System Concepts – 10th Edition 6.36 Silberschatz, Galvin and Gagne ©2018
Liveness
Deadlock – two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);

Consider if P0 executes wait(S) and P1 wait(Q). When P0 executes


wait(Q), it must wait until P1 executes signal(Q)
However, P1 is waiting until P0 execute signal(S).
Since these signal() operations will never be executed, P0 and P1 are
deadlocked.

Operating System Concepts – 10th Edition 6.37 Silberschatz, Galvin and Gagne ©2018
End of Chapter 6

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy