Esteu aquí: Inici Assignatures Programació Concurrent i en Temps Real Erlang: Exemples ping_pong2.erl

ping_pong2.erl

ping_pong2.erl — Erlang source code, 983 bytes

Continguts del fitxer

-module(ping_pong2).
-export([inici/0, inici/2, ping/3, pong/1]).

inici() ->
    inici(3, 3).

inici(Max, NumPing) -> 
    Pong = spawn(ping_pong2, pong, [NumPing]),
    F = fun(I) -> 
                Ping = spawn(ping_pong2, ping, [I, Pong, Max]),
				io:format("Inici del proc�s ~p~n",[I]),
                Ping ! pong
        end,
    lists:foreach ( F, lists:seq(1, NumPing) ).

ping(I, Pong, Max) ->
    receive
        pong when Max > 0 ->
            io:format("Proc�s Ping ~p ~n", [I]),
            Pong ! {self(), ping, I},
            ping(I, Pong, Max -1);
        _ ->
            Pong ! finalitzar,
            io:format("Fi Ping ~p~n", [I])
    end.

pong(NumPing) ->
    receive
        {Ping, ping, I} ->
            io:format("Proc�s Pong ~p ~n", [I]),
            Ping ! pong,
            pong(NumPing);
        finalitzar when NumPing > 1 ->
            pong(NumPing-1);
        _ ->
            io:format("Fi Pong~n")
    end.