From 396bc7433aa7fc3c404f2d175d1c93b82791cdde Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Mar 2018 19:55:01 +0100 Subject: [PATCH] TD1: Initial commit of exercice 3 code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Proper locking will be done later on. Signed-off-by: Jérôme Benoit --- TD1/exo3/BufferCirc.java | 57 +++++++++++++++++++++++ TD1/exo3/Consommateur.java | 25 +++++++++++ TD1/exo3/Main.java | 20 +++++++++ TD1/exo3/Makefile | 92 ++++++++++++++++++++++++++++++++++++++ TD1/exo3/Producteur.java | 26 +++++++++++ 5 files changed, 220 insertions(+) create mode 100644 TD1/exo3/BufferCirc.java create mode 100644 TD1/exo3/Consommateur.java create mode 100644 TD1/exo3/Main.java create mode 100644 TD1/exo3/Makefile create mode 100644 TD1/exo3/Producteur.java diff --git a/TD1/exo3/BufferCirc.java b/TD1/exo3/BufferCirc.java new file mode 100644 index 0000000..71204cc --- /dev/null +++ b/TD1/exo3/BufferCirc.java @@ -0,0 +1,57 @@ + + +/** + * implementation du producteur consommateur avec un buffer circulaire + */ +public class BufferCirc { + + private Object[] tampon; + private int taille; + private int prem, der, nbObj; + + + public BufferCirc (int t) { + taille = t; + tampon = new Object[taille]; + prem = 0; + der = 0; + nbObj = 0; + } + + + public boolean isEmpty() { + return nbObj == 0; + } + + + public boolean isFull() { + return nbObj == taille; + } + + + public synchronized void depose(Object obj) { + if (!isFull()) { + nbObj++; + tampon[prem] = obj; + prem = (prem + 1) % taille; + System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj); + } else { + System.out.println("Buffer plein pour " + Thread.currentThread().getName()); + } + } + + + public synchronized Object preleve() { + Object outObj = null; + if (isEmpty()) { + nbObj--; + outObj = tampon[der]; + der = (der + 1) % taille; + System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj); + } else { + System.out.println("Buffer vide pour " + Thread.currentThread().getName()); + } + return outObj; + } + +} // fin class BufferCirc diff --git a/TD1/exo3/Consommateur.java b/TD1/exo3/Consommateur.java new file mode 100644 index 0000000..5a54881 --- /dev/null +++ b/TD1/exo3/Consommateur.java @@ -0,0 +1,25 @@ + + +public class Consommateur implements Runnable { + + private BufferCirc buffer; + + + public Consommateur(BufferCirc b) { + buffer = b; + } + + + public void run() { + Integer val; + while (true) { + val = (Integer)buffer.preleve(); + System.out.println (Thread.currentThread().getName() + " a preleve " + val); + try { + Thread.sleep((int)(Math.random()*1000)); + } + catch (InterruptedException e) {} + } + } + +} // fin classe Consommateur diff --git a/TD1/exo3/Main.java b/TD1/exo3/Main.java new file mode 100644 index 0000000..929534c --- /dev/null +++ b/TD1/exo3/Main.java @@ -0,0 +1,20 @@ + +import java.util.ArrayList; + + +public class Main { + + + public static void main (String[] args) { + BufferCirc b = new BufferCirc(20); + Producteur p = new Producteur(b); + Consommateur c = new Consommateur(b); + Thread P1 = new Thread(p); + P1.setName("P1"); + Thread C1 = new Thread(c); + C1.setName("C1"); + P1.start(); + C1.start(); + } + +} diff --git a/TD1/exo3/Makefile b/TD1/exo3/Makefile new file mode 100644 index 0000000..c636449 --- /dev/null +++ b/TD1/exo3/Makefile @@ -0,0 +1,92 @@ +# 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 \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + BufferCirc.java \ + Producteur.java \ + Consommateur.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 diff --git a/TD1/exo3/Producteur.java b/TD1/exo3/Producteur.java new file mode 100644 index 0000000..10eab6d --- /dev/null +++ b/TD1/exo3/Producteur.java @@ -0,0 +1,26 @@ + + +public class Producteur implements Runnable { + + private BufferCirc buffer; + private int val; + + + public Producteur(BufferCirc b) { + buffer = b; + } + + + public void run() { + while (true) { + buffer.depose(new Integer(val)); + System.out.println (Thread.currentThread().getName() + " a depose " + val); + val++; + try { + Thread.sleep((int)(Math.random()*100)); + } + catch (InterruptedException e) {} + } + } + +} // fin classe Producteur -- 2.34.1