Operating Systems and Distributed Systems Sincronizzazione di processi e thread (1) #define NITER 1000000 int count = 0; void *ThreadAdd(void *); /*thread’s function prototype */ int main(int argc, char * argv[]) { pthread_t tid1, tid2; pthread_create(&tid1, NULL, ThreadAdd, NULL); /* creates thread 1 */ pthread_create(&tid2, NULL, ThreadAdd, NULL); /* creates thread 2 */ pthread_join(tid1, NULL); /* wait for the thread 1 to finish */ pthread_join(tid2, NULL); /* wait for the thread 2 to finish */ if (count < 2 * NITER) printf("\n BOOM! count is [%d], should be %d\n", count, 2*NITER); else printf("\n OK! count is [%d]\n", count); pthread_exit(NULL); } void *ThreadAdd(void * a) /*thread’s function implementation */ { int i, tmp; for(i = 0; i < NITER; i++){ tmp = count; /* copy the global count locally */ tmp = tmp+1; /* increment the local copy */ count = tmp; /* store the local value into the global count */ } } Operating Systems and Distributed Systems 2 Problemi con la concorrenza host:pthreadsync Bebo$ ./p_thradd BOOM! il conteggio e! [1008904], invece di 2000000 host:pthreadsync Bebo$ ./p_thradd BOOM! il conteggio e! [1050931], invece di 2000000 host:pthreadsync Bebo$ ./p_thradd BOOM! il conteggio e! [1014420], invece di 2000000 host:pthreadsync Bebo$ ./p_thradd BOOM! il conteggio e! [1001119], invece di 2000000 host:pthreadsync Bebo$ ./p_thradd BOOM! il conteggio e! [1009707], invece di 2000000 Operating Systems and Distributed Systems 3 Compile & run p_thradd.c! Risultati aleatori? Problemi con la concorrenza Operating Systems and Distributed Systems Il problema della concorrenza 4 • Sincronizzazione: necessaria in condizioni di concorrenza. • Concorrenza: due o più processi/thread sono in esecuzione nello stesso intervallo di tempo e potenzialmente interagiscono l’uno con l’altro (e.g., condivisione di risorse). Race condition Critical section
12
Embed
Problemi con la concorrenza Sincronizzazione di processi e ...homes.di.unimi.it/~boccignone/GiuseppeBoccignone_webpage/Sistemi... · Sincronizzazione di processi e thread (1) #define
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Operating Systems and Distributed Systems
Sincronizzazione di processi e thread (1)
#define NITER 1000000
int count = 0;
void *ThreadAdd(void *); /*thread’s function prototype */
pthread_join(tid1, NULL);! /* wait for the thread 1 to finish */ !
pthread_join(tid2, NULL); /* wait for the thread 2 to finish */
!
if (count < 2 * NITER) printf("\n BOOM! count is [%d], should be %d\n", count, 2*NITER);
else printf("\n OK! count is [%d]\n", count);
!
pthread_exit(NULL);
}
void *ThreadAdd(void * a) /*thread’s function implementation */
{
int i, tmp;
for(i = 0; i < NITER; i++){
tmp = count; /* copy the global count locally */
tmp = tmp+1; /* increment the local copy */
count = tmp; /* store the local value into the global count */
}
}
Operating Systems and Distributed Systems 2
Problemi con la concorrenza
host:pthreadsync Bebo$ ./p_thradd
BOOM! il conteggio e! [1008904], invece di 2000000
host:pthreadsync Bebo$ ./p_thradd
BOOM! il conteggio e! [1050931], invece di 2000000
host:pthreadsync Bebo$ ./p_thradd
BOOM! il conteggio e! [1014420], invece di 2000000
host:pthreadsync Bebo$ ./p_thradd
BOOM! il conteggio e! [1001119], invece di 2000000
host:pthreadsync Bebo$ ./p_thradd
BOOM! il conteggio e! [1009707], invece di 2000000
Operating Systems and Distributed Systems 3
Compile & run p_thradd.c!
Risultati
aleatori?
Problemi con la concorrenza
Operating Systems and Distributed Systems
Il problema della concorrenza
4
• Sincronizzazione: necessaria in condizioni di concorrenza.
• Concorrenza: due o più processi/thread sono in esecuzione nello stesso intervallo di tempo e potenzialmente interagiscono l’uno con l’altro (e.g., condivisione di risorse).
Race condition
Criticalsection
Operating Systems and Distributed Systems 5
• Race condition: La manipolazione simultanea della stessa risorsa da parte di due o più P/T causa risultati inconsistenti.
• Sezione critica (Critical section): Segmento di codice che coordina l’accesso a una risorsa condivisa.
• Mutua esclusione (Mutual exclusion): proprietà del software che assicura accesso esclusivo a una risorsa condivisa.
• Deadlock: condizione speciale di stallo creata da due o più P/T e due o più locks su risorse che bloccano il lavoro.
Concetti importanti
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 6
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 7
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 8
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 9
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 10
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Thread 1 Thread 2 count
tmp = count; --- 0
tmp = tmp +1; --- 0
--- tmp = count;!!// 0 0
--- tmp = tmp + 1; 0
count = tmp;!!// 1 --- 1
--- count = tmp;!!// 1 1
Operating Systems and Distributed Systems 11
tmp = count;
tmp = tmp+1;
count = tmp;
Thread 1 Thread 2
tmp = count;
tmp = tmp+1;
count = tmp;
Race condition: esempio con pthread
Operating Systems and Distributed Systems
Meccanismi di sincronizzazione
Operating Systems and Distributed Systems13
Problema: race condition
• I thread P1 e P2 scrivono nella memoria condivisa
• Problema: non è possibile garantire che la read seguita da una write sia atomica
– L’ordine di esecuzione ha importanza!
• Necessario eliminare le race condition…
R1 <= x
R1 = R1+1
R1 => x
R3 <= x
R3 = R3+1
R3 => x
P1 P2x=3
x=5R1 <= x
R1 = R1+1
R1 => x
R3 <= x
R3 = R3+1
R3 => x
x=6!
LOAD
LOAD
STORE
STORE
STORE
LOAD
LOAD
STORE
Operating Systems and Distributed Systems
Il problema del latte
Arriva a casa
Frigorifero: niente latte
Va a comprare in latteria
arriva in latteria
Compra latte
Arriva a casa: mette in frigo
Arriva a casa
Frigorifero: niente latte
Va a comprare in latteria
Compra latte
Arriva a casa: mette in frigo
Primo studente Secondo studenteOra
Operating Systems and Distributed Systems
• Si assume di avere delle load/store atomiche
• Lascia un biglietto (lock)
• Togli il biglietto (unlock)
• Non comprare latte se c’è un biglietto (wait)
Il problema del latte
Operating Systems and Distributed Systems
Il problema del latte: soluzione 1
troppo latte!Compile & run p_milknota.c!
Operating Systems and Distributed Systems
Il problema del latte: soluzione 2
niente latte! (starvation)
usiamo bigliettini etichettati
Compile & run p_milknota2.c!
Operating Systems and Distributed Systems
Il problema del latte: soluzione 3aspetta il biglietto giusto
Operating Systems and Distributed Systems
Il problema del latte: soluzione 3aspetta il biglietto giusto
prima A e poi B
Operating Systems and Distributed Systems
Il problema del latte: soluzione 3aspetta il biglietto giusto
prima B e poi A
Operating Systems and Distributed Systems
Il problema del latte: soluzione 3aspetta il biglietto giusto
interleaving: A aspetta e poi compra
Operating Systems and Distributed Systems
Il problema del latte: soluzione 3aspetta il biglietto giusto
interleaving: A aspetta, B compra
Compile & run p_milknota3.c!
• Si definiscono delle regioni critiche (RC) a cui si applica la mutual exclusion per governare la corsa