From: Jérôme Benoit <jerome.benoit@piment-noir.org>
Date: Fri, 9 Mar 2018 09:16:17 +0000 (+0100)
Subject: TD1: Explain and fix the deadlock in exo3.
X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7bd74736638acf238b5bcb2ae51e05519ad116ae;p=TD_SR.git

TD1: Explain and fix the deadlock in exo3.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
---

diff --git a/TD1/exo3/BufferCirc.java b/TD1/exo3/BufferCirc.java
index 68f1f1f..ebfb287 100644
--- a/TD1/exo3/BufferCirc.java
+++ b/TD1/exo3/BufferCirc.java
@@ -43,7 +43,7 @@ public class BufferCirc {
 		tampon[prem] = obj;
 		prem = (prem + 1) % taille;
 		//System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);
-		notify();
+		notifyAll();
 	}
 
 
@@ -61,7 +61,7 @@ public class BufferCirc {
 		outObj = tampon[der];
 		der = (der + 1) % taille;
 		//System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);
-		notify();
+		notifyAll();
 		return outObj;
 	}
 
diff --git a/TD1/exo3/Main.java b/TD1/exo3/Main.java
index 1d65a13..78c3b03 100644
--- a/TD1/exo3/Main.java
+++ b/TD1/exo3/Main.java
@@ -7,8 +7,8 @@ public class Main {
 
 	public static void main (String[] args) {
 		final int BUFFER_SIZE = 1;
-		final int PROD_NUMBER = 10;
-		final int CONS_NUMBER = 10;
+		final int PROD_NUMBER = 20;
+		final int CONS_NUMBER = 20;
 		BufferCirc b = new BufferCirc(BUFFER_SIZE);
 		Thread[] P = new Thread[PROD_NUMBER];
 		Thread[] C = new Thread[CONS_NUMBER];
diff --git a/TD1/exo3/deadlock.txt b/TD1/exo3/deadlock.txt
new file mode 100644
index 0000000..c926496
--- /dev/null
+++ b/TD1/exo3/deadlock.txt
@@ -0,0 +1,13 @@
+Trace deadlock avec deux consommateurs, deux producteurs et un tampon de taille 1.
+
+Buffer vide  -> C1 et C2 sont en attente (suspendus).
+             -> P1 rentre en section critique, depose et notifie un thread, mettons P2
+Buffer plein -> P1 se met en attente, P2 a été notifié mais le buffer est plein, donc
+                P2 en attente.
+             -> donc C1, C2, P1, P2 sont en attente
+                et aucune notification ne sera faite pour les réveiller.
+             -> deadlock.
+
+Solution: faire un notifyAll() en sortie de SC dans le buffer pour s'assurer que un
+          thread d'un type différent du type courant sortant du moniteur est notifié
+          (Consommateur -> Producteur ou Producteur -> Consommateur)