Hot Code Loading and Modules
Erlang supports language-level Dynamic Software Updating. To implement this, code is loaded and managed as "module" units; the module is a compilation unit. The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each. The versions are referred to as the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.
An example of the mechanism of hot code loading:
%% A process whose only job is to keep a counter. %% First version -module(counter). -export. start -> loop(0). loop(Sum) -> receive {increment, Count} -> loop(Sum+Count); {counter, Pid} -> Pid ! {counter, Sum}, loop(Sum); code_switch -> ?MODULE:codeswitch(Sum) % Force the use of 'codeswitch/1' from the latest MODULE version end. codeswitch(Sum) -> loop(Sum).For the second version, we add the possibility to reset the count to zero.
%% Second version -module(counter). -export. start -> loop(0). loop(Sum) -> receive {increment, Count} -> loop(Sum+Count); reset -> loop(0); {counter, Pid} -> Pid ! {counter, Sum}, loop(Sum); code_switch -> ?MODULE:codeswitch(Sum) end. codeswitch(Sum) -> loop(Sum).Only when receiving a message consisting of the atom 'code_switch' will the loop execute an external call to codeswitch/1 (?MODULE
is a preprocessor macro for the current module). If there is a new version of the "counter" module in memory, then its codeswitch/1 function will be called. The practice of having a specific entry-point into a new version allows the programmer to transform state to what is required in the newer version. In our example we keep the state as an integer.
In practice, systems are built up using design principles from the Open Telecom Platform which leads to more code upgradable designs. Successful hot code loading is a tricky subject; code needs to be written to make use of Erlang's facilities.
Read more about this topic: Erlang (programming Language)
Famous quotes containing the words hot, code and/or loading:
“Babe, you know how these things go, its like a crap game. When youre hot you shoot everything, you shoot the works. Well, right now baby, Im so hot Im burning up all over.”
—Gus Van Sant, U.S. screenwriter, and Dan Yost. Bob Hughes (Matt Dillon)
“Motion or change, and identity or rest, are the first and second secrets of nature: Motion and Rest. The whole code of her laws may be written on the thumbnail, or the signet of a ring.”
—Ralph Waldo Emerson (18031882)
“Nitrates and phosphates for ammunition. The seeds of war. Theyre loading a full cargo of death. And when that ship takes it home, the world will die a little more.”
—Earl Felton, and Richard Fleischer. Captain Nemo (James Mason)