TD2: Add preliminary code for question 1.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Mar 2018 20:03:08 +0000 (21:03 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Mar 2018 20:03:08 +0000 (21:03 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/exo3/deadlock.txt
TD2/ClientSimplifie.java [new file with mode: 0644]
TD2/Main.java [new file with mode: 0644]
TD2/Makefile [new file with mode: 0644]

index 1dcd56943aa5cfa776549d26804a6a018d9b3462..0aa206a80ead45ad790e0498a0fb747e8d5eba68 100644 (file)
@@ -1,13 +1,19 @@
-Trace deadlock avec deux consommateurs, deux producteurs et un tampon de taille 1.
+Pseudo-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.
+Buffer vide  -> C1 et C2 se mettent en attente dans la file (C1, C2).
+             -> P1 rentre en section critique et P2 se met en attente soit (P2, C1, C2).
+                P1 depose et notifie a un thread en attente. Le dernier en attente est P2 donc P2 est notifié.
+Buffer plein -> P2 sort de la file d'attente (C1, C2) et entre en SC, P1 se met en attente (P1, C1, C2).
+                Or le buffer est plein, donc P2 se met en attente dans la file (P2, P1, C1, C2).
+             -> donc C1, C2, P1, P2 sont en attente dans la file (P2, P1, C1, C2)
+                et aucune notification ne sera faite pour les sortir de la file.
              -> deadlock.
 
 Solution: faire un notifyAll() en sortie de SC dans le buffer pour s'assurer que un
              -> 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) et sort de son attente. 
+          thread d'un type différent du type courant sortant du moniteur est également notifié
+          (Consommateur -> Producteur ou Producteur -> Consommateur) et sort de son attente.
+
+N.B. : On suppose dans la mise en attente dans moniteur des threads est une file.
+       Mais même si c'est une autre structure de données, la possibilité de deadlock
+       demeure.
diff --git a/TD2/ClientSimplifie.java b/TD2/ClientSimplifie.java
new file mode 100644 (file)
index 0000000..35f389d
--- /dev/null
@@ -0,0 +1,68 @@
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class ClientSimplifie {
+    BufferedReader lecture;  // pour le flot d'entrée venant du serveur
+    PrintWriter ecriture;  // pour le flot de sortie vers le serveur
+    Socket sock; // le socket client
+
+    public ClientSimplifie() {
+        // établie une connexion au serveur par un appel
+        // à connexionServeur()
+        connexionServeur("localhost", 5000);
+    }
+
+    public ClientSimplifie(String adresseIPServeur, int portServeur) {
+        // établie une connexion au serveur par un appel
+        // à connexionServeur()
+        connexionServeur(adresseIPServeur, portServeur);
+    }
+
+    private void connexionServeur(String adresseIPServeur, int portServeur) {
+        // créer un objet socket lié au socket serveur et l'affecte à sock
+        // puis établie les chaînages de flot nécessaires
+        // pour l'envoi et la reception de messages
+        try {
+            sock = new Socket(adresseIPServeur, portServeur);
+        }
+        catch (IOException e) {}
+        InputStream IStream = null;
+        try {
+            IStream = sock.getInputStream();
+        } catch (IOException e) {}
+        InputStreamReader IMesg = new InputStreamReader(IStream);
+        lecture = new BufferedReader(IMesg);
+
+        OutputStream OStream = null;
+        try {
+            OStream = sock.getOutputStream();
+        }
+        catch (IOException e) {}
+        ecriture = new PrintWriter(OStream);
+    }
+
+    public void sendMsg(String msg) {
+        ecriture.println(msg + "\n");
+        ecriture.flush();
+    }
+
+    public String receiveMsg() {
+        String line = new String();
+        try {
+            //FIXME: read only the line before the ending newline
+            line = lecture.readLine();
+        }
+               catch (IOException e) {}
+        return line;
+    }
+
+    public void closeRWIO() {
+        ecriture.close();
+               try {
+                       lecture.close();
+               }
+               catch (IOException e) {}
+    }
+
+} // fin classe ClientSimplifie
diff --git a/TD2/Main.java b/TD2/Main.java
new file mode 100644 (file)
index 0000000..9e4fba5
--- /dev/null
@@ -0,0 +1,19 @@
+import java.io.*;\r
+\r
+\r
+public class Main {\r
+\r
+\r
+       public static void main (String[] args) {\r
+\r
+               ClientSimplifie client = new ClientSimplifie();\r
+\r
+               client.sendMsg("Line 1 Line 2");\r
+               String msg = client.receiveMsg();\r
+               System.out.println(msg);\r
+\r
+               client.closeRWIO();\r
+\r
+       }\r
+\r
+}\r
diff --git a/TD2/Makefile b/TD2/Makefile
new file mode 100644 (file)
index 0000000..2ff069e
--- /dev/null
@@ -0,0 +1,90 @@
+# define compiler and compiler flag variables
+# define a variable for compiler flags (JFLAGS)
+# define a variable for the compiler (JC)
+# define a variable for the Java Virtual Machine (JVM)
+
+JFLAGS = -g
+JC = javac
+JVM = java
+
+#
+# Clear any default targets for building .class files from .java files; we
+# will provide our own target entry to do this in this makefile.
+# make has a set of default targets for different suffixes (like .c.o)
+# Currently, clearing the default for .java.class is not necessary since
+# make does not have a definition for this target, but later versions of
+# make may, so it doesn't hurt to make sure that we clear any default
+# definitions for these
+#
+
+.SUFFIXES: .java .class
+
+
+#
+# Here is our target entry for creating .class files from .java files
+# This is a target entry that uses the suffix rule syntax:
+#      DSTS:
+#              rule
+# DSTS (Dependency Suffix     Target Suffix)
+# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency
+#  file, and 'rule'  is the rule for building a target
+# '$*' is a built-in macro that gets the basename of the current target
+# Remember that there must be a < tab > before the command line ('rule')
+#
+
+.java.class:
+       $(JC) $(JFLAGS) $*.java
+
+
+#
+# CLASSES is a macro consisting of N words (one for each java source file)
+# When a single line is too long, use \<return> to split lines that then will be
+# considered as a single line. For example:
+# NAME = Camilo \
+         Juan
+# is understood as
+# NAME = Camilo        Juan
+
+CLASSES = \
+               ClientSimplifie.java \
+               Main.java
+
+#
+# MAIN is a variable with the name of the file containing the main method
+#
+
+MAIN = Main
+
+#
+# the default make target entry
+# for this example it is the target classes
+
+default: classes
+
+
+# Next line is a target dependency line
+# This target entry uses Suffix Replacement within a macro:
+# $(macroname:string1=string2)
+# In the words in the macro named 'macroname' replace 'string1' with 'string2'
+# Below we are replacing the suffix .java of all words in the macro CLASSES
+# with the .class suffix
+#
+
+classes: $(CLASSES:.java=.class)
+
+
+# Next two lines contain a target for running the program
+# Remember the tab in the second line.
+# $(JMV) y $(MAIN) are replaced by their values
+
+run: $(MAIN).class
+       $(JVM) $(MAIN)
+
+# this line is to remove all unneeded files from
+# the directory when we are finished executing(saves space)
+# and "cleans up" the directory of unneeded .class files
+# RM is a predefined macro in make (RM = rm -f)
+#
+
+clean:
+       $(RM) *.class