Title: Concurrency: Deadlock and Starvation
1Concurrency Deadlock and Starvation
2Revision
- Describe three necessary conditions for deadlock
- Which condition is the result of the three
necessary conditions - Deadlock avoidance makes sure that at least one
of the conditions does not exist in the system
(TRUE/FALSE) - An unsafe state is a deadlocked state (T/F)
3Deadlock Avoidance
- Maximum resource requirement must be stated in
advance - Processes under consideration must be
independent no synchronization requirements - There must be a fixed number of resources to
allocate - No process may exit while holding resources
4Deadlock Detection
- OS can perform deadlock detection to detect and
break deadlocks - Detection can be carried out on each resource
request - Allocation matrix, Available vector and Request
matrix are defined - Mark processes that are not deadlocked
- Begin by marking zero rows in Allocation matrix
5Deadlock Detection
- Initialize W A
- Find an i such that process i is currently
unmarked - Determine if ith row of Q is less than or equal
to W (for all elements), if no, EXIT - If yes, mark process i and set W WA for all
elements in this row and go back to find next
unmarked process - At the end, any unmarked process is deadlocked
6Deadlock Detection
P4 is marked first P3 is taken new W is
00011 P1 and P2 fail the test QltW so they are
deadlocked
7Strategies once Deadlock Detected
- Abort all deadlocked processes
- Back up each deadlocked process to some
previously defined checkpoint, and restart all
process - original deadlock may occur
- Successively abort deadlocked processes until
deadlock no longer exists - Successively preempt resources until deadlock no
longer exists
8Selection Criteria for Deadlocked Processes(34)
- Least amount of processor time consumed so far
- Least number of lines of output produced so far
- Most estimated time remaining
- Least total resources allocated so far
- Lowest priority
9Dining Philosophers
- Five philosophers are in deep thought sitting
around a dining table - When they get hungry, they try to eat the
spaghetti - There is only one fork to the left of each
philosopher - Each philosopher must acquire two forks to eat
- One philosopher can begin eating if the neighbour
to the right has put down the fork - No deadlock, no snatching, no starvation
10Dining Philosophers Problem
11Solution With Semaphores
- semaphore fork5 1
- void philosopher(int i)
-
- while (true)
-
- think()
- wait(forki)
- wait(fork(i1) mod 5)
- eat()
- signal(fork(i1) mod 5)
- signal(forki)
-
-
- All philosophers may starve to death!! Why??
12UNIX Concurrency Mechanisms
- Pipes
- Messages
- Shared memory
- Semaphores
- Signals
13Pipes
- Based on producer-consumer model
- A pipe is a FIFO queue written by one process and
read by another - Writing process is blocked if no room
- Mutual exclusion is enforced by UNIX
- Named pipes can be shared by unrelated processes
as well
14Messages
- Each process has a mailbox, an associated
message queue - System calls are provided for message passing
- Process sending message to a full queue is
suspended
15Shared Memory and Semaphores
- Common block of virtual memory shared by multiple
processes - Fastest form of IPC
- UNIX kernel handles the semaphore operations
atomically - A semaphore contains
- Current value
- PID of last process that accessed it
- Number of processes waiting
- System calls are provided to handle semaphores
16Signals
- A signal informs a process about an event
- Signals do not have priorities
- Some signals in UNIX
- SIGFPT Floating point exception
- SIGALARM Wake up after a time period
- SIGBUS Bus error
17Solaris Thread Synchronization Primitives
- Mutual exclusion (mutex) locks
- Semaphores
- Multiple readers, single writer locks
- Condition variables
- Implemented in KLT and ULT
- Once created, either enter or release
- Kernel does not enforce mutual exclusion and
unlocking on abort of threads
18Solaris Mutex Locks and Semaphores
- If a thread has locked a mutex, only this thread
will unlock it - If another thread approaches the mutex, it will
be either blocked or wait in a spin wait loop - Use mutex_tryenter() to do busy waiting
- Solaris provides sema_P(), sema_v() and
sema_tryp() primitives for semaphores
19(No Transcript)
20Readers/Writer and Condition Variables
- Readers/writer lock allows read-only access for
an object protected by this lock - If a thread is writing, all others must wait
- Condition variables are used with mutex locks
- Wait until a condition is true
21(No Transcript)
22Windows 2000 Concurrency Mechanisms
- Process
- Thread
- File
- Console input
- File change notification
- Mutex
- Semaphore
- Event
- Waitable timer
23W2K Summary
- Objects 5 through 9 are designed for supporting
synchronization - Each of these objects can be in a signaled or
unsignaled state - A thread issues wait request to W2K, using the
handle of the object - When an object enters signaled state, threads
waiting on it are released
24W2K Example
- A thread requests wait on a file that is
currently open - The thread is blocked
- The file is set to signaled state when the I/O
operation completes - The waiting thread is released