[erlang-questions] Documentation on Binding a C library to Erlang

Peer Stritzinger peerst@REDACTED
Sun Feb 2 14:03:03 CET 2014


Besides what Steve suggested I'd recommend writing a port (going the 
"not really wanting the C library running with the VM" path).

Ports are external processes which comunicate via I/O.   This is a bit 
slower than the other posibilities butit has a huge advantage: if your 
port program crashes, your Erlang node will live on its easy to have a 
supervisor restart your Port.  This will keep your fault tolerance.

When writing a C port program, I usually use a very simplistic protocol 
(framing with {packet, 2} or {packet, 4} ) which keeps especially the C 
code easy.  It easier to pack everything C friendly in binaries than 
decode general Erlang terms in the port.

Since when my C code will be restarted when it crashes it accomodates a 
C style with lots of assert() macros in it (which are enabled in 
production).

Also be aware: NIFs look easier than linked in drivers but are actually 
hard to do right.  Problem is that a long running NIF blocks the Erlang 
scheduler it runs on.   This counteracts often the faster performance 
of the C code and results in a less responsive system.

Cheers,
-- Peer

On 2014-01-31 21:42:02 +0000, Steve Vinoski said:

> 
> 
> 
> On Fri, Jan 31, 2014 at 4:35 PM, kraythe . <kraythe@REDACTED> wrote:
> Greetings, I am a newbie to erlang. I am interested in binding a c 
> library that I have as source (that I have written) to Erlang. In Java 
> I would use JNI. In Erlang I don't know. Can anyone point me at where 
> the docs are for this? Especially a tutorial. Google has been 
> unrevealing and i have been through hundreds of dead ends. 
> 
> If you really want the C library running together with the Erlang VM, 
> you'll write either a set of native implemented functions (NIFs) or a 
> linked-in driver. See the links below.
> 
> http://www.erlang.org/doc/tutorial/nif.html
> http://www.erlang.org/doc/man/erl_nif.html
> http://www.erlang.org/doc/apps/erts/driver.html
> http://www.erlang.org/doc/man/erl_driver.html
> 
> --steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140202/ad361216/attachment.htm>


More information about the erlang-questions mailing list