Tp n°4 moniteurs correction - systèmes d’exploitation - t -

Télécharger PDF

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.

pack complet des cours, TDs, TPs et examens exercices sur Systèmes d’Exploitation

Accédez à une collection complète des supports de cours, des travaux dirigés (TDs) corrigés, TPs avec solution, examens...

Télécharger pack

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

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

TP N°

TP N°4 4M M

ONITEURS

ONITEURS - C - C

ORRECTION

ORRECTION

Allocateur de ressources

Mme. Lilia SFAXIPage 1/5C CLASSE LASSEP P

ROCESSUS

ROCESSUS

package resources;

public class Processus extends Thread {

private Ressource ressources;

public Processus(Ressource ressources) {

this.ressources = ressources;} public void run() {

int m = (int)Math.random() * 10;

try {

ressources.request(m);

sleep((long) (Math.random() * 200));

} catch (InterruptedException e) {e.printStackTrace();}

ressources.release(m);} }C CLASSE LASSEM MAIN AIN

package resources;

public class Main {

public static void main(String[] args) {

int nbThr = 5;

Ressource ressource = new Ressource();

for (int i = 0; i < nbThr; i++){

Processus p = new Processus(ressource);

p.start();} }} TP4 : Moniteurs2010-2011

Mme. Lilia SFAXIPage 2/5C CLASSE LASSER R

ESSOURCE

ESSOURCE

package resources;

public class Ressource {

public int dispo = 10;

public synchronized void request(int m) throws InterruptedException {

while (dispo < m) {

System.out.println("**** " + Thread.currentThread().getName()

+ " en veut " + m + "... doit attendre :(");wait(); }

System.out.println("Le demandeur " + Thread.currentThread().getName()

+ " utilise " + m + " ressources!");

dispo -= m;

System.out.println("Ressources disponibles : " + dispo);} public synchronized void release(int m) {

System.out.println("Le demandeur " + Thread.currentThread().getName()

+ " libère " + m + " ressources!");

dispo += m;

System.out.println("Ressources disponibles : " + dispo);

notifyAll();} }

TP4 : Moniteurs2010-2011

Compteur

Mme. Lilia SFAXIPage 3/5C CLASSE LASSES SYNCHRO YNCHROC CLASS LASS

package synchropairimpairthread;

public class SynchroClass {

public int tour = 0;} CC LASSELASSE MM AINAIN package synchropairimpairthread;

public class Main {

public static void main(String[] args) {

SynchroClass synchronizer = new SynchroClass();

ImpairThread thimp = new ImpairThread(1,synchronizer);

PairThread thp = new PairThread(0,synchronizer);

thp.start();

thimp.start();} }

TP4 : Moniteurs2010-2011

Mme. Lilia SFAXIPage 4/5C CLASSE LASSEP PAIR AIRT THREAD HREAD

package synchropairimpairthread;

public class PairThread extends Thread{

private int debut;

private SynchroClass sync ;

public PairThread(int start, SynchroClass s){

début = start;

sync = s;} public void run(){

while(true){

synchronized(sync){

try {

if (sync.tour != 0) {

sync.wait();} System.out.print(" " + debut);

début += 2;

sync.tour = 1;

sync.notify();

} catch (InterruptedException ex) {} }} }} TP4 : Moniteurs2010-2011

Mme. Lilia SFAXIPage 5/5C CLASSE LASSEI IMPAIR MPAIRT THREAD HREAD

package synchropairimpairthread;

public class ImpairThread extends Thread{

private int debut;

private SynchroClass sync ;

public ImpairThread(int start, SynchroClass s){

début = start;

sync = s;} public void run(){

while(true){

synchronized(sync){

try {

if (sync.tour != 1) {

sync.wait();} System.out.print(" " + debut);

début += 2;

sync.tour = 0;

sleep(1000);

sync.notify();

} catch (InterruptedException ex) {} }} }

}

Partagez vos remarques, questions ou propositions d'amélioration ici...

Enregistrer un commentaire (0)
Plus récente Plus ancienne

Publicité 1

Publicité 2