Uni v er si dadNaci onalJor geBasadr eGr ohmann Fac ul t addeI ng e ni e r í a
Pr ac t i c adel abor at or i oN° 5 Es t udi ant e:
Ke vi nMi keHer r er aVega
Códi go:
201236144
Facul t ad:
Fac ul t addeI ng e ni e r í a
Escuel apr of esi onal :
I ngeni erí aenI nf ormá mát i caySi st ema mas
Asi gnat ur a:
Labo r at o r i odeAl g or i t mo sPar al e l o s
Nomb m br edel apráct i ca: Al gori t modeDekkeroPet er son Fecha:
21/06/2013
Hor a:
6pm –8pm
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Tacna–2013 Practica 05-Algoritmos paralelos Alumno: Kevin Mike Herrera Vega
Código: 2012-36144
Realice Realice un program programaa concur concurren rente te con uso de sincro sincroni niaci! aci!n n ! varia" varia"le le compart compartida ida #algoritmo de $ekker o %eterson&' (ue simula la actividad del "anco' en la cual las dos operaciones son realiadas por procesos di)erentes* +uponga (ue el proceso se repite in)initamente* ,l programa concurrente se de"e realiar en %ascal.* Planteamiento del problema
,l /anco* +upongamos un "anco (ue tiene 20 cuentas' numeradas del 1 al 20' cada cuenta tiene' inicialmente' +* 100 de saldo* %or lo tanto' el "anco tiene +* 2 000* ,l "anco realia dos operaciones: 1* .onta"ili .onta"iliaa los saldos de todas todas las cuentas cuentas #cada minuto&* minuto&* 2* raspaso raspasoss de una cuenta a otra #cuentas #cuentas elegidas aleatoriame aleatoriamente nte el saldo aleatorio entre 0 10&* ACTIVIDADES 1* plicar plicar uno a uno uno los intent intentos os de eclusion eclusion mutua mutua sincroni sincroniacion acion indicado indicadoss en la teor5a* 2* naliar naliar las dos soluciones soluciones propuestas propuestas a continua continuaci!n* ci!n*
%rimer ntento: PROGRAM bancoprimerintento; bancoprimerintento; NCTAS=20; CONST NCTAS=20; CONST VAR Cuenta array[1..NCTAS] integer; Cuenta : array [1..NCTAS] of integer ; (* Almacena los salos *! i: "nte#er; integer; Turno : integer ; Auitor; PROCESS Auitor; PROCESS VAR $%ci% $%ci% Total:INTEGER Total: INTEGER ; BEGIN &')AT while Turno while do; Turno = 2 do ; Total:=0; FOR TO Nctas $:=1 TO Nctas do Cuenta[$] := 100; FOR ci:=1 TO NCTAS ci:=1 TO NCTAS DO
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
PROCESS banuero; PROCESS banuero; VAR c1% c1% c2% cant: "nte#er; BEGIN &')AT while Turno while do; Turno = 1 do ; c1 := 'AN/3(NCTAS41!+1; (* /ri#en *! c2 := 'AN/3(NCTAS41!+1; (* estino *! cant:=0; IF (Cuenta[c1]510! IF THEN cant (Cuenta[c1]510! THEN cant := 'AN/3(10!; Cuenta[c1] := Cuenta[c1] 4 cant ; Cuenta[c2] := Cuenta[c2] + cant ; 6riteln(-/peracion 6riteln( -/peracion reali7aa con e8ito-!; e8ito- !; Turno := 1; &/'' END; END ; BEGIN (* )rincipal *! Turno := 1; TO NCTAS or i:= 1 TO NCTAS DO cuenta[i]:=100; cuenta[i]:=100; COBEGIN 9anuero; Auitor; COEND END. END .
+egundo ntento: PROGRAM bancose#unointento; CONST NCTAS=20; CONST NCTAS=20; Cuenta : array [1..NCTAS] of integer ; (* Almacena los salos *! VAR Cuenta array[1..NCTAS] integer; integer; i: integer ; pi7arra1 : integer ; integer ; integer ; pi7arra2 : integer ; &1 : pescao 2: no pescano porseaca PROCESS Auitor; PROCESS Auitor; VAR $%ci% $%ci% Total:INTEGER Total: INTEGER ; BEGIN REPEAT while pi7arra2 while do; pi7arra2 = 1 do ; pi7arra1:=1; Total:=0; $:=1 TO Nctas do FOR TO Nctas Cuenta[$] := 100; ci:=1 TO NCTAS DO FOR ci:=1 TO NCTAS Total:= Total + Cuenta[ci]; ,riteln(-Total ,riteln( -Total : -% -% Total!; pi7arra1 := 2; FOREVER END; END; PROCESS banuero; PROCESS banuero; VAR c1% c1% c2% cant: "nte#er; BEGIN REPEAT while pi7arra1 while do; pi7arra1 = 1 do ; pi7arra1:= 1; c1 := 'AN/3(NCTAS41!+1; (* /ri#en *!
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
pi7arra2:= 2; FOREVER END; END; BEGIN (* )rincipal *! pi7arra1:= 2; pi7arra2:=2; TO NCTAS or i:= 1 TO NCTAS DO cuenta[i]:=100; cuenta[i]:=100; COBEGIN 9anuero; Auitor; COEND . END. END
ercer ercer ntento: bancotercerintento; PROGRAM bancotercerintento; CONST NCTAS=20; CONST NCTAS=20; Cuenta : array [1..NCTAS] of integer ; (* Almacena los salos *! VAR Cuenta array[1..NCTAS] integer; i: "nte#er; oolean; pi7arra1%pi7arra2: oolean ; PROCESS Auitor; PROCESS Auitor; VAR $%ci% $%ci% Total:INTEGER Total: INTEGER ; BEGIN &')AT pi7arra2 = tr!e do ; while pi7arra2 while do; tr!e; pi7arra1 := tr!e ; Total:=0; Nctas do FOR $:=1 TO TO Nctas Cuenta[$] := 100; FOR ci:=1 TO NCTAS ci:=1 TO NCTAS DO Total:= Total + Cuenta[ci]; ,riteln(-Total ,riteln( -Total : -% - % Total!; fal"e; pi7arra1 := fal"e ; &/'' END; END ; PROCESS banuero; PROCESS banuero; VAR c1% c1% c2% cant: "nte#er; BEGIN &')AT while pi7arra1 while do; pi7arra1 = tr!e do ; tr!e; pi7arra2 := tr!e ; c1 := 'AN/3(NCTAS41!+1; (* /ri#en *! c2 := 'AN/3(NCTAS41!+1; (* estino *! cant:=0; IF (Cuenta[c1]510! THEN cant (Cuenta[c1]510! THEN cant := 'AN/3(10!; Cuenta[c1] := Cuenta[c1] 4 cant ; Cuenta[c2] := Cuenta[c2] + cant ; 6riteln(-/peracion 6riteln( -/peracion reali7aa con e8ito- !; fal"e; pi7arra2 := fal"e ; &/'' END; END ; BEGIN (* )rincipal *! fal"e; pi7arra1 := fal"e ; fal"e; fal"e pi7arra2 := ;
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
. END. END
.uarto ntento: PROGRAM bancocuartointento; CONST NCTAS=20; CONST NCTAS=20; VAR Cuenta array[1..NCTAS] integer; Cuenta : array [1..NCTAS] of integer ; (* Almacena los salos *! i: "nte#er; oolean; pi7arra1%pi7arra2 pi7arra1%pi7arra2 : oolean ; PROCESS Auitor; PROCESS Auitor; VAR $%ci% $%ci% Total:INTEGER Total: INTEGER ; ; BEGIN &')AT pi7arra1 := tr!e ; tr!e; while pi7arra2 while do; pi7arra2 = tr!e do ; Total:=0; FOR TO Nctas $:=1 TO Nctas do Cuenta[$] := 100; FOR ci:=1 TO NCTAS ci:=1 TO NCTAS DO Total:= Total + Cuenta[ci]; ,riteln(-Total ,riteln( -Total : -% -% Total!; fal"e; pi7arra1 := fal"e ; &/'' ; END; END PROCESS banuero; PROCESS banuero; c1% c2% cant: "nte#er; VAR c1% BEGIN &')AT tr!e; pi7arra2 := tr!e ; while pi7arra1 while do; pi7arra1 = tr!e do ; c1 := 'AN/3(NCTAS41!+1; (* /ri#en *! c2 := 'AN/3(NCTAS41!+1; (* estino *! cant:=0; IF (Cuenta[c1]510! IF THEN cant (Cuenta[c1]510! THEN cant := 'AN/3(10!; Cuenta[c1] := Cuenta[c1] 4 cant ; Cuenta[c2] := Cuenta[c2] + cant ; 6riteln(-/peracion 6riteln( -/peracion reali7aa con e8ito-!; e8ito- !; fal"e; pi7arra2 := fal"e ; &/'' END; END ; BEGIN (* )rincipal *! fal"e; pi7arra1 := fal"e ; fal"e; pi7arra2 := fal"e ; TO NCTAS or i:= 1 TO NCTAS DO cuenta[i]:=100; cuenta[i]:=100; COBEGIN 9anuero; Auitor; COEND . END. END
7uinto ntento: PROGRAM bancouintointento; CONST NCTAS=20; CONST NCTAS=20; VAR Cuenta Cuenta : [1..NCTAS] of integer
(* Almacena los salos *!
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
REPEAT tr!e; pi7arra1 := tr!e ; pi7arra2 = tr!e DO #HI$E pi7arra2 #HI$E IF turno IF turno = 2 THEN BEGIN (* tratamiento e cortes
PROCESS ContarSaldos; VAR 'i( Total: "NT)*)R; +)*"N ,$ile not ini#$e% %o &e!in la!1 := true; turn := 2; ,$ile -la!2 an% -turn = 2 %o &e!in null; en%; Total:=0; /OR 'i:=1 TO NCTAS O Total:=Total Cuenta['i]; riteln-3Total riteln-3Total %el +an'o:3(Total; +an'o:3(Total; la!1 := al#e;
)N;
PROCESS AUDITOR; &e!in ,$ile not ini#$e% %o &e!in sleep(random(SleepTime!!; ,riteln-39n 4omento...3; en%; en%;
process s"per#isor; &e!in sleep(R"nTime!; ini#$e% := true; en%;
en%; )N;
PROCESS Operacion; VAR '1( '2( 'ant: "nte!er; +)*"N ,$ile not ini#$e% %o &e!in la!2 := true; turn := 1; ,$ile -la!1 an% -turn = 1 %o &e!in
-4A"N $EGI% -"N"C -"N"C"A4 "A4OS OS OS 4ONTOS 4ONTOS ) 100 SO)S
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
,n c!digo mostrado' se toman en cuenta dos operaciones: !peración (ue son la operaci!n (ue se realia entre dos cuenta aleatorias ContarSaldos (ue es la cuenta recursiva cada minuto del proceso de saldo del "anco* 8e proceso de supervisor auditor son patrones tomados del algoritmo de %eterson* +e muestra a continuaci!n los resultados del programa: 5 "nterpreter Ver#ion <>.? 5
Cuenta# Aleatoria# >y Nue8o 4onto para Cuenta Nue8o 4onto para Cuenta Total Total %el +an'o: 10 Cuenta# Aleatoria# ?y Nue8o 4onto para Cuenta Nue8o 4onto para Cuenta Total Total %el +an'o: 1 Cuenta# Aleatoria# y Nue8o 4onto para Cuenta Nue8o 4onto para Cuenta
10 > e#: 10 e#:
> >
> ? e#: > e#:
B
1B e#: 1B e#:
0 0
Solución " 9tro aplicando otro algoritmo de eclusi!n mutua de $ekker:
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
The world’s largest digital library
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
-4A"N +)*"N -"N"C"A4OS OS 4ONTOS ) 100 SO) S