Advertisement
tomasfdel

SO Práctica 3

May 8th, 2018
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 7.66 KB | None | 0 0
  1. TO DO:
  2.     - Pedirle el Ej 2 a Giannini.
  3.  
  4.  
  5. //Ejercicio 1
  6. -module(pepe).
  7. -export([init/0]).
  8.  
  9. match_test () ->
  10.   {A,B} = {5,4},
  11.   %% Esta linea se rompe porque no puede hacer matchear C a dos valores distintos.
  12.   %%{C,C} = {5,4},
  13.   {B,A} = {4,5},
  14.   {D,D} = {5,5}.
  15.  
  16. apellido ({persona, {nombre, _}, {apellido, A}}) -> A.
  17. nombre ({persona, {nombre, N}, {apellido, _}}) -> N.
  18.  
  19.  
  20. tuple_test (P1,P2) ->
  21.   io:format("El nombre de P1 es ~p y el apellido de P2 es ~p~n",[nombre(P1),apellido(P2)]).
  22.  
  23.  
  24. string_test () -> [
  25.   helloworld == 'helloworld', %% Este es True porque son dos maneras de escribir el mismo atomo.
  26.   "helloworld" < 'helloworld', %% Este es False y no entendemos por que.
  27.   helloworld == "helloworld", %% Este es False porque uno es un atomo y el otro, un string.
  28.   [$h,$e,$l,$l,$o,$w,$o,$r,$l,$d] == "helloworld", %% Este es True porque son dos maneras de escribir el mismo string.
  29.   [104,101,108,108,111,119,111,114,108,100] < {104,101,108,108,111,119,111,114,108,100}, %% Este es False y no entendemos por que.
  30.   [104,101,108,108,111,119,111,114,108,100] > 1, %% Este es True y no entendemos por que.
  31.   [104,101,108,108,111,119,111,114,108,100] == "helloworld"]. %% Este es True porque cada caracter corresponde con su codigo.
  32.  
  33. filtrar_por_apellido(Personas,Apellido) -> [nombre(X) || X <- Personas, apellido(X) == Apellido].
  34.  
  35. init () ->
  36.     P1 = {persona,{nombre,"Juan"},{apellido, "Gomez"}},
  37.     P2 = {persona,{nombre,"Carlos"},{apellido, "Garcia"}},
  38.     P3 = {persona,{nombre,"Javier"},{apellido, "Garcia"}},
  39.     P4 = {persona,{nombre,"Rolando"},{apellido, "Garcia"}},
  40.     match_test(),
  41.     tuple_test(P1, P2),
  42.     string_test(),
  43.     Garcias = filtrar_por_apellido([P4,P3,P2,P1],"Garcia"),
  44.     io:format("~p ~n~p ~n~p ~n", Garcias),
  45.     io:format("Todo esta bien ~n").
  46.  
  47.  
  48.  
  49. // Ejercicio 3
  50. -module(eco).
  51. -compile(export_all).
  52.                          
  53. server() ->
  54.     {ok, ListenSocket} = gen_tcp:listen(8000, [{active, false}]),
  55.     wait_connect(ListenSocket, 1).
  56.  
  57. wait_connect(ListenSocket, N) ->
  58.     {ok, Socket} = gen_tcp:accept(ListenSocket),
  59.     spawn(?MODULE, wait_connect, [ListenSocket, N+1]),
  60.     get_request(Socket, N).
  61.  
  62. get_request(Socket, N) ->   {ok, Msg} = gen_tcp:recv(Socket, 0),
  63.                             gen_tcp:send(Socket, Msg),
  64.                             gen_tcp:close(Socket).
  65.  
  66.  
  67.  
  68.  
  69.  
  70. // Ejercicio 4
  71. %%Version original
  72. -module(ej4).
  73. -compile(export_all).
  74. -define(N_Procesos, 7).
  75. -define(N_Inicial, 10).
  76. proceso(IdHermano) -> receive   {id, Pid} -> proceso(Pid);
  77.                                 {msg, 0} -> IdHermano ! {msg_exit}, io:format("RIP in Process ~p~n", [self()]), ok;
  78.                                 {msg, N} -> io:format("Soy el proceso ~p y le mando N = ~p al proceso ~p~n",[self(), N - 1, IdHermano]), IdHermano ! {msg, N - 1}, proceso(IdHermano);
  79.                                 {msg_exit} -> IdHermano ! {msg_exit}, io:format("RIP in Process ~p~n", [self()]), ok end.
  80.  
  81. iniciarProcesos(ListaDePids, Actual, Total) when (Actual > Total) -> lists:nth(1, ListaDePids) ! {msg, ?N_Inicial}, ok;
  82. iniciarProcesos(ListaDePids, Actual, Total) -> lists:nth(Actual, ListaDePids) ! {id, lists:nth( (Actual rem ?N_Procesos) + 1, ListaDePids)}, iniciarProcesos(ListaDePids, Actual + 1, Total).
  83.  
  84. init() ->   IDs = [spawn(?MODULE, proceso, [0]) || _ <- lists:seq(1, ?N_Procesos, 1)],
  85.             iniciarProcesos(IDs, 1, ?N_Procesos).
  86.  
  87.  
  88. %%Modificación
  89. -module(ej4).
  90. -compile(export_all).
  91. -define(N_Procesos, 50).
  92.  
  93. proceso(IdHermano, Repetir) ->
  94.     receive {id, Pid} -> proceso(Pid, Repetir);
  95.             {msg} -> case Repetir of
  96.                         true  ->    io:format("RIP in Process ~p~n", [self()]),
  97.                                     IdHermano ! {msg_exit},
  98.                                     ok;
  99.                         false ->    io:format("Soy el proceso ~p y le mando un mensaje al proceso ~p~n",[self(), IdHermano]),
  100.                                     IdHermano ! {msg},
  101.                                     proceso(IdHermano, true)
  102.                         end;
  103.             {msg_exit} -> IdHermano ! {msg_exit}, io:format("RIP in Process ~p~n", [self()]), ok end.
  104.  
  105. iniciarProcesos(ListaDePids, Actual, Total) when (Actual > Total) -> lists:nth(1, ListaDePids) ! {msg}, ok;
  106. iniciarProcesos(ListaDePids, Actual, Total) -> lists:nth(Actual, ListaDePids) ! {id, lists:nth( (Actual rem ?N_Procesos) + 1, ListaDePids)}, iniciarProcesos(ListaDePids, Actual + 1, Total).
  107.  
  108. init() ->   IDs = [spawn(?MODULE, proceso, [0, false]) || _ <- lists:seq(1, ?N_Procesos, 1)],
  109.             iniciarProcesos(IDs, 1, ?N_Procesos).
  110.  
  111. //EJERCICIO 6 SUGOIIIII
  112. -module(ej6).
  113. %~ -export([testLock/0,testsenpai/0]).
  114. -compile(export_all).
  115.  
  116. %internal
  117. %~ -export([f/2,waiter/2]).
  118. %~ -export([waiter_senpai/2,senpai/2]).
  119.  
  120.  
  121. mutex () -> receive {lock, Pid} -> Pid!ok,
  122.                 receive {unlock, Pid} -> ok end, mutex();
  123.                     {destroyLock, Pid} -> ok end.
  124.  
  125.  
  126. lock (L) -> L!{lock, self()}, receive ok -> ok end.
  127. unlock (L) -> L!{unlock, self()}, ok.
  128. createLock () -> spawn(?MODULE, mutex, []).
  129. destroyLock (L) -> L!{destroyLock, self()}.
  130.  
  131.  
  132. f (L,W) -> lock(L),
  133.   %   regioncritica(),
  134.   io:format("uno ~p~n",[self()]),
  135.   io:format("dos ~p~n",[self()]),
  136.   io:format("tre ~p~n",[self()]),
  137.   io:format("cua ~p~n",[self()]),
  138.   unlock(L),
  139.   W!finished.
  140.  
  141.  
  142. waiter (L,0)  -> io:format("Destruyendo Lock~n"), destroyLock(L);
  143. waiter (L,N)  -> receive finished -> waiter(L,N-1) end.
  144.  
  145.  
  146. testLock () -> L = createLock(),
  147.   W=spawn(?MODULE,waiter,[L,3]),
  148.   spawn(?MODULE,f,[L,W]),
  149.   spawn(?MODULE,f,[L,W]),
  150.   spawn(?MODULE,f,[L,W]),
  151.   ok.
  152.  
  153. %~ #noweeb
  154. semaphore (N, L) when N>0 ->
  155.         receive {senpaiP, Pid} ->   Pid!ok,
  156.                                     semaphore(N-1, [Pid | L]);
  157.                 {senpaiV, Pid2} ->
  158.                     case lists:member(Pid2, L) of
  159.                         true  -> semaphore(N+1, lists:delete(Pid2, L));
  160.                         false -> semaphore(N,L)
  161.                     end;
  162.                 {destroysenpai, Pid3} -> ok end;
  163. semaphore (N, L) ->
  164.         receive {senpaiV, Pid2} ->
  165.                     case lists:member(Pid2, L) of
  166.                         true  -> semaphore(N+1, lists:delete(Pid2, L));
  167.                         false -> semaphore(N,L)
  168.                     end;
  169.                 {destroysenpai, Pid3} -> ok end.
  170.  
  171. createsenpai (N) -> spawn(?MODULE, semaphore, [N, []]).
  172. destroysenpai (S) -> io:format("S-senpai, I don't feel so good... UwU~n"), S!{destroysenpai, self()}, ok.
  173. senpaiP (S) -> S!{senpaiP, self()}, receive ok -> ok end.
  174. senpaiV (S) -> S!{senpaiV, self()}, ok.
  175.  
  176.  
  177. senpai (S,W) ->
  178.   senpaiP(S),
  179.   %regioncritica(), bueno, casi....
  180.   io:format("uno ~p~n",[self()]),
  181.   io:format("dos ~p~n",[self()]),
  182.   io:format("tre ~p~n",[self()]),
  183.   io:format("cua ~p~n",[self()]),
  184.   senpaiV(S),
  185.   W!finished.
  186.  
  187. waiter_senpai (S,0)  -> destroysenpai(S);
  188. waiter_senpai (S,N)  -> receive finished -> waiter_senpai(S,N-1) end.
  189.  
  190.  
  191. testsenpai () -> S = createsenpai(1), % a lo sumo dos usando io al mismo tiempo
  192.   W=spawn(?MODULE,waiter_senpai,[S,5]),
  193.   spawn(?MODULE,senpai,[S,W]),
  194.   spawn(?MODULE,senpai,[S,W]),
  195.   spawn(?MODULE,senpai,[S,W]),
  196.   spawn(?MODULE,senpai,[S,W]),
  197.   spawn(?MODULE,senpai,[S,W]),
  198.   '=w= s-senpai...'.
  199.  
  200.  
  201.  
  202.  
  203. // Ejercicio 7
  204. -module(ej7).
  205. -compile(export_all).
  206.  
  207. launch() -> process_flag(trap_exit, true),
  208.             H = spawn_link(?MODULE, hello, []),
  209.             receive {'EXIT', _, _} -> launch() end.
  210.  
  211. hello() ->
  212.    
  213.     {A1,A2,A3} = now(),
  214.     random:seed(A1,A2,A3),
  215.     helloloop().
  216.  
  217. helloloop() ->
  218.     receive
  219.         after 1000 -> ok
  220.     end,
  221.     io:format("Hello ~p~n",
  222.         [case random:uniform(10) of 10 -> 1/uno; _ -> self() end]),
  223.     helloloop().
  224.  
  225. init() -> spawn(?MODULE, launch, []).
  226.  
  227.  
  228.  
  229.  
  230. // Ejercicio 8
  231. -module(ej8).
  232. -compile(export_all).
  233.  
  234. launch() -> process_flag(trap_exit, true),
  235.             H = spawn_link(?MODULE, hello, []),
  236.             receive {'EXIT', _, _} -> launch() end.
  237.  
  238. hello() ->
  239.    
  240.     {A1,A2,A3} = now(),
  241.     random:seed(A1,A2,A3),
  242.     helloloop().
  243.  
  244. helloloop() ->
  245.     receive
  246.         after 1000 -> ok
  247.     end,
  248.     io:format("Hellori ~p~n",
  249.         [case random:uniform(10) of 10 -> 1/uno; _ -> self() end]),
  250.     ?MODULE:helloloop().
  251.  
  252. init() -> spawn(?MODULE, launch, []).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement