





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
Silberschatz, Galvin and Gagne © th
To present the concept of process synchronization.
To introduce the critical-section problem, whose solutions
can be used to ensure the consistency of shared data
To present both software and hardware solutions of the
critical-section problem
To examine several classical process-synchronization
problems
To explore several tools that are used to solve process
synchronization problems
Silberschatz, Galvin and Gagne © th
Process synchronization is required when multiple processes or threads share
resources (memory, variables, files, CPU, etc.) to prevent race conditions, data
inconsistency, and deadlocks.
Two types of Processes in Synchronizations
Does not share data/resources with other processes. Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Shared Variables
updates.
Shared Memory
data corruption.
consumes it.
Shared Code Execution
occur.
Shared Resources (Files, Printers, etc.)
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Mutex (Mutual Exclusion Object)
Allows only one process/thread to access a resource at
a time.
Semaphore
Manages multiple processes trying to access a limited
resource.
Monitor
controlled access.
Atomic Operations
Ensures operations on shared data happen indivisibly.
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
while (true) {
/* produce an item in next produced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
The Critical Section is the part of a program where shared resources
are modified.
Example:
Bank Account Transaction: Two processes transferring money at
the same time can lead to incorrect balance calculations.
data corruption may happen.
Goal: Ensure that only one process is inside the critical section at a
time. Each process follows these steps:
shared file).
Silberschatz, Galvin and Gagne © th
i
do {
while (turn == j);
critical section
turn = j;
remainder section
} while (true);
Silberschatz, Galvin and Gagne © th
no other process should be allowed in.
user shouldn’t be able to access the same account
simultaneously.
process should be allowed to enter. The system must not
postpone a process forever if it wants to enter the critical
section.
cross without unnecessary delay.
section must eventually get its turn. There should be a limit on
how many times other processes can enter before a waiting
process gets a chance. Prevents starvation, where a process
keeps waiting indefinitely.
skipping ahead, one customer might never get to pay.
Bounded waiting ensures fairness!
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
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
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
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 P
i
is
ready!
Silberschatz, Galvin and Gagne © th
i
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Algorithm Steps:
Process Pi sets flag[i] = true (it wants to enter).
wants to enter).
Wait until the other process is not in the critical section
(flag[j] == false).
Silberschatz, Galvin and Gagne © th
Many systems provide hardware support for implementing the
critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
n buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value n
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
A data set is shared among a number of concurrent processes
Readers – only read the data set; they do not perform any updates
Writers – can both read and write
Problem – allow multiple readers to read at the same time
Only one single writer can access the shared data at the same time
Several variations of how readers and writers are considered – all
involve some form of priorities
Silberschatz, Galvin and Gagne © th
Philosophers spend their lives alternating thinking and eating
Don’t interact with their neighbors, occasionally try to pick up 2
chopsticks (one at a time) to eat from bowl
Need both to eat, then release both when done
In the case of 5 philosophers
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Silberschatz, Galvin and Gagne © th
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up the chop sticks only if
both are available (picking must be done in a critical
section.
Use an asymmetric solution -- an odd-numbered
philosopher picks up first the left chopstick and then
the right chopstick. Even-numbered philosopher picks
up first the right chopstick and then the left chopstick.
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Each philosopher i invokes the operations pickup() and
putdown() in the following sequence:
DiningPhilosophers.pickup(i);
DiningPhilosophers.putdown(i);
No deadlock, but starvation is possible
Solution to Dining Philosophers (Cont.)
Silberschatz, Galvin and Gagne © Operating System Concepts – 9
th Edition
Solaris
Windows
Linux
Pthreads
Silberschatz, Galvin and Gagne © th
Implements a variety of locks to support multitasking, multithreading
(including real-time threads), and multiprocessing
Uses adaptive mutexes for efficiency when protecting data from short
code segments
Starts as a standard semaphore spin-lock
If lock held, and by a thread running on another CPU, spins
If lock held by non-run-state thread, block and sleep waiting for signal of
lock being released
Uses readers-writers locks when longer sections of code need
access to data
Uses turnstiles to order the list of threads waiting to acquire either an
adaptive mutex or reader-writer lock
Turnstiles are per-lock-holding-thread, not per-object
Priority-inheritance per-turnstile gives the running thread the highest of
the priorities of the threads in its turnstile
Silberschatz, Galvin and Gagne © th
Uses interrupt masks to protect access to global resources on
uniprocessor systems
Uses spinlocks on multiprocessor systems
Spinlocking-thread will never be preempted
Also provides dispatcher objects user-land which may act
mutexes, semaphores, events, and timers
Events
An event acts much like a condition variable
Timers notify one or more thread when time expired
Dispatcher objects either signaled-state (object available)
or non-signaled state (thread will block)