reporte viernes 24

22
BENEMERITA UNIVERSIDAD AUTONOMA DE PUEBLA FACULTAD DE CIENCIAS DE LA COMPUTACION PROGRAMACION CONCURRENTE Y PARALELA PROGRAMAS DE MONITORES

Upload: antares-zehcnas

Post on 01-Jul-2015

140 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reporte viernes 24

BENEMERITA UNIVERSIDAD

AUTONOMA DE PUEBLA

FACULTAD DE CIENCIAS

DE LA COMPUTACION

PROGRAMACION CONCURRENTE Y PARALELA

PROGRAMAS DE MONITORES

Page 2: Reporte viernes 24

LEZAMA SANCHEZ ANA LAURA

H

Introducción:

En esta práctica se realizaron 4 programas con monitores

Desarrollo:

El primer programa es el mismo que el de la práctica 4, donde se simula el retiro y deposito de

dinero.

public class cliente1 implements Runnable

{

private Monitor_Cajero caja1;

public cliente1(Monitor_Cajero A)

{

this.caja1=A;

}

public void run()

{

Page 3: Reporte viernes 24

int cantidad;

for (int i=0; i<5; i++)

{

do

{

cantidad=((int)(Math.random()*1000));

}

while (cantidad<1 || cantidad>1000);

System.out.println ("Retirando: "+cantidad);

caja1.retira(cantidad);

}

}

}

public class cliente2 extends Thread

{

private Monitor_Cajero caja1;

public cliente2(Monitor_Cajero A)

{

this.caja1=A;

}

public void run()

{

int cantidad;

Page 4: Reporte viernes 24

for (int i=0; i<5; i++)

{

do

{

cantidad=((int)(Math.random()*1000));

}

while (cantidad<1 || cantidad>1000);

System.out.println ("Ingresando: "+cantidad);

caja1.ingresar(cantidad);

}

}

}

public class Monitor_Cajero

{

private int cantidad;

public Monitor_Cajero()

{

cantidad=10000;

}

public synchronized void retira(int x)

{

while (cantidad-x <=0)

try {wait();}

Page 5: Reporte viernes 24

catch (InterruptedException e)

{

System.err.println ("Error"+e.toString());

}

cantidad=cantidad-x;

notify();

}

public synchronized void ingresar(int x)

{

while (cantidad+x>=10000)

try {wait();}

catch (InterruptedException e){System.err.println ("Error"+e.toString());}

cantidad=cantidad+x;

notify();

}

}

//clase principal

import java.io.*;

public class cajeroautomatico

{

public static void main (String []args)

{

Monitor_Cajero A=new Monitor_Cajero();

Page 6: Reporte viernes 24

cliente1 cli1=new cliente1(A);

cliente2 cli2=new cliente2(A);

Thread hilo1=new Thread(cli1);

Thread hilo2=new Thread(cli2);

hilo1.start();

hilo2.start();

}

}

SALIDA

********************************************************************************

El segundo programa es el mismo que el de la practica 4 pero solo este se presenta con semáforos

y su ejecución es la misma.

public class SemaforoBinario {

protected int contador = 0;

public SemaforoBinario (int valorInicial) {

contador = valorInicial;

}

Page 7: Reporte viernes 24

synchronized public void WAIT () {

while (contador == 0)

try {

wait();

}

catch (Exception e) {}

contador--;

}

synchronized public void SIGNAL () {

contador = 1;

notify();

}

}

class Semaphore

{

private int count;

public Semaphore(int n)

{

this.count = n;

}

public synchronized void WAIT()

{

while(count == 0)

{

Page 8: Reporte viernes 24

try

{

wait();

}

catch (InterruptedException e) {//keep trying}

}

count--;

}

}

public synchronized void SIGNAL()

{

count++;

notify();

}

}

public class cajero

{

private int cantidad;

public cajero() {cantidad=10000;}

public synchronized void retira(int x)

{

while (cantidad-x <=0)

try {wait();}

catch (InterruptedException e)

{

Page 9: Reporte viernes 24

System.err.println ("Error"+e.toString());

}

cantidad=cantidad-x;

notify();

}

public synchronized void ingresar(int x)

{

while (cantidad+x>=10000)

try {wait();}

catch (InterruptedException e){System.err.println ("Error"+e.toString());}

cantidad=cantidad+x;

notify();

}

}

import java.io.*;

class Procesos1

{

protected static final SemaforoBinario SA = new SemaforoBinario(0);

protected static final SemaforoBinario SB = new SemaforoBinario(0);

protected static final SemaforoBinario SC = new SemaforoBinario(0);

protected static final SemaforoBinario SD = new SemaforoBinario(0);

protected static final SemaforoBinario SE = new SemaforoBinario(0);

protected static final SemaforoBinario mutex = new SemaforoBinario(1);

Page 10: Reporte viernes 24

public static void main(String args[]) throws IOException

{

try

{

cajero caja=new cajero();

Thread cli1 = new Thread(new cliente1(caja));

Thread cli2 = new Thread(new cliente2(caja));

cli1.start();

cli2.start();

Thread.sleep(300);

}

catch (InterruptedException e) {}

// System.out.println(" Termine...");

// System.exit(0);

}

}

class cliente1 extends Procesos1 implements Runnable

