DEV Community

Cover image for Deadlock in Python
Hassan Tahir
Hassan Tahir

Posted on

Deadlock in Python

In the field of computer science, deadlock refers to a specific situation at a time planning, when no progress can be made and the system is locked in it current situation. In many cases, the condition is caused by a lack of, or mistreatment, communication between different locking devices (for thread synchronization purposes).
Deadlock is also defined by the conditions that the corresponding system must have at the same time for a deadlock. These issues were first raised by the media computer scientist Edward G. Coffman, Jr., and as a result became known as the Coffman conditions.

Let's take a quick look at a basic example of deadlock. Consider the same program in
which are two different processes (process A and process B), and two different processes
resources (R1 service and R2 service), as follows:
deadlock sample
There are no resources that can be assigned to all the different processes, and each process needs to be done access both resources to use its instructions. Take procedure A, for example. It already exists holding a R1 service, but it also requires R2 to continue its operation. However, R2 cannot be detected by process A, as it is detected by process B. Thus, process A cannot go on. It is the same with process B, which holds R2 and requires R1 to continue. R1 says,on the other hand, managed by process A.

Python Preceding in Deadlock Situation

we will use the preceding situation in the actual Python application.Specifically, we will have two locks (we will call them Lock A and lock B), and two are separate strands connecting the locks (string A and string B). In our program, we will establish a situation where string A gets the A lock and waits to get the B lock, which it has already been secured with string B, that is, waiting for lock A.

import threading
import time
 def thread_a():
     print('Thread A is starting...') 
     print('Thread A waiting to acquire lock A.')
     lock_a.acquire()
     print('Thread A has acquired lock A, performing some calculation...')

time.sleep(2)
     print('Thread A waiting to acquire lock B.')
     lock_b.acquire()
     print('Thread A has acquired lock B, performing some calculation...')
     time.sleep(2)
     print('Thread A releasing both locks.')
     lock_a.release()
     lock_b.release()
 def thread_b():
     print('Thread B is starting...')
     print('Thread B waiting to acquire lock B.')
     lock_b.acquire()
     print('Thread B has acquired lock B, performing some calculation...')
     time.sleep(5)
     print('Thread B waiting to acquire lock A.')
     lock_a.acquire()
     print('Thread B has acquired lock A, performing some calculation...')
     time.sleep(5)
     print('Thread B releasing both locks.')
 lock_b.release()
 lock_a.release()
 lock_a = threading.Lock()
 lock_b = threading.Lock()

 thread1 = threading.Thread(target=thread_a)
 thread2 = threading.Thread(target=thread_b)
 thread1.start()
 thread2.start()
 thread1.join()
 thread2.join()

 print('Finished.')
Enter fullscreen mode Exit fullscreen mode

In this code, the functions of thread_a () and thread_b () specify our thread A and thread B, respectively. In our main program, we also have two addictive items.Lock: lock A and lock B.

Note that we use the time.sleep () function to mimic the action of others calculated statistics.

deadlock lizard
First, we start both cable A and cable B at about the same time, inside the main system. With the suspension of the string command in mind, we can see that in at this point, both threads will be started; string A will try to get the lock A, and it will successfully do so, as lock A is currently available. The same goes for series B then lock B. The two strands will then proceed to perform the calculations themselves.

Let's consider the current state of our system: Lock A is obtained by string A, too Lock B is obtained by string B. After their various calculation procedures complete, series A will then try to find the B key, and string B will try to find the A key.

We can easily see that this is the beginning of our deadlock situation: as lock B already exists held by string B, and cannot be obtained by rope A, string B, for the same reason,
can't find key A.
Both wires will now wait permanently, to get their second lock.

However, the only way the lock can be released is for the cord to continue its operation instructions and release all the locks it has at the end. So our plan will be is stuck in its current operation, and no further progress will be made.

to be continued.. .

  1. Algorithms Detailed Python Algorithms
  2. Github Thehassantahir
  3. Share + Comment + Like

Top comments (0)