Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang error when spawning a process

Tags:

erlang

I start a process as follows

start() ->
register (dist_erlang, spawn(?MODULE, loop, [])),
ok.

But get the following error when trying to run start().

Error in process <0.62.0> with exit value: {undef,[{dist_erlang,loop,[]}]}

The module is called dist_erlang.

What am I doing wrong?

Thanks

like image 761
some_id Avatar asked Oct 11 '10 01:10

some_id


People also ask

What is spawn in Erlang?

spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list.

Which module is used to spawn processes manually in Erlang?

The Erlang BIF spawn is used to create a new process: spawn(Module, Exported_Function, List of Arguments).

What function is used to link processes together in Erlang?

It is possible to call link(pid) in Erlang to link the currently executing process to the process identified by pid .

What is process dictionary in Erlang?

Erlang has something called Process Dictionary which is a sort of Key/Value store that belongs to each Erlang process. We can use it to store values in a simple manner inside Erlang programs while avoiding the limitations of Pure Functional Programming.


2 Answers

Although the question is old, I post what helped me when I was wrestling with the Erlang compiler.

This (incomplete) snippet

-export([start/0]).

start() ->
    Ping = spawn(?MODULE, ping, [[]]),
    ...

ping(State) ->
    receive
        ...
    end.

fails with error:

=ERROR REPORT==== 2-Sep-2013::12:17:46 ===
Error in process <0.166.0> with exit value: {undef,[{pingpong,ping,[[]],[]}]}

until you export explicitly ping/1 function. So with this export:

-export([start/0, ping/1]).

it works. I think that the confusion came from some examples from Learn You Some Erlang for great good where the modules sometimes have

-compile(export_all).

which is easy to overlook

like image 182
Jakub M. Avatar answered Oct 06 '22 01:10

Jakub M.


Based on your previous question, your loop function takes one parameter, not none. Erlang is looking for loop/0 but can't find it because your function is loop/1.

The third parameter to spawn/3 is a list of parameters to pass to your function, and in the case you've shown the list is empty. Try:

register (dist_erlang, spawn(?MODULE, loop, [[]]))

In this case, the third parameter is a list that contains one element (an empty list).

like image 21
Greg Hewgill Avatar answered Oct 06 '22 00:10

Greg Hewgill