What's the difference between deadlock and livelock?

后端 未结 7 1699
渐次进展
渐次进展 2021-01-29 17:20

Can somebody please explain with examples (of code) what is the difference between deadlock and livelock?

7条回答
  •  走了就别回头了
    2021-01-29 17:38

    I just planned to share some knowledge.

    Deadlocks A set of threads/processes is deadlocked, if each thread/process in the set is waiting for an event that only another process in the set can cause.

    The important thing here is another process is also in the same set. that means another process also blocked and no one can proceed.

    Deadlocks occur when processes are granted exclusive access to resources.

    These four conditions should be satisfied to have a deadlock.

    1. Mutual exclusion condition (Each resource is assigned to 1 process)
    2. Hold and wait condition (Process holding resources and at the same time it can ask other resources).
    3. No preemption condition (Previously granted resources can not forcibly be taken away) #This condition depends on the application
    4. Circular wait condition (Must be a circular chain of 2 or more processes and each is waiting for resource held by the next member of the chain) # It will happen dynamically

    If we found these conditions then we can say there may be occurred a situation like a deadlock.

    LiveLock

    Each thread/process is repeating the same state again and again but doesn't progress further. Something similar to a deadlock since the process can not enter the critical section. However in a deadlock, processes are wait without doing anything but in livelock, the processes are trying to proceed but processes are repeated to the same state again and again.

    (In a deadlocked computation there is no possible execution sequence which succeeds. but In a livelocked computation, there are successful computations, but there are one or more execution sequences in which no process enters its critical section.)

    Difference from deadlock and livelock

    When deadlock happens, No execution will happen. but in livelock, some executions will happen but those executions are not enough to enter the critical section.

提交回复
热议问题