Unit 2 Process Synchronization
Unit 2 Process Synchronization
Process Synchronization
Introduction
• What is Process Synchronization?
• Race Conditions
• Critical Sections
• Control Synchronization and Indivisible Operations
• Synchronization Approaches
• Structure of Concurrent Systems
Introduction (continued)
• Classic Process Synchronization Problems
• Algorithmic Approach to Implementing Critical Sections
• Semaphores
• Monitors
• Case Studies of Process Synchronization
6.3
3
What is Process Synchronization?
• The term process is a generic term for both a process
and a thread
6.4
4
6.5
5
Race Conditions
• Uncoordinated accesses to shared data may affect
consistency of data
• Consider processes Pi and Pj that update the value of
ds through operations ai and aj , respectively:
Operation ai : ds := ds + 10; Let fi(ds) represent its result
Operation aj : ds := ds + 5; Let fj(ds) represent its result
– What situation could arise if they execute concurrently?
6.6
6
An Example of Race Condition
Race conditions
in cases 2 and 3
6.7
7
Race Conditions (continued)
• Race conditions in Figure 6.2 are prevented by
ensuring that operations ai and aj of Definition 6.2 do
not execute concurrently
– This is called mutual exclusion
• Data access synchronization is coordination of
processes to implement mutual exclusion over shared
data
6.8
8
Critical Sections
• Mutual exclusion is implemented by using critical
sections (CS) of code
6.10
10
Properties of a Critical Section
Implementation
6.12
12
Control Synchronization and Indivisible
Operations (continued)
6.13
13
Control Synchronization and Indivisible
Operations (continued)
• Naive signaling in previous example does not work
– Pi may face indefinite blocking in some situations
6.15
15
Synchronization Approaches
• Looping versus Blocking
• Hardware Support for Process Synchronization
• Algorithmic Approaches, Synchronization Primitives,
and Concurrent Programming Constructs
6.16
16
Looping versus Blocking
• Busy wait:
6.17
17
Looping versus Blocking (continued)
• To avoid busy waits, a process waiting for entry to a CS
is put in blocked state
– Changed to ready only when it can enter the CS
6.20
20
Algorithmic Approaches,
Synchronization Primitives, and
Concurrent Programming Constructs
• Algorithmic Approaches
– For implementing mutual exclusion
– Independent of hardware or software platform
• Busy waiting for synchronization
• Synchronization Primitives
– Implemented using indivisible instructions
– E.g., wait and signal of semaphores
• Problem: can be used haphazardly
• Concurrent Programming Constructs
– Monitors
6.21
21
Structure of Concurrent Systems
• Three key components:
– Shared data
• Application data used and manipulated by processes
• Synchronization data
– Operations on shared data
• Convenient unit of code which accesses and manipulates
shared data
– A synchronization operation is on synchronization data
– Interacting processes
• A snapshot of a concurrent system is a view of the
system at a specific time instant
6.22
22
Structure of Concurrent Systems
(continued)
6.25
25
Producers-Consumers with Bounded
Buffers
6.28
28
Producers-Consumers with Bounded
Buffers (continued)
6.29
29
Readers and Writers
6.31
31
Dining Philosophers
6.32
32
Dining Philosophers (continued)
6.35
35
Two-Process Algorithms
6.38
38
Two-Process Algorithms (continued)
6.39
39
n-Process Algorithms
6.41
41
Semaphores
6.43
43
Uses: Mutual Exclusion
6.44
44
Uses: Bounded Concurrency
• Implemented by initializing a semaphore sem_c to c
• Every process wishing to perform opi performs a
wait(sem_c) before performing opi and a signal(sem_c)
after performing it
• Up to c processes can concurrently perform opi
6.45
45
Uses: Signaling between Processes
6.46
46
Producers-Consumers Using
Semaphores
6.48
48
Producers-Consumers Using
Semaphores (continued)
• Solution to n-buffer producers–consumers problem:
6.49
49
Readers-Writers Using Semaphores
• Significance of counters:
– runread: Number of readers reading
– totread: Number of readers wishing to read or reading
– Similarly runwrite and totwrite
6.50
50
Implementation of Semaphores
6.52
52
Monitors
• A monitor type resembles a class in a language like C+
+ or Java
– Contains declarations of shared data
– Its procedures encode operations that manipulate shared
data and implement process synchronization
• A condition is a situation of interest in a monitor
• A condition variable is associated with a condition
• A process executing a wait operation on condition variable
is blocked until some process performs a signal operation
• In a concurrent system, processes share data by
creating a monitor object
– We call it simply monitor
6.53
53
Example: Monitor Implementation of a
Binary Semaphore
6.54
54
Example: Producers-Consumers
Using Monitors
6.55
55
Monitors in Java
• A Java class becomes a monitor type when the
attribute synchronized is associated with one or more
methods in the class
• An object of such a class is a monitor
• Each monitor contains a single unnamed condition
variable
– Can lead to busy waits in an application that has many
conditions
6.56
56