-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadlock.py
More file actions
executable file
·50 lines (46 loc) · 1.05 KB
/
Copy pathdeadlock.py
File metadata and controls
executable file
·50 lines (46 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python2.7
import threading
import time
import inspect
a = 5
b = 5
alock = threading.Lock()
block = threading.Lock()
def calc_add_five():
global a
global b
print inspect.stack()[0][3] + " acquires lock a"
alock.acquire()
time.sleep(1)
a = a + 5
print inspect.stack()[0][3] + " acquires lock b"
block.acquire()
time.sleep(1)
b = b + 5 + a
print inspect.stack()[0][3] + " releases lock b"
block.release()
print inspect.stack()[0][3] + " releases lock a"
alock.release()
def calc_add_ten():
global a
global b
print inspect.stack()[0][3] + " acquires lock b"
block.acquire()
time.sleep(1)
b = b + 10
print inspect.stack()[0][3] + " acquires lock a"
alock.acquire()
time.sleep(1)
a = a + 10 + b
print inspect.stack()[0][3] + " releases lock b"
block.release()
print inspect.stack()[0][3] + " releases lock a"
alock.release()
t1 = threading.Thread(target = calc_add_five)
t2 = threading.Thread(target = calc_add_ten)
t1.start()
t2.start()
t1.join()
t2.join()
print "a = " + str(a)
print "b = " + str(b)