0
votes

I am trying to make a master process here which willspawn the 5 processes and then these process will make a call to the people in the list. The text file is as follows :

{john, [jill,joe,bob]}. {jill, [bob,joe,bob]}. {sue, [jill,jill,jill,bob,jill]}. {bob, [john]}. {joe, [sue]}.

Blockquote

in it john is a process and will send a message to Jill, joe and bob. Same with the other processes. So far I was able to spawn the process and get the elements from the sublist but I donot know how to send the messages from processes to the sublists .I am a beginner in erlang and I am facing too much problem in it I have read a lot about the processes and concurrency and from many resources but this is way too much for me .the required output looks like this :

bob received intro message from jill [738000] joe received intro message from john [741004] bob received intro message from john [770008] joe received intro message from jill [779007] john received intro message from bob [736102] john received reply message from joe [741004] john received reply message from bob [770008] jill received intro message from sue [737001] bob received intro message from jill [816004] bob received reply message from john [736102] bob received intro message from sue [897005] jill received intro message from john [739000]

The time stamp of message sent and received should match . any help would be appreciated a code snippet performing the message sending and receiving would be highly appreciated . Thank you .

-module(exchange). -import(lists,[nth/2]). -export([start/0,for/2,for1/2,process/0]).

start()->
A=file:consult("calls.txt"),
T=element(2,A),

   L=length(T),
   %io:fwrite("~w",[L]),
   for(L,T).


for(0,_)->
   [];
   for(L,Term) when L > 0 ->

   S=nth(L,Term),
   S1=element(1,S),
   io:fwrite(" ~w~n",[S1]),
   Q=element(2,S),
   R=length(Q),
   B=nth(1,Q),
   for1 (R,Q),
   Pid=spawn(exchange,process,[]),
   register(S1,Pid),
   S1 ! {sender, S1,R,Q},

   for(L-1,Term).


for1(0,_)->
[];
for1(L1,Term1) when L1 >0 ->
S2=nth(L1,Term1),
  process()->
   receive
   {sender, S1,R,Q}->
    io:fwrite("~w received intro message from ",[S1]),
    process()
  end;
for1 (L1-1,Term1).
1

1 Answers

2
votes

There are a couple of changes you could make to make the code clearer, and hopefully that would make it easier to find the problem. Normally you'd use the function element very rarely, preferring pattern matching instead. So change this:

A=file:consult("calls.txt"),
T=element(2,A),

to:

{ok, T} = file:consult("calls.txt"),

This has the advantage that if file:consult returns an error, the code will catch it quickly, instead of trying to proceed even though it doesn't have the data from the file.

Walking through a list and doing something for each element is a common pattern in Erlang. Instead of keeping an index and using lists:nth, you'd normally call lists:foreach, which is a higher-level function that calls your function on each list element. So instead of:

   L=length(T),
   %io:fwrite("~w",[L]),
   for(L,T).

you could write:

lists:foreach(fun for/1, T).

Then your for function (which perhaps would get another name, since it's no longer doing the looping) would start with:

for(S) ->
   S1=element(1,S),
   io:fwrite(" ~w~n",[S1]),
   %% ...

Here, you could do the pattern matching in the function head, to avoid calling element:

for({S1, Q}) ->
   io:fwrite(" ~w~n",[S1]),
   R=length(Q),
   B=nth(1,Q),
   for1 (R,Q),
   Pid=spawn(exchange,process,[]),
   register(S1,Pid),
   S1 ! {sender, S1,R,Q}.

Here we have the length+nth pattern again, this time for for1, so we could replace that too with lists:foreach:

for({S1, Q}) ->
   io:fwrite(" ~w~n",[S1]),
   lists:foreach(fun for1/1, Q),
   Pid=spawn(exchange,process,[]),
   register(S1,Pid),
   S1 ! {sender, S1,R,Q}.

Hope that helps - the for1 function doesn't compile when I try it, but hopefully this should get you started.