Update Concurrent Programming

This commit is contained in:
Jean Sirmai 2021-03-31 06:02:27 +00:00
parent fec95335a9
commit 53df6f852a
1 changed files with 15 additions and 12 deletions

View File

@ -9,22 +9,22 @@ local_threads_number = 10
number_of_cycles = 20 number_of_cycles = 20
renew = [] renew = []
done = [] done = []
space = [] arrows = []
copy = [] copy = []
def init(): def init():
for i in range(0, renew_length): for i in range(0, renew_length):
renew.append(0) renew.append(0)
for j in range(0, local_threads_number): for j in range(0, local_threads_number):
space.append(random.randint(10,99)) arrows.append(random.randint(10,99))
copy.append(space[j]) copy.append(arrows[j])
print(' ',space,' < initial global state',' '*27,end='[') print(' ',arrows,' < initial global state',' '*27,end='[')
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='') for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
print(']') print(']')
def disp(coord, id, start, prev, next): def disp(coord, id, start, prev, next):
print(' {} at [{}] {} > {} by thread n°{:>3} {!s:.4} {}'.format( print(' {} at [{}] {} > {} by thread n°{:>3} {!s:.4} {}'.format(
space, arrows,
coord, coord,
prev, prev,
next, next,
@ -42,8 +42,8 @@ def local_thread(coord, id):
start = datetime.now().timestamp() start = datetime.now().timestamp()
val = random.randint(1,1000) val = random.randint(1,1000)
time.sleep(val / 1000) time.sleep(val / 1000)
prev = space[coord] prev = arrows[coord]
next = space[coord] = 10 + val % 89 next = arrows[coord] = 10 + val % 89
done.append(id) done.append(id)
for i in range(0, renew_length): for i in range(0, renew_length):
if renew[i] == id: if renew[i] == id:
@ -52,11 +52,14 @@ def local_thread(coord, id):
disp(coord, id, start, prev, next) disp(coord, id, start, prev, next)
init() # Scheduler init() # Scheduler
for id in range (1, number_of_cycles): for id in range (0, number_of_cycles):
if renew[id % renew_length] == 0: for i in range (0, renew_length):
renew[id % renew_length] = id if renew[i] == 0:
renew[i] = id
t = Thread(target=local_thread, args=(random.randint(0, len(space) - 1), id)) break
# là où les local_thread qui se terminent ont écrit un zéro,
# les local_thread nouvellement créés devraient apparaitre !?
t = Thread(target=local_thread, args=(random.randint(0, len(arrows) - 1), id))
t.start() t.start()
time.sleep(1) time.sleep(1)
print(' ',copy,' < initial global state (to compare)') print(' ',copy,' < initial global state (to compare)')