2009 March 30th Monday (三月 三十日 月曜日)

来源:互联网 发布:华宇软件 基金会 编辑:程序博客网 时间:2024/04/19 17:36

 
  Erlang supports change of code in a running system. Code replacement is done on module level.

  The code of a module can exist in two variants in a system: current and old. When a module is loaded into the system for the first time, the code
becomes 'current'. If then a new instance of the module is loaded, the code of the previous instance becomes 'old' and the new instance becomes 'current'.

  Both old and current code is valid, and may be evaluated concurrently. Fully qualified function calls always refer to current code. Old code may stil
be evaluated because of processes lingering in the old code.

  If a third instance of the module is loaded, the code server will remove (purge) the old code and any processes lingering in it will be terminated.
Then the third instance becomes 'current' and the previously current code becomes 'old'.

  To change from old code to current code, a process must make a fully qualified function call. Example:

-module(m).
-export([loop/0]).

loop() ->
    receive
        code_switch ->
            m:loop();
        Msg ->
            ...
            loop()
    end.
  To make the process change code, send the message code_switch to it. The process then will make a fully qualified call to m:loop() and change to
current code. Note that m:loop/0 must be exported.

  For code replacement of funs to work, the tuple syntax {Module,FunctionName} must be used to represent the fun.

Code Loading

  The object code must be loaded into the Erlang runtime system. This is handled by the code server.

  The code server loads code according to a code loading strategy which is either interactive (default) or embedded. In interactive mode, code are
searched for in a code path and loaded when first referenced. In embedded mode, code is loaded at start-up according to a boot script.