{

private cajero caja1;

public cliente1(cajero x)

{

this.caja1=x;

Page 11: Reporte viernes 24

}

public void run()

{

try

{

mutex.WAIT();

int cantidad;

for (int i=0; i<5; i++)

{

do

{

cantidad=((int)(Math.random()*1000));

}

while (cantidad<1 || cantidad>1000);

System.out.println ("Retirando: "+cantidad);

caja1.retira(cantidad);

}

Thread.sleep(500);

mutex.SIGNAL();

SA.SIGNAL();

}

catch (InterruptedException e) {}

}

}

Page 12: Reporte viernes 24

public class cliente2 extends Procesos1 implements Runnable

{

private cajero caja1;

public cliente2(cajero x)

{

this.caja1=x;

}

public void run()

{

try

{

mutex.WAIT();

int cantidad;

for (int i=0; i<5; i++)

{

do

{

cantidad=((int)(Math.random()*1000));

}

while (cantidad<1 || cantidad>1000);

System.out.println ("Ingresando: "+cantidad);

caja1.ingresar(cantidad);

}

Thread.sleep(500);

Page 13: Reporte viernes 24

mutex.SIGNAL();

SB.SIGNAL();

}

catch (InterruptedException e) {}

}

}

public class cliente3 extends Procesos1 implements Runnable

{

private cajero caja1;

public cliente3 (cajero x)

{

this.caja1=x;

}

public void run()

{

try

{

mutex.WAIT();

int cantidad;

for (int i=0; i<5; i++)

{

do

Page 14: Reporte viernes 24

{

cantidad=((int)(Math.random()*1000));

}

while (cantidad<1 || cantidad>1000);

System.out.println ("Ingresando: "+cantidad);

caja1.ingresar(cantidad);

}

Thread.sleep(500);

mutex.SIGNAL();

SB.SIGNAL();

}

catch (InterruptedException e) {}

}

}

//clase principal

import java.io.*;

public class cajeroautomatico

{

public static void main (String []args)

{

cajero caja=new cajero();

cliente1 c1=new cliente1(caja);

cliente2 c2=new cliente2(caja);

cliente3 c3=new cliente3(caja);

c1.start();

Page 15: Reporte viernes 24

c2.start();

c3.start();

}

}

Salida

********************************************************************************

El tercer programa es el monitor ejemplo donde desde el cuerpo del código sele asigno una

cantidad de 100 y de acuerdo a las instrucciones se incrementa o decrementa.

public class proceso1 implements Runnable

{

Monitor_Ejemplo A;

int i;

public proceso1(Monitor_Ejemplo A)

{

this.A=A;

this.i=100;

}

public void run()

{

Page 16: Reporte viernes 24

A.dec(i);

A.inc(i);

A.valor();

}

}

public class proceso2 implements Runnable

{

Monitor_Ejemplo A;

int i;

public proceso2(Monitor_Ejemplo A)

{

this.A=A;

this.i=20;

}

public void run()

{

A.dec(i);

A.inc(i);

A.valor();

}

}

Page 17: Reporte viernes 24

public class Monitor_Ejemplo

{

private int dato;

public Monitor_Ejemplo()

{

dato=10;

}

public synchronized void dec(int cant)

{

dato=dato-cant;

System.out.println(dato);

}

public synchronized void inc(int cant)

{

dato=dato+cant;

System.out.println(dato);

}

public synchronized int valor()

{

System.out.println(dato);

return dato;

}

Page 18: Reporte viernes 24

}

public class Principal

{

public static void main(String args[])

{

Monitor_Ejemplo A=new Monitor_Ejemplo();

proceso1 pro1=new proceso1(A);

proceso2 pro2=new proceso2(A);

Thread hilo1=new Thread(pro1);

Thread hilo2=new Thread(pro2);

hilo1.start();

hilo2.start();

}

SALIDA

Page 19: Reporte viernes 24

********************************************************************************

El cuarto programa es el monitor incremento donde el único método es el imprimir del al 10, con

2 hilos imprimiendo cada hilo 5 números de forma sincronizada.

public class proceso1 implements Runnable

{

Monitor_Incremento A;

int i;

public proceso1(Monitor_Incremento A)

{

this.A=A;

}

public void run()

{

for(int i=1;i<=5;i++)

{

A.sumaUno();

}

}

}

public class proceso2 implements Runnable

{

Monitor_Incremento A;

int i;

public proceso2(Monitor_Incremento A)

Page 20: Reporte viernes 24

{

this.A=A;

}

public void run()

{

for(int i=1;i<=5;i++)

{

A.sumaUno();

}

}

}

public class Monitor_Incremento

{

private int x;

public Monitor_Incremento()

{

x=0;

}

public synchronized void sumaUno()

{

x=x+1;

System.out.println(x);

}

Page 21: Reporte viernes 24

}

public class PruebaMonitor

{

public static void main(String[]args)

{

Monitor_Incremento A=new Monitor_Incremento();

proceso1 pro1=new proceso1(A);

proceso2 pro2=new proceso2(A);

Thread hilo1=new Thread(pro1);

Thread hilo2=new Thread(pro2);

hilo1.start();

hilo2.start();

}

}

SALIDA

*******************************************************************************

Conclusión:

Page 22: Reporte viernes 24

En la elaboración de estos programas se asimilo de mejor forma el concepto de monitor

Bibliografia

http://www.cs.buap.mx/~mceron/principal_con.html