Correction TD ProdCons
Correction TD ProdCons
Correction TD ProdCons
Imene Lahyani
Variable
partagée
Processus 1 Processus 2
… Section non critique …Section non critique
If (s>0) s=s-1
s.acquire() s.acquire() Else attendre
…………x…….. …………x……..
…………x……..
Section critique 1 …………x……..
Section critique 2
…………x…….. …………x……..
s.release() s.release ()
s=s+1
take()
class ProdConsSem
{
ProdConsSem
{ // constructeur
}
public static void main(String argv[])
{
ProdConsSem pc = new ProdConsSem(10) ;
producteur.setName("Producteur") ;
producteur.start() ;
consommateur.setName("Consommateur") ;
consommateur.start() ;
}
public static void main(String argv[])
{
try{
producteur.setName("Producteur") ;
producteur.start() ;
consommateur.setName("Consommateur") ;
consommateur.start() ;
}
catch(ArrayIndexOutOfBoundsException exp)
{ System.out.println("erreur");
}
}}
class Producteur extends Thread {
Producteur ()
{}
Void deposer ()
{
}
public void run() {
}
Deposer()
}
class Consommateur extends Thread {
Consommateur ()
{}
Int prelever()
{
}
Int x = prelever()
}
import java.util.concurrent.Semaphore ;
class ProdConsSem
{ static int tampon[]; static int N;
ProdConsSem (int N)
{
This.N=N;
tampon = new int [N] ;
}
public static void main(String argv[])
{
ProdConsSem pc = new ProdConsSem(10) ;
Producteur producteur = new producteur () ;
Consommateur consommateur = new consommateur () ;
producteur.setName("Producteur") ;
producteur.start() ;
consommateur.setName("Consommateur") ;
consommateur.start() ;
}}
class Producteur extends Thread {
Static Int in ;
Producteur ()
{}
Void deposer ()
{
int m= (int)(1000*Math.random());
ProdConsSem [in] =m ;
in = (in +1)% N;
System.out.println(Thread.currentThread().getName() + " vient de produire "+ m) ;
}
public void run() {
}
Deposer()
}
class Consommateur extends Thread {
Static Int out
Consommateur ()
{}
Int prelever()
{
int m= ProdConsSem .tampon[tete];
}
class Consommateur extends Thread {
Static int out
Consommateur ()
{} Un consommateur ne peut pas consommer d’un tampon vide
Int Prelever()
{
int m= ProdConsSem .tampon[out];
out = (out + 1)% ProdConsSem .N;
System.out.println(Thread.currentThread().getName() + " vient
de consommer " + m) ;
return m ;
}
public void run() { If(nbcaseplein>0) nbcaseplein=nbcaseplein-1
try { Else attendre
{
ProdConsSem.nbcasePlein.acquire());
ProdConsSem.S.acquire()
int m = Prelever() ;
ProdConsSem.S.release());
ProdConsSem.nbcaseVide.release(); Nbcasevide=nbcasevide+1
} }
catch(InterruptedException e) {} } // fin
run
} // fin de la classe Consommateur
class Producteur extends Thread {
Static int in
Producteur ()
{} Un producteur ne peut pas produire dans un tampon plein
Void deposer ()
{int m= (int)(1000*Math.random());
ProdConsSem .tampon[in] =m ;
in = (in +1)% ProdConsSem .N;
System.out.println(Thread.currentThread().getName() + " vient de produire "+ m) ;
}
public void run() {
try If(nbcasevide>0) nbcasevide=nbcasevide-1
{ { Else attendre
int m= (int)(1000*Math.random());
ProdConsSem.nbcaseVide.acquire();
ProdConsSem.S.acquire();
Deposer(m);
ProdConsSem.S.release(); Nbcaseplein=nbcaseplein+1
ProdConsSem.nbcasePlein.release()();
}
} catch(InterruptedException e) {} }// fin run
}
import java.util.concurrent.Semaphore ;
class ProdConsSem
{ static int tampon[]; static int N;
ProdConsSem (int N)
{ This.N=N;
tampon = new int [N] ;
Static nbcaseVide=new Semaphore (N, true);
Static nbcasePlein=new Semaphore(0,true);
Static S=new Semaphore (1,true);}
0 ff ff 00
2 Flag[1]=true f true 0
3 Test(token<>0) f true 0
5 Test (token<>0)
******SC*****
6 Token=1 1
7 Test(token<>0)
******SC*****
ex3
Une piscine qui contient C cabines peut contenir au maximum un nombre N de nageurs.
Chaque nageur dispose d'un panier pour ranger ses habits ; il y a donc P paniers.
Pour se déshabiller, les nageurs ont besoin d'une cabine et d'un panier pour placer leurs
habits.
Pendant qu'ils se baignent, ils conservent le panier. Après la baignade, il leur faut à
nouveau
une cabine pour se ré-habiller. Enfin, avant de sortir, ils libèrent le panier et la cabine.
Les conditions d'utilisation sont les suivantes :
1) il y a P paniers disponibles pour ranger leurs habits ; il ne pourra donc pas y
avoir plus de P baigneurs simultanément ;
2) il y a d'autre part C cabines d'habillage, avec 1 < C << P<<N.
Développez une solution à ce problème en utilisant les sémaphores.
EX 3
1) variable partagé ? nbcabine, nbpanier
2) threads ? Nageur
3) sections critiques ?
Reservercabine()
Reserverpanier()
Reserverpanier()
libererpanier()
public class Piscine {
int P,C;
Piscine(int P, int C)
{
this.P=P;
this.C=C;
}
Public static void main (String args[])
{
Piscine ps=new Piscine(3,2);
Nageur n1=new Nageur("n1");
Nageur n2=new Nageur("n2");
n1.start()
n2.start()
}
public class Nageur extends Thread {
Nageur(String Nom)
{
super(Nom);
}
public void run(){
*********************
}
}
while(true){ ReserverCabine();
ReserverPanier(); shabiller();
acqCabine(); try {
sleep(2000);
seDeshabiller(); } catch
try { (InterruptedException e) {}
sleep(2000);
libererCabine();
}
catch (InterruptedException e) {} libererPanier();
libererCabine(); } } // fin run
nager(Nom);
try {
sleep(4000);
} catch
(InterruptedException e) {
}
public void ReserverCabine() public void ReserverPanier()
{ {
try { try {
piscine.CabineDisp.acquire(); piscine.PanierDisp.acquire();
} catch } catch
(InterruptedException e) {} (InterruptedException e) {}
System.out.println("Le
nageur "+getName()+" Acquerir panier");
System.out.println("Le
nageur "+getName()+" Acquerir Cabine"); }
}
public void seDeshabiller() public void libererPanier()
{ {
System.out.println("Le nageur System.out.println("Le nageur "+
"+getName()+" Se deshabiller"); getName()+" +" libère le panier");
} PanierDisp.release();
public void shabiller() }
{
System.out.println("Le nageur "+ public void nager()
getName()+" +" Se habiller"); {
} System.out.println("Le nageur "+
public void libererCabine() getName()+" +" Je suis entrain de nager");
{ }
System.out.println("Le nageur "+ }
getName()+" +" libère la cabine"); }// fin nageur
CabineDisp.release();
}
public class Piscine {
static int P,C;
static Semaphore CabineDisp,PanierDisp;
Piscine(int P, int C)
{
this.P=P;
this.C=C;
CabineDisp=new Semaphore(C);
PanierDisp=new Semaphore(P);
}
Public static void main (String args[])
{
Piscine ps=new Piscine(3,2);
Nageur n1=new Nageur("n1");
Nageur n2=new Nageur("n2");
n1.start()
n2.start()
Public class piscine
Class nageur extends thread
{
}
X=3 p1 X=6
S1=0
TD – ex 1(a completer) p3
S1=0(1)
S2=0 S2=0
X=0 X=6
p3 X=3 p1 X=3 p3 X=3 p1
S1=0 S1=1
S1=0 S1=1 S1=1
(1) S2=0
S2=1 S2=0 S2=0
p2 S2=0
X=0
S1=1
S2=0 p3 X=3
p3 S1=1
X=3 S2=1
S1=0
S2=1
p1 X=3
S1=0
S2=0