Search code examples
processerlangterminate

erlang: ending a process without knowing PiD?


Simply put; how can I end a process if I accidentally forgot to equate a Pid variable when I started a process using this:

9> trivial_process:start().
<0.67.0>
10>  

I know I should have written Pid = trivial_process:start(). Is there some way to take <0.67.0> and terminate the process?

-module(trivial_process).
-export([start/0]).

start() -> 
  spawn(fun() -> loop() end).

loop() ->
  receive
    Any ->
      io:format("~nI got the message: ~p~n",[Any]),
      loop()
  end.

EDIT:Answer.

8> Pid = "<0.67.0>".
9> A2 = list_to_pid(Pid).
<0.67.0>

Solution

  • You can use the list_to_pid function. The docs are here. You shouldn't use this in deployed code, it's only useful for debugging. It doesn't work with remote pids, either.

    Reference: Something maybe you don’t know about Erlang PIDs