notes15 - Deadlock and starvation Deadlock It is a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Deadlock and starvation Deadlock It is a situation where one or more threads will block forever waiting for a resource (mutex or semaphore) that will never be available. You break a deadlock is by “bouncing” (kill and restart) the system. Best way is to prevent deadlocks from happening. Example of deadlock: Assume a bank where you have multiple accounts. Each account has its own mutex. class Account{ double _amount; mutex_t _mutex; public: static void transfer(Account *from, Account *to, double howmuch); }; void Account::transfer( Account *from,Account *to, double howmuch) { mutex_lock(&from->_mutex); mutex_lock(&to->_mutex); from->_amount-=howmuch; to->_amount+= howmuch; mutex_unlock(&from->_mutex); mutex_unlock(&to->_mutex); } How do we get a deadlock? Have 1 thread do a transder from account A to account B and another thread do a a transfer from account B to account A. T1 T2 Transfer(A,B,100) Transfer(B,A,20) B’s lock is already locked so it waits
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

notes15 - Deadlock and starvation Deadlock It is a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online