You are on page 1of 5

Module : Systèmes d'Exploitation Avancés Institut Supérieur d'Informatique

Niveau : 1ère année Ingénieur Année Universitaire : 2010-2011

TP N°3
SÉMAPHORES - CORRECTION
Barrière de Synchronisation

CLASSE PROCESSUS

package rdv;
class Processus extends Thread {
private RendezVous rv;
public Processus(RendezVous rv) {
this.rv = rv;
}
public void run() {
try {
Thread.sleep(6000 * (int) Math.random());
rv.proc_rdv();
} catch (InterruptedException e) {}
}
}

CLASSE MAIN

package rdv;
public class Main {
public static void main(String argv[]) {
int N = Integer.parseInt(argv[0]);
RendezVous rv = new RendezVous(N);
Processus process[] = new Processus[N];
for (int i = 0; i < N; i++) {
process[i] = new Processus(rv);
process[i].setName("Processus" + i);
process[i].start();
}
}
}

Mme. Lilia SFAXI Page 1/5


TP3 : Synchronisation des Threads en Java 2010-2011

CLASSE RENDEZ-VOUS

package rdv;

import java.util.concurrent.Semaphore;
public class RendezVous {
private int nbArrives;
private int N;
private Semaphore s;
private Semaphore mutex;
public RendezVous(int MaxProcess) {
nbArrives = 0;
s = new Semaphore(0, true);
mutex = new Semaphore(1, true);
N = MaxProcess;
}
public void proc_rdv() throws InterruptedException {
mutex.acquire();
nbArrives = nbArrives + 1;
if (nbArrives < N) {
mutex.release();
System.out.println(Thread.currentThread().getName()
+ " se bloque ");
s.acquire();
} else {
mutex.release();
System.out.println(Thread.currentThread().getName()
+ " est le dernier, il reveille les autres"
+ " processus ("+nbArrives+" sont arrivés)");
for (int i = 1; i <= N - 1; i++)
s.release();
}
}
}

Mme. Lilia SFAXI Page 2/5


TP3 : Synchronisation des Threads en Java 2010-2011

Producteur/Consommateur

CLASSE ITEM

package prodConsVect;

public class Item {


public double item;
public Item(double item) {
this.item = item;
}
}

CLASSE MAIN

package prodConsVect;

import java.util.Vector; CLASSE MAIN


import java.util.concurrent.Semaphore;
package rdv;
public class Main {
public class Main {
public static void main(String[] args) {
public static void main(String argv[]) {
Vector<Item> v = new Vector<Item>();
int N = Integer.parseInt(argv[0]);
Semaphore empty = new Semaphore(10);
RendezVous rv = new RendezVous(N);
Semaphore full = new Semaphore(0);
Processus process[] = new Processus[N];
Semaphore mutex = new Semaphore(1);
for (int i = 0; i < N; i++) {
Consommateur cons = new Consommateur(v, full, empty, mutex);
process[i] = new Processus(rv);
Producteur prod = new Producteur(v, full, empty, mutex);
process[i].setName("Processus" + i);
cons.start();
process[i].start();
prod.start();
}
}
}
}
}

Mme. Lilia SFAXI Page 3/5


TP3 : Synchronisation des Threads en Java 2010-2011

CLASSE PRODUCTEUR

package prodConsVect;
import java.util.Vector;
import java.util.concurrent.Semaphore;
public class Producteur extends Thread {
private Vector<Item> v;
private Semaphore full;
private Semaphore empty;
private Semaphore mutex;
public Producteur(Vector<Item> v, Semaphore full, Semaphore empty,
Semaphore mutex) {
super();
this.v = v;
this.full = full;
this.empty = empty;
this.mutex = mutex;
}
public void run() {
while (true) {
try {
empty.acquire();
mutex.acquire();
Item i = new Item(Math.random());
v.add(i);
System.out.println("Produced : " + i.item);
mutex.release();
full.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Mme. Lilia SFAXI Page 4/5


TP3 : Synchronisation des Threads en Java 2010-2011

CLASSE CONSOMMATEUR

package prodConsVect;
import java.util.Vector;
import java.util.concurrent.Semaphore;
public class Consommateur extends Thread {
private Vector<Item> v;
private Semaphore full;
private Semaphore empty;
private Semaphore mutex;
public Consommateur(Vector<Item> v, Semaphore full, Semaphore empty,
Semaphore mutex) {
super();
this.v = v;
this.full = full;
this.empty = empty;
this.mutex = mutex;
}
public void run() {
while (true) {
try {
full.acquire();
mutex.acquire();
Item i = v.firstElement();
System.out.println("Consumed : " + i.item);
mutex.release();
empty.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Mme. Lilia SFAXI Page 5/5

You might also like