-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemaphore.py
More file actions
executable file
·54 lines (36 loc) · 990 Bytes
/
Copy pathsemaphore.py
File metadata and controls
executable file
·54 lines (36 loc) · 990 Bytes
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
51
52
53
54
#!/usr/bin/python3
import random
import time
import sys
import threading
if len(sys.argv) == 2:
n_threads = int(sys.argv[1])
else:
n_threads = 10
sem = threading.Semaphore()
values = 5
def calc_random(v):
a = []
b = []
c = []
i=0
while(i<=v):
a.append(random.randint(0,9))
b.append(random.randint(0,9))
c.append(a[i] + b[i])
time.sleep(0.5)
print( threading.current_thread().getName() + ": " + str(a[i]) + " + " + str(b[i]) + " = " + str(c[i]) + "\n", file=sys.stderr)
i+=1
sem.acquire()
print("Hello my name is: " + threading.current_thread().getName())
i = 0
while(i<=v):
print(threading.current_thread().getName() + ":c[" + str(i) + "]= " + str(c[i]))
i+=1
sem.release()
threads = []
for n in list(range(0, n_threads, 1)):
thread = threading.Thread(target=calc_random, args=(values,))
threads.append(thread)
thread.start()
#print(a)