Chapter

Deadlock
• 4 necessary conditions
–
–
–
–
Mutual exclusion
Hold and wait
No preemption
Circular wait
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-1
Figure 6.7 Deadlock example. Threads T1 and T2 hold
locks L1 and L2, respectively, and each thread attempts
to acquire the other lock, which cannot be granted.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-2
Preventing Deadlock
• Two major categories for dealing with deadlock
– Prevent deadlock from happening
– Detect deadlocks and then break the deadlock
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-3
Lock Hierarchies – preventing deadlock
• Each lock is assigned a position in an ordering.
Must acquire the locks in order
• Assumes a priori knowledge of all the locks
needed.
• If need a lower lock after acquiring a higher
lock, could release the higher locks, acquire the
lower locks and then reacquire the higher locks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-4
Monitors
• Abstract concept that could be implemented in
a language.
• Only allows one thread access to the data.
• Has one single entry point for several methods
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-5
Figure 6.8 Monitors provide an abstraction of synchronization
in which only one thread can access the monitor’s data at any
time. Other threads are blocked either waiting to enter the
monitor or waiting on events inside the monitor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-6
Figure 6.12 Multiple readers, single
writer support routines.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-7
Figure 6.13 Multiple readers, single-writer
support routines based on a single-condition
variable, but subject to spurious wake-ups.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-8
Thread Scheduling
• Two ways to have a thread
– Map multiple threads to a single OS aware thread
• Unbound threads
• Create and destroy overhead low
• If OS blocks this thread, all the mapped threads are
blocked also
– Map each thread to its own OS aware thread
• Bound threads
• Better performance expected
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-9
Code Spec 6.18 POSIX Thread routine
for setting thread scheduling attributes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-10
Figure 6.14 A 2D relaxation replaces—on
each iteration—all interior values by the
average of their four nearest neighbors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-11
Figure 6.15
False Sharing – when a processor has more
from the shared memory in its cache than
is “allocated” to that processor
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-12
Figure 6.16 2D Sucessive over-relaxation
program written using POSIX Threads.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-13
Figure 6.16 2D Sucessive over-relaxation
program written using POSIX Threads. (cont.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-14
Figure 6.16 2D Sucessive over-relaxation
program written using POSIX Threads. (cont.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-15
Figure 6.16 2D Sucessive over-relaxation
program written using POSIX Threads. (cont.)
0
1
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-16
Figure 6.17 It’s often profitable to do useful
work while waiting for some long-latency
operation to complete.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-17
Figure 6.18 A split-phase barrier allows a thread
to do useful work while waiting for the other
threads to arrive at the barrier.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-18
Figure 6.19 A 1D over-relaxation replaces—on each
iteration—all interior values by the average of their
two nearest neighbors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-19
Figure 6.20 Program for 1D successive
over-relaxation using a single-phase
barrier.
+1
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-20
Figure 6.21 Program for 1D successive overrelaxation using a split-phase barrier.
+1
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-21
Figure 6.21 Program for 1D successive overrelaxation using a split-phase barrier. (cont.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-22
Code Spec 6.19 Java’s Thread class
methods.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-23
Figure 6.27 Count 3s solution in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-24
Figure 6.27 Count 3s solution in Java.
(cont.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-25
Figure 6.27 Count 3s solution in Java.
(cont.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-26
Code Spec 6.20 Examples of Java’s
atomic objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-27
Figure 6.28 OpenMP
schedule (static)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-28
Code Spec 6.21 OpenMP parallel for
statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-29
Code Spec 6.22 OpenMP reduce
operation. The <op> choice comes from
the accompanying table.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-30
Code Spec 6.23 Atomic specification in
OpenMP.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-31