Академический Документы
Профессиональный Документы
Культура Документы
Languages
Chapter 3 Objectives
Introduce correctness issues that arise as a result
of concurrency.
Discuss techniques for controlling concurrency to
prevent these issues from resulting in software
defects.
Outline
Correctness
Control mechanisms
Correctness
Concurrency introduces a family of correctness
problems that can arise due to interactions
between threads of execution that are:
Executing at the same time.
Interleaved in an unpredictable manner.
Correctness
Race conditions
Deadlock and livelock
Liveness, fairness, and starvation
Nondeterminism
Race conditions
Consider two sequences of operations that can
execute concurrently.
A1
A2
AX
A3
A4
B1
B2
BX
B3
B4
Race conditions
If the result of operations after the critical point in
the sequence (AX or BX) is dependent on which
executes first (AX then BX versus BX then AX), a
race is present in the code.
The result is dependent on which thread of execution
wins the race to that point.
Race conditions
Very difficult to debug in some cases, as races are
dependent on timing.
OS upgrade may change performance of underlying
system primitives and change relative ordering, and
races may be revealed by code breaking.
Deadlock
Deadlock results from a cyclical chain of
dependencies.
A depends on B, B depends on C, and C depends on A.
Deadlock
Deadlock is similar to a race, in that a program
may execute correctly sometimes, but under
certain circumstances the relative ordering of
threads of execution will cause the program to
hang.
Like races, this is due to unenforced (or improperly
enforced) dependencies within the program
between concurrent threads of execution.
10
Livelock
Similar to deadlock, but instead of freezing, a
program enters into an endless cycle of operations
that it cannot continue past.
Example: Acquire lock 1, attempt to acquire lock 2 and
fail, release lock 1, retry.
11
Liveness
If multiple threads of execution are started, we
expect that they will all eventually finish.
Often we will go further, and assume that during
any reasonable interval of time (such as 1
second), all concurrent threads will make some
progress.
When a system or program ensures that this
property holds, we say is ensures liveness.
12
Liveness
Often liveness is a property of a scheduler, either
within the OS, thread library, or user application.
Concurrent programmers that manage their own
threads must occasionally consider liveness of
their management algorithms.
When a system doesnt ensure liveness, then
starvation results.
13
Starvation
A thread of execution will starve if it ceases to
make progress in the presence of others.
Example: a program that is continuously preempted by
others and never allowed to execute.
Nondeterminism
Not always a problem some algorithms can
exploit nondeterminism to their advantage.
Example: concurrent queries to multiple search engines,
return whichever result returns first.
15
Outline
Correctness
Control mechanisms
16
Control
To prevent correctness problems, we must control
how concurrently executing programs interact.
Synchronization
Semaphores, locks, monitors
Mutual exclusion
Transactions
Speculative execution and conflict resolution
17
Synchronization
Synchronization allows concurrent threads of
execution to communicate and coordinate
information about their relative state.
This can be used to coordinate their execution and
prevent correctness problems.
18
Semaphores
Semaphores are one of the original
synchronization primitives due to Dijkstra.
Represented as:
A single integer.
A queue of blocked accessors.
19
Semaphores
Consider a semaphore s.
Initialized to 1 with an empty queue.
20
Semaphores
Example:
Before entering critical section, execute P(s).
If no other thread is in section, s will have value 1, so it will be
decremented and thread can enter section.
Otherwise, thread blocks.
21
Semaphores
We have described a binary semaphore so far.
Values can be 0 or 1.
Locks
A lock is a familiar synchronization construct.
Consider locks on doors that have only one key.
Someone obtains key, is able to unlock (acquire) the
lock.
Others must wait for the key to be released before they
can unlock the lock themselves.
23
Monitors
Introduced by P. Brinch-Hansen.
An early object-oriented approach to
synchronization.
Encapsulate synchronization structures along with the
data they are to protect and the routines that will
operate on this data.
24
Monitors
Example: Bank account object
Data: Balance of account.
Synchronization structure: Lock or semaphore.
Operations: Withdraw, deposit.
25
Monitors
Important feature of monitors beyond
encapsulation is introduction of condition
variables.
Condition variables allow threads to signal each
other.
Beyond simple lock semantics.
26
Monitors
Condition variable signaling
A thread may be executing within a monitor routine and
reach a state where it must block and yield the monitor
to allow another thread to access it so it can later pick
up where it left off and continue.
Two operations are provided for condition variables.
Wait()
Signal()
27
Monitors
A thread may wait on a condition variable.
Yield the synchronization structures for the monitor to
allow other threads in.
Block waiting for another thread to signal it.
28
29
Transactions
Locks, semaphores, and monitors provide a
pessimistic view of synchronization.
Assume a conflict will occur, so restrict access to critical
sections.
30
Transactions
Transactions are sequences of operations with a
start and an end.
When the sequence ends, the transaction
software layer checks to make sure no other
thread of execution conflicted with the transaction.
If no conflict occurred, the transaction is declared
successful and the results of the operation are
committed.
If not, a set of operations is executed to roll-back the
transaction.
Go back to a state where we can pretend the transaction
operations never occurred.
31
Transactions: ACID
Transactions come from the databases world.
They define requirements of a transaction as:
Atomicity: Transactions represent sequences of
operations intended to be atomic.
Consistency: A system that starts in a consistent state
will remain in a consistent state after the transaction.
Isolation: Intermediate states during the execution of a
transaction will not be visible to other threads.
Durability: Once a transaction completes, the initiator
of the transaction will guarantee that the result will
persist.
32
Transactions
The ACID requirements do not apply to all
transactional systems.
Example: software transactional memory
33