Академический Документы
Профессиональный Документы
Культура Документы
Facultad de Ingeniera
Practica de laboratorio N5
Estudiante:
Cdigo:
2012-36144
Facultad:
Facultad de Ingeniera
Escuela profesional:
Asignatura:
Nombre de la prctica:
Fecha:
21/06/2013
Hora:
6pm 8pm
Tacna 2013
Practica 05-Algoritmos paralelos
Alumno: Kevin Mike Herrera Vega
Cdigo: 2012-36144
PROCESS banquero;
VAR c1, c2, cant: Integer;
BEGIN
{REPEAT}
while Turno = 1 do;
c1 := RANDOM(NCTAS-1)+1; (* Origen *)
c2 := RANDOM(NCTAS-1)+1; (* Destino *)
cant:=0;
IF (Cuenta[c1]>10) THEN cant := RANDOM(10);
Cuenta[c1] := Cuenta[c1] - cant ;
Cuenta[c2] := Cuenta[c2] + cant ;
writeln('Operacion realizada con exito');
Turno := 1;
{FOREVER}
END;
BEGIN (* Principal *)
Turno := 1;
For i:= 1 TO NCTAS DO cuenta[i]:=100;
COBEGIN
Banquero;
Auditor;
COEND
END.
Segundo Intento:
PROGRAM bancosegundointento;
CONST NCTAS=20;
VAR Cuenta : array[1..NCTAS] of integer; (* Almacena los saldos *)
i: integer;
pizarra1 : integer ;
pizarra2 : integer ;
{1 : pescado y 2: no pescando porseaca XD}
PROCESS Auditor;
VAR k,ci, Total:INTEGER;
BEGIN
REPEAT
while pizarra2 = 1 do;
pizarra1:=1;
Total:=0;
FOR
k:=1 TO Nctas do
Cuenta[k] := 100;
FOR ci:=1 TO NCTAS DO
Total:= Total + Cuenta[ci];
Writeln('Total : ', Total);
pizarra1 := 2;
FOREVER
END;
PROCESS banquero;
VAR c1, c2, cant: Integer;
BEGIN
REPEAT
while pizarra1 = 1 do;
pizarra1:= 1;
c1 := RANDOM(NCTAS-1)+1; (* Origen *)
c2 := RANDOM(NCTAS-1)+1; (* Destino *)
cant:=0;
IF (Cuenta[c1]>10) THEN cant := RANDOM(10);
Cuenta[c1] := Cuenta[c1] - cant ;
Cuenta[c2] := Cuenta[c2] + cant ;
pizarra2:= 2;
FOREVER
END;
BEGIN (* Principal *)
pizarra1:= 2;
pizarra2:=2;
For i:= 1 TO NCTAS DO cuenta[i]:=100;
COBEGIN
Banquero;
Auditor;
COEND
END.
Tercer Intento:
PROGRAM bancotercerintento;
CONST NCTAS=20;
VAR Cuenta : array[1..NCTAS] of integer; (* Almacena los saldos *)
i: Integer;
pizarra1,pizarra2: boolean;
PROCESS Auditor;
VAR k,ci, Total:INTEGER;
BEGIN
{REPEAT}
while pizarra2 = true do;
pizarra1 := true;
Total:=0;
FOR k:=1 TO Nctas do
Cuenta[k] := 100;
FOR ci:=1 TO NCTAS DO
Total:= Total + Cuenta[ci];
Writeln('Total : ', Total);
pizarra1 := false;
{FOREVER}
END;
PROCESS banquero;
VAR c1, c2, cant: Integer;
BEGIN
{REPEAT}
while pizarra1 = true do;
pizarra2 := true;
c1 := RANDOM(NCTAS-1)+1; (* Origen *)
c2 := RANDOM(NCTAS-1)+1; (* Destino *)
cant:=0;
IF (Cuenta[c1]>10) THEN cant := RANDOM(10);
Cuenta[c1] := Cuenta[c1] - cant ;
Cuenta[c2] := Cuenta[c2] + cant ;
writeln('Operacion realizada con exito');
pizarra2 := false;
{FOREVER}
END;
BEGIN (* Principal *)
pizarra1 := false;
pizarra2 := false;
For i:= 1 TO NCTAS DO cuenta[i]:=100;
COBEGIN
Banquero;
Auditor;
COEND
END.
Cuarto Intento:
PROGRAM bancocuartointento;
CONST NCTAS=20;
VAR Cuenta : array[1..NCTAS] of integer; (* Almacena los saldos *)
i: Integer;
pizarra1,pizarra2 : boolean;
PROCESS Auditor;
VAR k,ci, Total:INTEGER;
BEGIN
{REPEAT}
pizarra1 := true;
while pizarra2 = true do;
Total:=0;
FOR
k:=1 TO Nctas do
Cuenta[k] := 100;
FOR ci:=1 TO NCTAS DO
Total:= Total + Cuenta[ci];
Writeln('Total : ', Total);
pizarra1 := false;
{FOREVER}
END;
PROCESS banquero;
VAR c1, c2, cant: Integer;
BEGIN
{REPEAT}
pizarra2 := true;
while pizarra1 = true do;
c1 := RANDOM(NCTAS-1)+1; (* Origen *)
c2 := RANDOM(NCTAS-1)+1; (* Destino *)
cant:=0;
IF (Cuenta[c1]>10) THEN cant := RANDOM(10);
Cuenta[c1] := Cuenta[c1] - cant ;
Cuenta[c2] := Cuenta[c2] + cant ;
writeln('Operacion realizada con exito');
pizarra2 := false;
{FOREVER}
END;
BEGIN (* Principal *)
pizarra1 := false;
pizarra2 := false;
For i:= 1 TO NCTAS DO cuenta[i]:=100;
COBEGIN
Banquero;
Auditor;
COEND
END.
Quinto Intento:
PROGRAM bancoquintointento;
CONST NCTAS=20;
VAR Cuenta : array[1..NCTAS] of integer; (* Almacena los saldos *)
total,turno,i: integer;
pizarra1,pizarra2 : boolean;
PROCESS Auditor;
VAR ci, Total:INTEGER;
BEGIN
REPEAT
pizarra1 := true;
WHILE pizarra2 = true DO
IF turno = 2 THEN
BEGIN
(* tratamiento de cortesa *)
pizarra1 := false;
WHILE turno = 2 DO; (* date una vuelta *)
pizarra1 := true;
END ;
Total:=0;
FOR ci:=1 TO NCTAS DO
Total:= Total + Cuenta[ci];
Writeln('Total : ', Total);
turno := 2;
pizarra1 := false;
FOREVER
END;
PROCESS banquero;
VAR c1, c2, cant: Integer;
BEGIN
REPEAT
pizarra2 := true;
WHILE pizarra1 = true DO
IF turno = 1 THEN
BEGIN
(* tratamiento de cortesa *)
pizarra2 := false;
WHILE turno = 1 DO; (* date una vuelta *)
Pizarra2 := true;
END ;
c1 := RANDOM(NCTAS-1)+1; (* Origen *)
c2 := RANDOM(NCTAS-1)+1; (* Destino *)
cant:=0;
IF (Cuenta[c1]>10) THEN cant := RANDOM(10);
Cuenta[c1] := Cuenta[c1] - cant ;
Cuenta[c2] := Cuenta[c2] + cant ;
turno := 1;
pizarra2 := false;
FOREVER
END;
BEGIN (* Principal *)
pizarra1 := false;
pizarra2 := false;
turno := 1;
For i:= 1 TO NCTAS DO cuenta[i]:=100;
COBEGIN
Banquero;
Auditor
COEND
END.
Solucin 1:
Aplicaremos el Algoritmo de Peterson con sincronizacin y exclusin mutua:
PROGRAM problema5;
CONST NCTAS=20;
kRunTime = 120;
kSleepTime = 10;
VAR Cuenta:array[1..NCTAS] of integer;
i: Integer;
finished : boolean;
flag1 : boolean;
flag2 : boolean;
turn : integer;
c1:=RANDOM(NCTAS-1)+1;
c2:=RANDOM(NCTAS-1)+1;
Writeln('Cuentas Aleatorias',c1,' y',c2);
cant:=0;
IF(Cuenta[c1]>10) THEN cant:=RANDOM(10);
Cuenta[c1]:=Cuenta[c1] - cant;
Writeln('Nuevo Monto para Cuenta',c1,' es:',Cuenta[c1]);
Cuenta[c2]:=Cuenta[c2] - cant;
Writeln('Nuevo Monto para Cuenta',c2,' es:',Cuenta[c2]);
flag2 := false;
end;
PROCESS ContarSaldos;
VAR ci, Total: INTEGER;
BEGIN
while not finished do
begin
flag1 := true;
turn := 2;
while (flag2 and (turn = 2)) do
begin
null;
end;
Total:=0;
FOR ci:=1 TO NCTAS DO
Total:=Total + Cuenta[ci];
Writeln('Total del Banco:',Total);
flag1 := false;
END;
PROCESS AUDITOR;
begin
while not finished do
begin
sleep(random(kSleepTime));
writeln('Un Momento...');
end;
end;
process supervisor;
begin
sleep(kRunTime);
finished := true;
end;
end;
END;
PROCESS Operacion;
VAR c1, c2, cant: Integer;
BEGIN
while not finished do
begin
flag2 := true;
turn := 1;
while (flag1 and (turn = 1)) do
begin
null;
end;
(*MAIN*)
BEGIN
(*INICIAMOS LOS MONTOS DE 100 SOLES PARA
CADA CUENTA*)
For i:=1 TO NCTAS DO Cuenta[i]:=100;
finished := false;
flag1 := false;
flag2 := false;
turn := 1;
(*EMPEZAMOS LOS PROCESOS*)
COBEGIN
supervisor;
auditor;
Operacion;
ContarSaldos;
COEND
sleep(1);
END.
10
5 es:
10 es:
95
95
5
3 es:
5 es:
99
94
14
8 es:
14 es:
90
90
Solucin 2:
Otro aplicando otro algoritmo de exclusin mutua de Dekker:
PROGRAM banco;
CONST NCTAS=20;
VAR Cuenta:array[1..NCTAS] of integer;
i: Integer;
finished : boolean;
flag1 : boolean;
flag2 : boolean;
PROCESS ContarSaldos;
VAR ci, Total: INTEGER;
BEGIN
while not finished do
begin
sleep(1);
while flag1 do
begin
null;
end;
flag2 := true;
Total:=0;
FOR ci:=1 TO NCTAS DO
Total:=Total + Cuenta[ci];
Writeln('Total del Banco:',Total);
flag2 := false;
end;
END;
PROCESS Operacion;
VAR c1, c2, cant: Integer;
BEGIN
while not finished do
begin
while flag2 do
begin
null;
end;
(* flag1 := true; *)
c1:=RANDOM(NCTAS-1)+1;
c2:=RANDOM(NCTAS-1)+1;
Writeln('Cuentas Aleatorias',c1,' y',c2);
cant:=0;
IF(Cuenta[c1]>10) THEN cant:=RANDOM(10);
Cuenta[c1]:=Cuenta[c1] - cant;
Writeln('Nuevo Monto para Cuenta',c1,' es:',Cuenta[c1]);
Cuenta[c2]:=Cuenta[c2] + cant;
Writeln('Nuevo Monto para Cuenta',c2,' es:',Cuenta[c2]);
(* flag1 := false; *)
end;
END;
(*MAIN*)
BEGIN
(*INICIAMOS LOS MONTOS DE 100 SOLES PARA CADA CUENTA*)
For i:=1 TO NCTAS DO Cuenta[i]:=100;
finished := false;
flag1 := false;
flag2 := false;
(*EMPEZAMOS LOS PROCESOS*)
COBEGIN
Operacion;
ContarSaldos;
COEND
END.