[erlang-questions] How does code:purge/1 know to kill processes?

zxq9 zxq9@REDACTED
Thu Jun 9 09:45:25 CEST 2016


On 2016年6月9日 木曜日 08:25:02 Roger Lipscombe wrote:
> The documentation for code:purge/1
> (http://erlang.org/doc/man/code.html#purge-1) says "If some processes
> still linger in the old code, these processes are killed before the
> code is removed."
> 
> How is "lingering" defined?
> 
> Does the VM kill the process when it attempts to invoke the old module?
> Does the VM walk the stack for all processes and kill those that have
> the old code somewhere in their call stack?
> Something else?

The second one:

ceverett@REDACTED:~/Code/erlang$ cat greeter.erl 
-module(greeter).
-export([start_monitor/0, hello/1, loop/0]).

start_monitor() ->
    spawn_monitor(?MODULE, loop, []).

hello(Pid) ->
    Pid ! greeting.

loop() ->
    receive
        greeting ->
            io:format("Hello!~n"),
            loop()
    end.
ceverett@REDACTED:~/Code/erlang$ erlc greeter.erl 
ceverett@REDACTED:~/Code/erlang$ erl
Erlang/OTP 18 [erts-7.2] [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]

Eshell V7.2  (abort with ^G)
1> {Pid1, Ref1} = greeter:start_monitor().
{<0.35.0>,#Ref<0.0.1.31>}
2> 
[1]+  停止                  erl
ceverett@REDACTED:~/Code/erlang$ sed -i 's/Hello/Hey/' greeter.erl 
ceverett@REDACTED:~/Code/erlang$ erlc greeter.erl 
ceverett@REDACTED:~/Code/erlang$ fg
erl

2> code:load_file(greeter).               
{module,greeter}
3> {Pid2, Ref2} = greeter:start_monitor().
{<0.38.0>,#Ref<0.0.1.40>}
4> greeter:hello(Pid1).
Hello!
greeting
5> greeter:hello(Pid2).
Hey!
greeting
6> code:purge(greeter).
true
7> flush().
Shell got {'DOWN',#Ref<0.0.1.31>,process,<0.35.0>,killed}
ok



More information about the erlang-questions mailing list