Synchronization & Implementation Issues
Synchronization & Implementation Issues
Implementation Issues
Synchronization Problems
Performance
Interaction with thread priorities
Starvation
Deadlock
Synchronization bugs
Heizenbugs
Service denial bugs
Performance
Blocking synchronization
Context switch if mutex is not available
But makes CPU available to others
Spinlock synchronization
Wastes CPU resources
But if mutex is available, or will be available soon, then we avoid the
context switch overhead
Hybrid Synchronization (Solaris)
Priority = 20
Priority = 10
Priority = 5
Priority Inversion
Happens a lot
vxWorks problems in Pathfinder
Fundamental problem:
Low-priority thread holds lock, enters C.S.
Higher-priority thread blocks waiting for lock
A medium-priority thread comes along and
KA-BOOOOOM
Solution: Priority Inheritance
Priority = 20
Priority = 10
Priority = 5
Priority = 20 Priority = 5
The Starvation Problem
Thread 1 Thread 2
P(s1) P(s2)
… …
P(s2) P(s1)
… …
V(s1) V(s2)
V(s2) V(s1)
Problem Definition
lock(f2); lock(f1);
… …
lock(f1); lock(f2);
Deadlock is Bad
…
lock(f2); <---- lock(f1);
… …
lock(f1); lock(f2);
Deadlock Prevention
lock(f2); lock(f1);
… …
lock(f1); lock(f2);
… …
Build a graph
Processes are vertices
An edge from process I to j if I wants a resource held by j.
If the graph has a cycle, there is deadlock
For Entertainment
P(s) P(s)
… …
V(s) V(s)
…
V(s)
Example
P(s); P(s);
…return; V(s);
… char * p = 0; * p = 0;
… while(1) … ;
V(s);