This is an example of how to solve the
A port driver is a linked in driver, that is accessible as a port from an Erlang program. It is a shared library (SO in Unix, DLL in Windows), with special entry points. The Erlang runtime calls these entry points, when the driver is started and when data is sent to the port. The port driver can also send data to Erlang.
Since a port driver is dynamically linked into the emulator process, this is the fastest way of calling C-code from Erlang. Calling functions in the port driver requires no context switches. But it is also the least safe, because a crash in the port driver brings the emulator down too.
Just as with a port program, the port communicates with a Erlang process. All communication goes through one Erlang process that is the connected process of the port driver. Terminating this process closes the port driver.
Before the port is created, the driver must be loaded. This is
done with the function
The port is then created using the BIF
-module(complex5). -export([start/1, init/1]). start(SharedLib) -> case erl_ddll:load_driver(".", SharedLib) of ok -> ok; {error, already_loaded} -> ok; _ -> exit({error, could_not_load_driver}) end, spawn(?MODULE, init, [SharedLib]). init(SharedLib) -> register(complex, self()), Port = open_port({spawn, SharedLib}, []), loop(Port).
Now it is possible to implement
foo(X) -> call_port({foo, X}). bar(Y) -> call_port({bar, Y}). call_port(Msg) -> complex ! {call, self(), Msg}, receive {complex, Result} -> Result end.
The
loop(Port) -> receive {call, Caller, Msg} -> Port ! {self(), {command, encode(Msg)}}, receive {Port, {data, Data}} -> Caller ! {complex, decode(Data)} end, loop(Port) end.
Assuming that both the arguments and the results from the C
functions will be less than 256, a very simple encoding/decoding
scheme is employed where
encode({foo, X}) -> [1, X]; encode({bar, Y}) -> [2, Y]. decode([Int]) -> Int.
The resulting Erlang program, including functionality for stopping the port and detecting port failures is shown below.
The C driver is a module that is compiled and linked into a
shared library. It uses a driver structure, and includes the
header file
The driver structure is filled with the driver name and function
pointers. It is returned from the special entry point, declared
with the macro
The functions for receiving and sending data, are combined into
a function, pointed out by the driver structure. The data sent
into the port is given as arguments, and the data the port
sends back is sent with the C-function
Since the driver is a shared module, not a program, no main
function should be present. All function pointers are not used
in our example, and the corresponding fields in the
All functions in the driver, takes a handle (returned from
The example_drv_start, is the only function that is called with a handle to the port instance, so we must save this. It is customary to use a allocated driver-defined structure for this one, and pass a pointer back as a reference.
It is not a good idea to use a global variable; since the port driver can be spawned by multiple Erlang processes, this driver-structure should be instantiated multiple times.
1. Compile the C code.
unix> gcc -o exampledrv -fpic -shared complex.c port_driver.c windows> cl -LD -MD -Fe exampledrv.dll complex.c port_driver.c
2. Start Erlang and compile the Erlang code.
> erl Erlang (BEAM) emulator version 5.1 Eshell V5.1 (abort with ^G) 1> c(complex5). {ok,complex5}
3. Run the example.
2> complex5:start("example_drv"). <0.34.0> 3> complex5:foo(3). 4 4> complex5:bar(5). 10 5> complex5:stop(). stop