Concurrent Programming : a simple model to tackle the problem
This commit is contained in:
parent
a7c44bfbad
commit
fec95335a9
|
@ -5,7 +5,7 @@ import random
|
|||
import time
|
||||
|
||||
renew_length = 12
|
||||
local_workers_number = 10
|
||||
local_threads_number = 10
|
||||
number_of_cycles = 20
|
||||
renew = []
|
||||
done = []
|
||||
|
@ -13,12 +13,14 @@ space = []
|
|||
copy = []
|
||||
|
||||
def init():
|
||||
for i in range(0, renew_length - 1):
|
||||
renew.append(-1)
|
||||
for j in range(0, local_workers_number):
|
||||
for i in range(0, renew_length):
|
||||
renew.append(0)
|
||||
for j in range(0, local_threads_number):
|
||||
space.append(random.randint(10,99))
|
||||
copy.append(space[j])
|
||||
print(' ',space,' < initial global state')
|
||||
print(' ',space,' < initial global state',' '*27,end='[')
|
||||
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
|
||||
print(']')
|
||||
|
||||
def disp(coord, id, start, prev, next):
|
||||
print(' {} at [{}] {} > {} by thread n°{:>3} {!s:.4} {}'.format(
|
||||
|
@ -28,43 +30,37 @@ def disp(coord, id, start, prev, next):
|
|||
next,
|
||||
str(id),
|
||||
datetime.now().timestamp() - start,
|
||||
renew
|
||||
)
|
||||
'[', # renew
|
||||
),
|
||||
end =''
|
||||
)
|
||||
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
|
||||
print(']')
|
||||
|
||||
|
||||
def local_worker(coord, id):
|
||||
def local_thread(coord, id):
|
||||
start = datetime.now().timestamp()
|
||||
val = random.randint(1,1000)
|
||||
time.sleep(val / 1000)
|
||||
prev = space[coord]
|
||||
next = space[coord] = 10 + val % 89
|
||||
done.append(id)
|
||||
for i in renew:
|
||||
for i in range(0, renew_length):
|
||||
if renew[i] == id:
|
||||
renew[i] = -1
|
||||
renew[i] = 0
|
||||
break
|
||||
disp(coord, id, start, prev, next)
|
||||
|
||||
init()
|
||||
for k in range (1, number_of_cycles):
|
||||
n = 0
|
||||
while renew[n] == -1: # a diagonal of zeros is expected !
|
||||
renew[n] = 0
|
||||
if n < renew_length - 2:
|
||||
n += 1
|
||||
break
|
||||
# for n in range(0, renew_length - 1):
|
||||
# if renew[n] == -1:
|
||||
# renew[n] = 0
|
||||
# break
|
||||
t = Thread(target=local_worker, args=(random.randint(0, len(space) - 1), k))
|
||||
init() # Scheduler
|
||||
for id in range (1, number_of_cycles):
|
||||
if renew[id % renew_length] == 0:
|
||||
renew[id % renew_length] = id
|
||||
|
||||
t = Thread(target=local_thread, args=(random.randint(0, len(space) - 1), id))
|
||||
t.start()
|
||||
time.sleep(1)
|
||||
print(' ',copy,' < initial global state (to compare)')
|
||||
print(done)
|
||||
done.sort()
|
||||
print(done)
|
||||
print('history: ',done) # done.sort() # print(done)
|
||||
|
||||
|
||||
|
||||
|
@ -72,12 +68,12 @@ print(done)
|
|||
|
||||
"""
|
||||
Le **scheduler**, ou processus principal, effectue un calcul sur l'**état global**.
|
||||
Pour cela, il génère des threads de calcul locaux ou '**local_workers**'
|
||||
Pour cela, il génère des threads de calcul locaux ou '**local_threads**'
|
||||
auxquels il confie une partie de l'état global.
|
||||
Il est seul à avoir accès à l'état global et à la liste des local_workers.
|
||||
Il est seul à avoir accès à l'état global et à la liste des local_threads.
|
||||
Il n'a pas accès aux **règles de transition**.
|
||||
Chaque local_worker effectue un calcul local puis en soumet le résultat au scheduler.
|
||||
Le scheduler assigne les résultats des local_workers à l'état global.
|
||||
Chaque local_thread effectue un calcul local puis en soumet le résultat au scheduler.
|
||||
Le scheduler assigne les résultats des local_threads à l'état global.
|
||||
Il délègue à un **superviseur** des vérification périodiques de l'intégrité de l'état global et peut interrompre le processus en cas d'erreur.
|
||||
Il délègue à la **CLI** les fonctions de communications avec les modules périphériques.
|
||||
|
||||
|
|
Loading…
Reference in New Issue