-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock.py
More file actions
40 lines (35 loc) · 982 Bytes
/
Copy pathLock.py
File metadata and controls
40 lines (35 loc) · 982 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
# -*- coding: utf-8 -*-
from multiprocessing import Process, Lock
from multiprocessing import current_process
from multiprocessing import Value, Array
N = 8
def task(lock, common, tid):
a = 0
for i in range(100):
print(f'{tid}−{i}: Non−critical Section')
a += 1
print(f'{tid}−{i}: End of non−critical Section')
lock.acquire()
try:
print(f'{tid}−{i}: Critical section')
v = common.value + 1
print(f'{tid}−{i}: Inside critical section')
common.value = v
print(f'{tid}−{i}: End of critical section')
finally:
lock.release()
def main():
lp = []
common = Value('i', 0)
lock = Lock()
for tid in range(N):
lp.append(Process(target=task, args=(lock,common,tid)))
print (f"Valor inicial del contador {common.value}")
for p in lp:
p.start()
for p in lp:
p.join()
print (f"Valor final del contador {common.value}")
print ("fin")
if __name__ == "__main__":
main()