{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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) A->mutex_lock(&_mutex) B->mutex_lock(&_mutex) B->mutex_lock(&_mutex) B’s lock is already locked so it waits A->mutex_lock(&_mutex)
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern