Obtenir le pack complet des cours, TDs, examens sur Systèmes d’Exploitation!
Vous souhaitez maîtriser Systèmes d’Exploitation ? Ne cherchez plus, nous avons le pack bien choisi pour vous.
Accédez à une collection complète des supports de cours, des travaux dirigés (TDs) corrigés, TPs avec solution, examens...
Télécharger packModule : Systèmes d'Exploitation AvancésInstitut Supérieur d'Informatique
Niveau : 1ère année IngénieurAnnée Universitaire : 2010-2011
TP N°3
TP N°3S S
ÉMAPHORES
ÉMAPHORES - C - C
ORRECTION
ORRECTION
Barrière de Synchronisation
Mme. Lilia SFAXIPage 1/5C CLASSE LASSEP P
ROCESSUS
ROCESSUS
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) {}
} }C CLASSE LASSEM MAIN AIN
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();} }} TP3 : Synchronisation des Threads en Java2010-2011
Mme. Lilia SFAXIPage 2/5C CLASSE LASSE R RENDEZ ENDEZ- -VOUS 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();} }} TP3 : Synchronisation des Threads en Java2010-2011
Producteur/Consommateur
Mme. Lilia SFAXIPage 3/5C CLASSE LASSEM MAIN AIN
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();} }} CC LASSELASSE II TEMTEM package prodConsVect;
public class Item {
public double item;
public Item(double item) {
this.item = item;} }C CLASSE LASSEM MAIN AIN
package prodConsVect;
import java.util.Vector;
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
Vector<Item> v = new Vector<Item>();
Semaphore empty = new Semaphore(10);
Semaphore full = new Semaphore(0);
Semaphore mutex = new Semaphore(1);
Consommateur cons = new Consommateur(v, full, empty, mutex);
Producteur prod = new Producteur(v, full, empty, mutex);
cons.start();
prod.start();} }
TP3 : Synchronisation des Threads en Java2010-2011
Mme. Lilia SFAXIPage 4/5C CLASSE LASSEP P
RODUCTEUR
RODUCTEUR
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();} }} }
TP3 : Synchronisation des Threads en Java2010-2011
Mme. Lilia SFAXIPage 5/5C CLASSE LASSEC C
ONSOMMATEUR
ONSOMMATEUR
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();} }} }
