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:
“As a bathtub lined with white porcelain,
When the hot water gives out or goes tepid,
So is the slow cooling of our chivalrous passion,
O my much praised but-not-altogether-satisfactory lady.”
—Ezra Pound (18851972)
“Wise Draco comes, deep in the midnight roll
Of black artillery; he comes, though late;
In code corroborating Calvins creed
And cynic tyrannies of honest kings;
He comes, nor parlies; and the Town, redeemed,
Gives thanks devout; nor, being thankful, heeds
The grimy slur on the Republics faith implied,
Which holds that Man is naturally good,
Andmoreis Natures Roman, never to be
scourged.”
—Herman Melville (18191891)
“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)