Systèmes
d'exploitation
Synchronisation de
exécution
Lesslie Lamport
• Américain
• MIT
• LaTeX
• L’algorithme Lamport
bakery
• Tolérance aux fautes
Byzantines
– Utilise sur blockchain
• L’algorithme Paxos
• Signature de Lamport
2
Contenu
• course
• espace critique
• opérations atomique
• busy waiting
• semaphore
• mutex
3
Bibliographie pour aujourd'hui
• Modern Operating Systems
– Chapitre 4
• 4.4
• 4.5
• 4.6
• Operating Systems Concepts
– Chapitre 9
• 9.1 – 9.4
• 9.6
• 9.7
4
COURSE
5
Course
Processus 1
// shared variable a
int a = 0;
if (a == 0)
{
a++;
}
printf ("%dn", a);
// shared variable a
int a = 0;
if (a == 0)
{
a++;
}
printf ("%dn", a);
6
Processus 2
Course – variante séquentiel
Processus 1
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
7
Processus 2
1 1
Course – variante intercalé
Processus 1
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
8
Processus 2
2 2
Course – désiré
Processus 1
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
// shared variable a
int a = 0;
if (a == 0) {
a++;
}
printf ("%dn", a);
9
Processus 2
espace critique
1 1
Espace critique
• une partie du code qui doit être exécuté seul
– l'accès aux variables (ressources) qui doit être
exécuté seul
10
if (a == 0) {
a++;
}
if (a == 1) {
a--;
}
Le raison est l’accès de variable a
Operation atomique
• Un opération qui est exécute sans interruption
• Interruptions possible
– interruption matériel
– signaux
– changement de contexte
• Les instructions de CPU sont atomique
11
Exemple
int main ()
{
int a = 0;
a++;
}
12
Plus des exemples sur https://godbolt.org/
Exemple – x86-64
int main ()
{
int a = 0;
a++;
}
; rbp-4 address of
variable a
mov DWORD PTR [rbp-4], 0
add DWORD PTR [rbp-4], 1
13
a++ est atomique
Exemple – ARM
int main ()
{
int a = 0;
a++;
}
; fp-8 address of
variable a
mov r3, #0
str r3, [fp, #-8]
ldr r3, [fp, #-8]
add r3, r3, #1
str r3, [fp, #-8]
14
a++ n’est pas atomique
Instruction pour synchronisation
if (test == 0)
{
test = 1;
// do some work
test = 0;
}
cmp DWORD PTR [rbp-4], 0
jne .L2
mov DWORD PTR [rbp-4], 1
; do some work
mov DWORD PTR [rbp-4], 0
.L2:
15
; rbp-4 address of variable test
N’est pas atomique
Test and set
• Une instruction qui
– Fait un comparaison
– Fait un attribution en dépendent de la
comparaison
• TSL
16
tsl r, value
tsl (r, value)
if (r == 0)
r = value;
Instruction pour synchronisation
if (tsl(test, 1))
{
// do some work
test = 0;
}
mov eax, 0
lock cmpxchg DWORD PTR
[rbp-4], 1
jne .L2
; do some work
mov DWORD PTR [rbp-4], 0
.L2:
17
; rbp-4 address of variable test
est atomique
Fair le code executer atomique
• Spinlock
• Semaphore
• Mutex
18
SPINLOCK
19
Spinlock
while (test != 0)
continue;
test = 1;
// do some work with
test = 0;
20
Spinlock
Atomique?
Spinlock x86
while (test != 0)
continue;
test = 1;
// do some work with
test = 0;
.L2:
cmp DWORD PTR [rbp-4], 0
jne .L2
mov DWORD PTR [rbp-4], 1
; do some work
mov DWORD PTR [rbp-4], 0
21
N’est pas atomique
; rbp-4 address of variable test
Spinlock
while (tsl(test, 1))
continue;
// do some work with
test = 0;
22
Spinlock
Spinlock x86
while (tsl(test, 1))
continue;
// do some work with
test = 0;
mov eax, 0
.L2:
lock cmpxchg DWORD PTR
[rbp-4], 1
jne .L2
; do some work
mov DWORD PTR [rbp-4], 0
23
Est atomique
; rbp-4 address of variable test
lock et unlock
• lock
– prend le spinlock
• unlock
– libère le spinlock
24
lock et unlock
void lock (s)
{
while (tsl(s, 1));
}
void unlock (s)
{
s = 0;
}
25
// s - spinlock
Exemple
Processus 1
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
26
Processus 2
1 1
Exemple
Processus 1
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
27
Processus 2
1 1
Deadlock
Que se passe-t-il si nous oublions d'utiliser le
unlock?
Que se passe-t-il si nous utilisons plusieurs de fois
lock sans unlock?
Que se passe-t-il si le program s’arrêt avant unlock?
28
Oublier unlock
Processus 1
// shared variables a and s
int a = 0;
int s;
lock (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
printf ("%dn", a);
29
Processus 2
... 1
Plusieurs de lock
Processus 1
// shared variables a and s
int a = 0;
int s;
lock (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
lock (s);
lock (s);
...waits a lot
30
Processus 2
... ...
Arrête avent unlock
Processus 1
// shared variables a and s
int a = 0;
int s;
lock (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
lock (s);
if (a == 0)
{
a++;
}
31
Processus 2
... erreur
SÉMAPHORE
32
Spinlock
while (tsl(test, 1))
continue;
// do some work with
test = 0;
33
Spinlock
Busy Waiting
Busy Waiting
• Essayer en permanence de tester la valeur de
la variable
• Le CPU fait des instructions inutiles
• Le processus va consume toute le quantum de
temps pur tester la valeur
34
Les états de processus
35
Spinlock
Les états de processus
36
Attende la resource
Sémaphore
• Object du Système d’Exploitation
• Partage entre plusieurs processus
• Fonctions exécuté atomique
– P (attendre) / wait / down
– V (signaler) / signal / up
• Value initiale
• Queue de processus qui attende
37
Fonctions de sémaphore
void wait (s)
{
while (s <= 0);
s = s - 1;
}
void signal (s)
{
s = s + 1;
}
38
// s - semaphore
Busy waiting
block et wakeup
• block
– arrête le processus curent
• état WAITING
• wakeup
– démarre touts les processus qui sont bloque sure
le sémaphore
• état READY
39
Les états de processus
40
block
wakeup
Fonction de sémaphore
void wait (s)
{
while (s <= 0)
block (s);
s = s - 1;
}
void signal (s)
{
s = s + 1;
wakeup (s);
}
41
// s - semaphore
Types des sémaphores
• binaire
– la valeur peut être seulement 0 ou 1
• comptage
– La valeur est une valeur entier
42
Sémaphore binaire
void wait (s)
{
while (s == 0)
block (s);
s = 0;
}
void signal (s)
{
s = 1;
wakeup (s);
}
43
// s - semaphore
Exemple
Processus 1
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
signal (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
signal (s);
printf ("%dn", a);
44
Processus 2
1 1
Exemple
Processus 1
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
signal (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
signal (s);
printf ("%dn", a);
45
Processus 2
1 1
Deadlock
Que se passe-t-il si nous oublions d'utiliser le
signal?
Que se passe-t-il si nous utilisons plusieurs de fois
wait sans signal?
Que se passe-t-il si le program s’arrêt avant signal?
46
Oublier signal
Processus 1
// shared variables a and s
int a = 0;
int s;
wait (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
printf ("%dn", a);
47
Processus 2
... 1
Plusieurs de wait
Processus 1
// shared variables a and s
int a = 0;
int s;
wait (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
wait (s);
wait (s);
...waits a lot
48
Processus 2
... ...
Arrête avent signal
Processus 1
// shared variables a and s
int a = 0;
int s;
wait (s);
... waits a lot
// shared variables a and s
int a = 0;
int s;
wait (s);
if (a == 0)
{
a++;
}
49
Processus 2
... erreur
MUTEX
50
Mutex
• Mutual Exclusion
• Similaire avec le sémaphore binaire
• Utilisez pour threads (même processus)
51
lock et unlock
• lock
– prend le mutex et mémorise le id de thread qui a
le pris
• la fonction lock est réentrante dans le même thread
• unlock
– libéré le mutex
• au fin du program (thread) le mutex est libéré
52
Deadlock
Que se passe-t-il si nous oublions d'utiliser le
unlock?
Que se passe-t-il si nous utilisons plusieurs de fois
wait sans lock?
Que se passe-t-il si le program s’arrêt avant unlock?
53
Oublier unlock
Processus 1
// shared variables a and s
int a = 0;
mutex s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
mutex s;
lock (s);
if (a == 0)
{
a++;
}
printf ("%dn", a);
54
Processus 2
1 1
Plusieurs de lock
Processus 1
// shared variables a and s
int a = 0;
mutex s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
mutex s;
lock (s);
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
55
Processus 2
1 1
Arrête avent unlock
Processus 1
// shared variables a and s
int a = 0;
mutex s;
lock (s);
if (a == 0)
{
a++;
}
unlock (s);
printf ("%dn", a);
// shared variables a and s
int a = 0;
mutex s;
lock (s);
if (a == 0)
{
a++;
}
56
Processus 2
1 erreur
Synchronization en Java
class Run
{
private Object a;
public synchronized void runAlone ()
{
// run some work
}
public void run ()
{
synchronized (a)
{
// run some work using the variable a
}
}
}
57
Mot clés
• course
• espace critique
• opération atomique
• busy waiting
• spinlock
• lock
• unlock
• sémaphore
• sémaphore binaire
• sémaphore comptage
• deadlock
• wait
• signal
• mutex
58
Questions
59

SdE 8 - Synchronisation de execution

  • 1.
  • 2.
    Lesslie Lamport • Américain •MIT • LaTeX • L’algorithme Lamport bakery • Tolérance aux fautes Byzantines – Utilise sur blockchain • L’algorithme Paxos • Signature de Lamport 2
  • 3.
    Contenu • course • espacecritique • opérations atomique • busy waiting • semaphore • mutex 3
  • 4.
    Bibliographie pour aujourd'hui •Modern Operating Systems – Chapitre 4 • 4.4 • 4.5 • 4.6 • Operating Systems Concepts – Chapitre 9 • 9.1 – 9.4 • 9.6 • 9.7 4
  • 5.
  • 6.
    Course Processus 1 // sharedvariable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); 6 Processus 2
  • 7.
    Course – varianteséquentiel Processus 1 // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); 7 Processus 2 1 1
  • 8.
    Course – varianteintercalé Processus 1 // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); 8 Processus 2 2 2
  • 9.
    Course – désiré Processus1 // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); // shared variable a int a = 0; if (a == 0) { a++; } printf ("%dn", a); 9 Processus 2 espace critique 1 1
  • 10.
    Espace critique • unepartie du code qui doit être exécuté seul – l'accès aux variables (ressources) qui doit être exécuté seul 10 if (a == 0) { a++; } if (a == 1) { a--; } Le raison est l’accès de variable a
  • 11.
    Operation atomique • Unopération qui est exécute sans interruption • Interruptions possible – interruption matériel – signaux – changement de contexte • Les instructions de CPU sont atomique 11
  • 12.
    Exemple int main () { inta = 0; a++; } 12 Plus des exemples sur https://godbolt.org/
  • 13.
    Exemple – x86-64 intmain () { int a = 0; a++; } ; rbp-4 address of variable a mov DWORD PTR [rbp-4], 0 add DWORD PTR [rbp-4], 1 13 a++ est atomique
  • 14.
    Exemple – ARM intmain () { int a = 0; a++; } ; fp-8 address of variable a mov r3, #0 str r3, [fp, #-8] ldr r3, [fp, #-8] add r3, r3, #1 str r3, [fp, #-8] 14 a++ n’est pas atomique
  • 15.
    Instruction pour synchronisation if(test == 0) { test = 1; // do some work test = 0; } cmp DWORD PTR [rbp-4], 0 jne .L2 mov DWORD PTR [rbp-4], 1 ; do some work mov DWORD PTR [rbp-4], 0 .L2: 15 ; rbp-4 address of variable test N’est pas atomique
  • 16.
    Test and set •Une instruction qui – Fait un comparaison – Fait un attribution en dépendent de la comparaison • TSL 16 tsl r, value tsl (r, value) if (r == 0) r = value;
  • 17.
    Instruction pour synchronisation if(tsl(test, 1)) { // do some work test = 0; } mov eax, 0 lock cmpxchg DWORD PTR [rbp-4], 1 jne .L2 ; do some work mov DWORD PTR [rbp-4], 0 .L2: 17 ; rbp-4 address of variable test est atomique
  • 18.
    Fair le codeexecuter atomique • Spinlock • Semaphore • Mutex 18
  • 19.
  • 20.
    Spinlock while (test !=0) continue; test = 1; // do some work with test = 0; 20 Spinlock Atomique?
  • 21.
    Spinlock x86 while (test!= 0) continue; test = 1; // do some work with test = 0; .L2: cmp DWORD PTR [rbp-4], 0 jne .L2 mov DWORD PTR [rbp-4], 1 ; do some work mov DWORD PTR [rbp-4], 0 21 N’est pas atomique ; rbp-4 address of variable test
  • 22.
    Spinlock while (tsl(test, 1)) continue; //do some work with test = 0; 22 Spinlock
  • 23.
    Spinlock x86 while (tsl(test,1)) continue; // do some work with test = 0; mov eax, 0 .L2: lock cmpxchg DWORD PTR [rbp-4], 1 jne .L2 ; do some work mov DWORD PTR [rbp-4], 0 23 Est atomique ; rbp-4 address of variable test
  • 24.
    lock et unlock •lock – prend le spinlock • unlock – libère le spinlock 24
  • 25.
    lock et unlock voidlock (s) { while (tsl(s, 1)); } void unlock (s) { s = 0; } 25 // s - spinlock
  • 26.
    Exemple Processus 1 // sharedvariables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); // shared variables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); 26 Processus 2 1 1
  • 27.
    Exemple Processus 1 // sharedvariables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); // shared variables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); 27 Processus 2 1 1
  • 28.
    Deadlock Que se passe-t-ilsi nous oublions d'utiliser le unlock? Que se passe-t-il si nous utilisons plusieurs de fois lock sans unlock? Que se passe-t-il si le program s’arrêt avant unlock? 28
  • 29.
    Oublier unlock Processus 1 //shared variables a and s int a = 0; int s; lock (s); ... waits a lot // shared variables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } printf ("%dn", a); 29 Processus 2 ... 1
  • 30.
    Plusieurs de lock Processus1 // shared variables a and s int a = 0; int s; lock (s); ... waits a lot // shared variables a and s int a = 0; int s; lock (s); lock (s); ...waits a lot 30 Processus 2 ... ...
  • 31.
    Arrête avent unlock Processus1 // shared variables a and s int a = 0; int s; lock (s); ... waits a lot // shared variables a and s int a = 0; int s; lock (s); if (a == 0) { a++; } 31 Processus 2 ... erreur
  • 32.
  • 33.
    Spinlock while (tsl(test, 1)) continue; //do some work with test = 0; 33 Spinlock Busy Waiting
  • 34.
    Busy Waiting • Essayeren permanence de tester la valeur de la variable • Le CPU fait des instructions inutiles • Le processus va consume toute le quantum de temps pur tester la valeur 34
  • 35.
    Les états deprocessus 35 Spinlock
  • 36.
    Les états deprocessus 36 Attende la resource
  • 37.
    Sémaphore • Object duSystème d’Exploitation • Partage entre plusieurs processus • Fonctions exécuté atomique – P (attendre) / wait / down – V (signaler) / signal / up • Value initiale • Queue de processus qui attende 37
  • 38.
    Fonctions de sémaphore voidwait (s) { while (s <= 0); s = s - 1; } void signal (s) { s = s + 1; } 38 // s - semaphore Busy waiting
  • 39.
    block et wakeup •block – arrête le processus curent • état WAITING • wakeup – démarre touts les processus qui sont bloque sure le sémaphore • état READY 39
  • 40.
    Les états deprocessus 40 block wakeup
  • 41.
    Fonction de sémaphore voidwait (s) { while (s <= 0) block (s); s = s - 1; } void signal (s) { s = s + 1; wakeup (s); } 41 // s - semaphore
  • 42.
    Types des sémaphores •binaire – la valeur peut être seulement 0 ou 1 • comptage – La valeur est une valeur entier 42
  • 43.
    Sémaphore binaire void wait(s) { while (s == 0) block (s); s = 0; } void signal (s) { s = 1; wakeup (s); } 43 // s - semaphore
  • 44.
    Exemple Processus 1 // sharedvariables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } signal (s); printf ("%dn", a); // shared variables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } signal (s); printf ("%dn", a); 44 Processus 2 1 1
  • 45.
    Exemple Processus 1 // sharedvariables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } signal (s); printf ("%dn", a); // shared variables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } signal (s); printf ("%dn", a); 45 Processus 2 1 1
  • 46.
    Deadlock Que se passe-t-ilsi nous oublions d'utiliser le signal? Que se passe-t-il si nous utilisons plusieurs de fois wait sans signal? Que se passe-t-il si le program s’arrêt avant signal? 46
  • 47.
    Oublier signal Processus 1 //shared variables a and s int a = 0; int s; wait (s); ... waits a lot // shared variables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } printf ("%dn", a); 47 Processus 2 ... 1
  • 48.
    Plusieurs de wait Processus1 // shared variables a and s int a = 0; int s; wait (s); ... waits a lot // shared variables a and s int a = 0; int s; wait (s); wait (s); ...waits a lot 48 Processus 2 ... ...
  • 49.
    Arrête avent signal Processus1 // shared variables a and s int a = 0; int s; wait (s); ... waits a lot // shared variables a and s int a = 0; int s; wait (s); if (a == 0) { a++; } 49 Processus 2 ... erreur
  • 50.
  • 51.
    Mutex • Mutual Exclusion •Similaire avec le sémaphore binaire • Utilisez pour threads (même processus) 51
  • 52.
    lock et unlock •lock – prend le mutex et mémorise le id de thread qui a le pris • la fonction lock est réentrante dans le même thread • unlock – libéré le mutex • au fin du program (thread) le mutex est libéré 52
  • 53.
    Deadlock Que se passe-t-ilsi nous oublions d'utiliser le unlock? Que se passe-t-il si nous utilisons plusieurs de fois wait sans lock? Que se passe-t-il si le program s’arrêt avant unlock? 53
  • 54.
    Oublier unlock Processus 1 //shared variables a and s int a = 0; mutex s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); // shared variables a and s int a = 0; mutex s; lock (s); if (a == 0) { a++; } printf ("%dn", a); 54 Processus 2 1 1
  • 55.
    Plusieurs de lock Processus1 // shared variables a and s int a = 0; mutex s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); // shared variables a and s int a = 0; mutex s; lock (s); lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); 55 Processus 2 1 1
  • 56.
    Arrête avent unlock Processus1 // shared variables a and s int a = 0; mutex s; lock (s); if (a == 0) { a++; } unlock (s); printf ("%dn", a); // shared variables a and s int a = 0; mutex s; lock (s); if (a == 0) { a++; } 56 Processus 2 1 erreur
  • 57.
    Synchronization en Java classRun { private Object a; public synchronized void runAlone () { // run some work } public void run () { synchronized (a) { // run some work using the variable a } } } 57
  • 58.
    Mot clés • course •espace critique • opération atomique • busy waiting • spinlock • lock • unlock • sémaphore • sémaphore binaire • sémaphore comptage • deadlock • wait • signal • mutex 58
  • 59.