Advertisement
PaweU

erlang

Nov 26th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. -module(test).
  2. -compile([export_all]).
  3.  
  4. produkuj(0,Posrednik) -> Posrednik ! {-1, liczba};
  5. produkuj(N, Posrednik) ->
  6. Liczba = rand:uniform(100),
  7. Posrednik ! {Liczba, liczba},
  8. % io:format("Producent wytworzyl: ~p\n", [Liczba]),
  9. produkuj(N-1, Posrednik).
  10.  
  11. producent(N) ->
  12. Posrednik = spawn(?MODULE, posrednik, [N]),
  13. produkuj(N, Posrednik),
  14. io:format("Producent zakonczyl dzialanie.\n").
  15.  
  16. posrednicz(Konsument, BufSize, []) ->
  17. receive
  18. {Liczba, liczba} -> posrednicz(Konsument, BufSize, [Liczba])
  19. end;
  20.  
  21. posrednicz(Konsument, BufSize, Bufor) ->
  22. io:format(" Nastepuje zmiana bufora: ~w\n", [Bufor]),
  23. receive
  24. {Liczba, liczba} when length(Bufor) < BufSize -> posrednicz(Konsument, BufSize, lists:reverse([Liczba | lists:reverse(Bufor)]));
  25. {Konsument, pobierzLiczbe} ->
  26. [H|T] = Bufor,
  27. Konsument ! {H, liczba}, posrednicz(Konsument, BufSize, T)
  28. end.
  29.  
  30. posrednik(N)->
  31. Konsument = spawn(?MODULE, konsument, [self()]),
  32. posrednicz(Konsument, N/2, []).
  33.  
  34. konsument(Posrednik) ->
  35. Posrednik ! {self(), pobierzLiczbe},
  36. receive
  37. {-1, liczba} -> io:format("-1, czyli koniec potoku.\n");
  38. {Liczba, liczba} -> io:format("Konsument otrzymal: ~p\n", [Liczba]), konsument(Posrednik)
  39. end.
  40.  
  41. main(N) ->
  42. producent(N).
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement