多进程Erlang消息收发

2ul0zpep  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(170)

I am a working on a school project in Erlang. I am trying to make a master process here which will spawn 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]}.
but I couldnot create new processes as I'm receiving this error. Please help me solve this, I don't understand the error.

invoke(Elem)  ->
  {X,Y} = Elem,
  Pid = spawn(calling, people, [X,Y]),
  register(X,Pid).

second module

people(N,Persons) ->
  lists:foreach(fun contact/1, Persons),
  io:fwrite("in people\n").

The error I'm getting is:

=ERROR REPORT==== 18-Jun-2020::20:27:49 ===
Error in process <0.59.0> with exit value:
{undef,[{calling,people,[john,[jill,joe,bob]],[]}]}

=ERROR REPORT==== 18-Jun-2020::20:27:49 ===
Error in process <0.60.0> with exit value:
{undef,[{calling,people,[jill,[bob,joe,bob]],[]}]}

=ERROR REPORT==== 18-Jun-2020::20:27:49 ===
Error in process <0.61.0> with exit value:
{undef,[{calling,people,[sue,[jill,jill,jill,bob,jill]],[]}]}

=ERROR REPORT==== 18-Jun-2020::20:27:49 ===
Error in process <0.62.0> with exit value:
{undef,[{calling,people,[bob,[john]],[]}]}

=ERROR REPORT==== 18-Jun-2020::20:27:49 ===
Error in process <0.63.0> with exit value:
{undef,[{calling,people,[joe,[sue]],[]}]}
bakd9h0s

bakd9h0s1#

The error message says that the function people with 2 arguments does not exist in the module calling . There are several possibilities for this:

  1. the module calling does not exist
  2. the module calling is not reachable (not in the path)
  3. the module calling is not compiled
  4. the function people/2 is not exported in the module calling

Edit

You can check the path code (the list of places where the Erlang machine will search for modules) with the command rp(code:get_path()). . If you did nothing special, you will get a list containing "." as first element and then all the erlang libraries. The dot means the working directory.
With the command pwd(). , you can get the place of this working directory.
Then you should look for a file named calling.beam. If it does not exist, you must compile the module for example with the command c(calling). in the shell.
If it exists but is in an another place than the working directory, you have to choose between one of these solutions (I can't know which one is better in your case):

  1. add the directory where the beam file is (must be done each time you start the Erlang VM) with the command code:add_path("Path/to/your/beam/file")
  2. Check that all the beam files you need are in the same directory and start the VM from it. Eventually change the working directory to the right place with: cwd("beam/file/directory"). .
    In Erlang, although it is not mandatory, people use to organize their files in the same way, it looks like:
  • application
  • doc -> for all documentation files
  • src -> for all source files (module.erl, application.app.src)
  • include -> for all header files (module.hrl)
  • ebin -> for all the compiled files (module.beam, application.app)
  • ...

This is enough for starting and make some simple programs. For real application, people use tools like rebar3 to manage and organize the files and the build process.

相关问题