Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(consumerproducer).
- -export([start/0]).
- producer() ->
- receive
- {Pid, produce} ->
- Pid ! produced,
- producer();
- terminate -> ok
- end.
- consumer() ->
- receive
- {Pid, consume} ->
- Pid ! consumed,
- consumer();
- terminate -> ok
- end.
- server(Count, Producer, Consumer) ->
- io:format("Count: ~w~n", [Count]),
- receive
- produced ->
- Consumer ! {self(), consume},
- server(Count + 1, Producer, Consumer);
- consumed ->
- Producer ! {self(), produce},
- server(Count - 1,Producer, Consumer)
- end.
- start_producer(1, Pid) -> Pid ! {self(), produce};
- start_producer(N, Pid) ->
- Pid ! {self(), produce},
- start_producer(N - 1, Pid).
- start() ->
- ProdPid = spawn(fun producer/0),
- ConsPid = spawn(fun consumer/0),
- start_producer(100, ProdPid),
- server(0, ProdPid, ConsPid).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement