Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(test).
- -compile([export_all]).
- produkuj(0,Posrednik) -> Posrednik ! {-1, liczba};
- produkuj(N, Posrednik) ->
- Liczba = rand:uniform(100),
- Posrednik ! {Liczba, liczba},
- % io:format("Producent wytworzyl: ~p\n", [Liczba]),
- produkuj(N-1, Posrednik).
- producent(N) ->
- Posrednik = spawn(?MODULE, posrednik, [N]),
- produkuj(N, Posrednik),
- io:format("Producent zakonczyl dzialanie.\n").
- posrednicz(Konsument, BufSize, []) ->
- receive
- {Liczba, liczba} -> posrednicz(Konsument, BufSize, [Liczba])
- end;
- posrednicz(Konsument, BufSize, Bufor) ->
- io:format(" Nastepuje zmiana bufora: ~w\n", [Bufor]),
- receive
- {Liczba, liczba} when length(Bufor) < BufSize -> posrednicz(Konsument, BufSize, lists:reverse([Liczba | lists:reverse(Bufor)]));
- {Konsument, pobierzLiczbe} ->
- [H|T] = Bufor,
- Konsument ! {H, liczba}, posrednicz(Konsument, BufSize, T)
- end.
- posrednik(N)->
- Konsument = spawn(?MODULE, konsument, [self()]),
- posrednicz(Konsument, N/2, []).
- konsument(Posrednik) ->
- Posrednik ! {self(), pobierzLiczbe},
- receive
- {-1, liczba} -> io:format("-1, czyli koniec potoku.\n");
- {Liczba, liczba} -> io:format("Konsument otrzymal: ~p\n", [Liczba]), konsument(Posrednik)
- end.
- main(N) ->
- producent(N).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement