From 9fe8adf35c16ab5d4566b03f3b36863c90b5b6dd Mon Sep 17 00:00:00 2001
From: Hans Bolinder How code is compiled and loaded is not a language issue, but
- is system dependent. This chapter describes compilation and
- code loading in Erlang/OTP with pointers to relevant parts of
+ is system-dependent. This section describes compilation and
+ code loading in Erlang/OTP with references to relevant parts of
the documentation. Erlang programs must be compiled to object code.
- The compiler can generate a new file which contains the object
- code. The current abstract machine which runs the object code is
+ The compiler can generate a new file that contains the object
+ code. The current abstract machine, which runs the object code, is
called BEAM, therefore the object files get the suffix
The compiler is located in the Kernel module The compiler is located in the module The Erlang shell understands the command There is also a module The compiler can also be accessed from the OS prompt, see
- There is also a module The compiler can also be accessed from the OS prompt, see the
+ The The object code must be loaded into the Erlang runtime
- system. This is handled by the code server, see
- The code server loads code according to a code loading strategy
+ system. This is handled by the code server, see the
+ 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
+ embedded. In interactive mode, code is 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. This is described in System Principles.
compile:file(Module)
compile:file(Module, Options)
% erl -compile Module1...ModuleN
% erl -make
@@ -68,13 +72,17 @@ compile:file(Module, Options)
Both old and current code is valid, and may be evaluated +
Both old and current code is valid, and can be evaluated concurrently. Fully qualified function calls always refer to - current code. Old code may still be evaluated because of processes + current code. Old code can still 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 +
If a third instance of the module is loaded, the code server + removes (purges) the old code and any processes lingering in it is + 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:
+ fully qualified function call. +Example:
-module(m). -export([loop/0]). @@ -109,60 +118,62 @@ loop() -> loop() end.
To make the process change code, send the message
-
For code replacement of funs to work, the syntax
-
For code replacement of funs to work, use the syntax
+
The
The
Doing external call in on_load to the module itself
+ Doing external call in At module upgrade, other processes calling the module
- get suspended waiting for on_load to finish. This can be very bad
+ get suspended waiting for At module upgrade, no rollback is done if the on_load function fails.
- The system will be left in a bad limbo state without any working
+ At module upgrade, no rollback is done if the
+
The problems with module upgrade described above could be fixed in future - releases by changing the behaviour to not make the module reachable until - after the on_load function has successfully returned.
+The problems with module upgrade described above can be fixed in future
+ Erlang/OTP releases by changing the behaviour to not make the module reachable until
+ after the
The
The
Its syntax is as follows:
-on_load(Name/0).-
It is not necessary to export the function. It will be called in a - freshly spawned process (which will be terminated as soon as the function +
It is not necessary to export the function. It is called in a
+ freshly spawned process (which terminates as soon as the function
returns). The function must return
A process that calls any function in a module whose
In embedded mode, all modules will be loaded first and then - will all on_load functions be called. The system will be - terminated unless all of the on_load functions return +
In embedded mode, first all modules are loaded.
+ Then all
Example:
+Example:
-module(m). @@ -174,7 +185,7 @@ load_my_nifs() -> erlang:load_nif(NifPath, Info).
If the call to