Академический Документы
Профессиональный Документы
Культура Документы
Spring 2013
Dan C. Marinescu
Office: HEC 304
Office hours: M-Wd 11:30 - 12:30 AM
Last time:
Today:
Next time
1/21/16
Thread coordination
Preemption ability to stop a running activity and start another one with a
higher priority.
Side effects of thread coordination
Deadlock
Priority inversion a lower priority activity is allowed to run before one with a higher priority
Lecture 18
Lecture 18
Dining philosophers
Critical section
Lecture 18
Applies only to two threads Ti and Tj with i,j ={0,1} which share
integer turn if turn=i then it is the turn of Ti to enter the critical section
boolean flag[2] if flag[i]= TRUE then Ti is ready to enter the critical section
sets turn=j
If both threads want to enter then turn will end up with a value of either i or j
and the corresponding thread will enter the critical section.
Ti enters the critical section only if either flag[j]= FALSE or turn=i
But this solution may not work as load and store instructions can be
interrupted on modern computer architectures
Lecture 18
Lecture 18
ACQUIRE
RELEASE
RSM (mem)
Lecture 18
Lecture 18
Lecture 18
10
Deadlocks
Happen quite often in real life and the proposed solutions are not
always logical: When two trains approach each other at a crossing,
both shall come to a full stop and neither shall start up again until
the other has gone. a pearl from Kansas legislation.
Deadlock jury.
Deadlock legislative body.
Lecture 18
11
Examples of deadlock
Lecture 18
12
Lecture 18
13
Thread deadlock
Deadlocks prevent sets of concurrent threads/processes from
completing their tasks.
How does a deadlock occur a set of blocked threads each
holding a resource and waiting to acquire a resource held by
another thread in the set.
Example
locks A and B, initialized to 1
P0
P1
wait (A);
wait (B);
wait(B)
wait(A)
Lecture 18
14
System model
Lecture 18
15
Lecture 18
16
Lecture 18
17
Semaphores
Lecture 18
18
P (s) (wait)
{
If s.C > 0 then s.C ;
else join s.L;
}
V (s) (signal)
{
If s.L is empty then s.C + +;
else release a process from s.L;
}
Lecture 18
19
Monitors
multiple threads may be allowed to enter the critical section guarded by the semaphore
may cause deadlocks
Threads may access the shared data directly without checking the semaphore.
Solution encapsulate shared data with access methods to operate on them.
Monitors an abstract data type that allows access to shared data with specific
methods that guarantee mutual exclusion
Lecture 18
20
Lecture 18
21
Examples: Event - child exit, signal - to parent. Control signal from keyboard.
Lecture 18
22
Lecture 18
23