How to avoid deadlocks?

前端 未结 8 837
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 03:30

When using multiple threads, shared memory needs to be locked by critical sections. However, using critical sections causes potential deadlocks. How can they be avoided?

相关标签:
8条回答
  • 2021-02-01 03:59

    THE FOLLOWING ALGORITHM IS USED TO AVOID DEADLOCK:

    Banker’s Algorithm

    –Impose less stringent conditions than in deadlock prevention in an attempt to get better resource utilization

    –Safe state

    •Operating system can guarantee that all current processes can complete their work within a finite time

    –Unsafe state

    •Does not imply that the system is deadlocked, but that the OS cannot guarantee that all current processes can complete their work within a finite time

    –Requires that resources be allocated to processes only when the allocations result in safe states. –It has a number of weaknesses (such as requiring a fixed number of processes and resources) that prevent it from being implemented in real systems

    0 讨论(0)
  • 2021-02-01 04:00

    Among the various methods to enter critical sections -- semaphores and mutexs are the most popular.

    • A semaphore is a waiting mechanism and mutex is a locking mechanism, well the concept is confusing to the most, but in short, a thread activating a mutex can only deactivate it. with this in mind...

    • Dont allow any process to lock partial no of resources, if a process need 5 resources, wait until all the are available.

    • if u use semaphore here, u can unblock/un-wait the resource occupied by other thread. by this i mean pre-emption is another reason.

    These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.

    If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.

    0 讨论(0)
提交回复
热议问题