Maximum Needs | Allocate | Need | Available | |
---|---|---|---|---|
T0 | 10 | 5 | 5 | 3 |
T1 | 4 | 2 | 2 | |
T2 | 9 | 2 | 7 |
/* thread_one runs in this function */
void * do_work_one (void *param)
{
pthread_mutex_lock (& first_mutex );
pthread_mutex_lock (& second_mutex );
/** * Do some work */
pthread_mutex_unlock (& second_mutex );
pthread_mutex_unlock (& first_mutex ); pthread_exit (0);
} /* thread_two runs in this function */
void * do_work_two (void *param)
{
pthread_mutex_lock (& second_mutex );
pthread_mutex_lock (& first_mutex );
/*** Do some work */
pthread_mutex_unlock (& first_mutex );
pthread_mutex_unlock (& second_mutex );
pthread_exit (0);
}
thread_one
attempts to acquire the mutex locks in the order (1) first_mutex , (2) second_mutex.thread_two
attempts to acquire the mutex locks in the order (1) second_mutex
, (2) first_mutex
.first_mutex
while thread_two
acquires second_mutex
.first_mutex
and second_mutex
before thread_two
attempts to acquire the locks.thread_one
acquires first_mutex
, followed by thread_two
acquiring second_mutex
.pthread_mutex_trylock()
, which fails, releases their respective locks, and repeats the same actions indefinitely./* thread_one runs in this function */
void * do_work_one (void *param)
{
int done = 0; while (!done) {
pthread_mutex_lock (& first_mutex );
if ( pthread_mutex_trylock (& second_mutex ))
{/*** Do some work */ pthread_mutex_unlock (& second_mutex );
pthread_mutex_unlock (& first_mutex );
done =_1;}
else
pthread_mutex_unlock (& first_mutex );}
pthread_exit (0);
}
/* thread_two runs in this function */
void * do_work_two (void *param)
{
int done = 0;
while (!done)
{
pthread_mutex_lock (& second_mutex );
if ( pthread_mutex_trylock (& first_mutex ))
{
/*** Do some work*/
pthread_mutex_unlock (& first_mutex );
pthread_mutex_unlock (& second_mutex );
done =_1;
}
else
pthread_mutex_unlock (& second_mutex );
}
pthread_exit (0);
}
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion
: only one process at a time can use a resourceHold and wait
: a process holding at least one resource is waiting to acquire additional resources held by other processesNo preemption
: a resource can be released only voluntarily by the process holding it, after that process has completed its taskCircular wait
: there exists a set \(\{P_0, P_1, \ldots, P_n\}\) of waiting processes such that \(P_0\) is waiting for a resource that is held by \(P_1\), \(P_1\) is waiting for a resource that is held by \(P_2\), \(\ldots\), \(P_{n-1}\) is waiting for a resource that is held by \(P_n\), and \(P_n\) is waiting for a resource that is held by \(P_0\).All four conditions must hold for a deadlock to occur
. The circular-wait condition implies the hold-and-wait condition, so the four conditions are not completely independentNeed = Max need – Allocation
; \({Available}_i\) = \({Available}_{(i-1)} + Allocate\).Maximum Needs | Allocate | Need | Available | |
---|---|---|---|---|
T0 | 10 | 5 | 5 | 3 |
T1 | 4 | 2 | 2 | |
T2 | 9 | 2 | 7 |
Maximum Needs | Allocate | Need | Available | |
---|---|---|---|---|
T0 | 10 | 5 | 5 | 2 |
T1 | 4 | 2 | 2 | |
T2 | 9 | 3 | 6 |
Total Resources = 15, Threads = 3
Maximum Needs | Allocate | Need | Available | |
---|---|---|---|---|
T0$ | 6 | 3 | ||
T1 | 7 | 5 | ||
T2 | 8 | 4 |
5 processes \(P_0\) through \(P_4\) ;
3 resource types: A (10 instances), B (5instances), and C (7 instances)
Snapshot at specific time is given in the table:
Allocated | Max Need | Available | Need | |
---|---|---|---|---|
A B C | A B C | A B C | A B C | |
T0 | 0 1 0 | 7 5 3 | 3 3 2 | |
T1 | 2 0 0 | 3 2 2 | ||
T2 | 3 0 2 | 9 0 2 | ||
T3 | 2 1 1 | 2 2 2 | ||
T4 | 0 0 2 | 4 3 3 |
Need | |
---|---|
A B C | |
T0 | 7 4 3 |
T1 | 1 2 2 |
T2 | 6 0 0 |
T3 | 0 1 1 |
T4 | 4 3 1 |
Allocated | Max Need | Available | Need | |
---|---|---|---|---|
A B C | A B C | A B C | A B C | |
T0 | 0 1 0 | 7 5 3 | 2 3 0 | 7 4 3 |
T1 | 3 0 2 | 3 2 2 | 0 2 0 | |
T2 | 3 0 2 | 9 0 2 | 6 0 0 | |
T3 | 2 1 1 | 2 2 2 | 0 1 1 | |
T4 | 0 0 2 | 4 3 3 | 4 3 1 |
Allocated | Max Need | Available | Need | |
---|---|---|---|---|
A B C | A B C | A B C | A B C | |
T0 | 0 1 0 | 7 5 3 | 2 3 0 | 7 4 3 |
T1 | 3 0 2 | 3 2 2 | 0 2 0 | |
T2 | 3 0 2 | 9 0 2 | 6 0 0 | |
T3 | 2 1 1 | 2 2 2 | 0 1 1 | |
T4 | 0 0 2 | 4 3 3 | 4 3 1 |
Allocated | Max Need | Available | Need | |
---|---|---|---|---|
A B C | A B C | A B C | A B C | |
T0 | 0 3 0 | 7 5 3 | 2 1 0 | 7 4 3 |
T1 | 3 0 2 | 3 2 2 | 0 2 0 | |
T2 | 3 0 2 | 9 0 2 | 6 0 0 | |
T3 | 2 1 1 | 2 2 2 | 0 1 1 | |
T4 | 0 0 2 | 4 3 3 | 4 3 1 |
Allocation | Request | Available | |
---|---|---|---|
A B C | A B C | A B C | |
T0 | 0 1 0 | 0 0 0 | 0 0 0 |
T1 | 2 0 0 | 2 0 2 | |
T2 | 3 0 3 | 0 0 0 | |
T3 | 2 1 1 | 1 0 0 | |
T4 | 0 0 2 | 0 0 2 |
Request | |
---|---|
A B C | |
T0 | 0 0 0 |
T1 | 2 0 2 |
T2 | 0 0 1 |
T3 | 1 0 0 |
T4 | 0 0 2 |
Adapted from “Operating System Concepts”, 10th Edition; Abraham Silberschatz, Peter B. Galvin,Greg Gagne; 2018