Erlang/OTP Forums

Author Message

<  Advanced Erlang/OTP  ~  Concurrency question when interfacing with a C program

ptmoy
Posted: Thu Dec 24, 2009 6:11 pm Reply with quote
User Joined: 24 Dec 2009 Posts: 10 Location: New Jersey, U.S.A.
I have some calculation routines written in C that I would like to call from Erlang programs. If I want to call the same C routine from many Erlang processes simultaneously, what's the best way to handle the inter-communication between the two languages and not loose the concurrency advantages offered by Erlang?

One way to do this seems to be using multiple OS C processes, or multi-threading the external OS C process (and communicate through one or more ports). Another possibility may be to create multiple C-nodes and let Erlang manage concurrency and distribution the usual way.

I am fairly new to Erlang, and can use all the help I can get in thinking this through. Please post if you have an opinion on this. Thanks in advance. Question
View user's profile Send private message
uwiger
Posted: Thu Dec 24, 2009 8:16 pm Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
ptmoy wrote:
I have some calculation routines written in C that I would like to call from Erlang programs. If I want to call the same C routine from many Erlang processes simultaneously, what's the best way to handle the inter-communication between the two languages and not loose the concurrency advantages offered by Erlang


There are a number of different alternatives. It is not particularly difficult to make all of them work through the same API from the Erlang point of view.

    Keep the C code in a separate OS process and talk to them via pipes, TCP loopback etc. This is safe, but the slowest option

    Make a linked-in driver. The driver API supports driver-level or port-level locking, which means you can safely run several instances of the C code - note, "safe" from a locking point of view. If your code misbehaves, it may well bring down the Erlang VM, as it runs in the same memory space.

    Write a NIF (Native Implemented Function). This is still experimental, but the fastest way to call C code. It doesn't offer any high-level support for concurrency control, so you need to ensure that multiple invocations of your code do not access shared data in an uncontrolled way.

_________________
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
ptmoy
Posted: Sat Jan 02, 2010 8:46 pm Reply with quote
User Joined: 24 Dec 2009 Posts: 10 Location: New Jersey, U.S.A.
If I spawned 10 Erlang processes on the same node, and call the same NIF from each of the 10 spawned processes simultaneously, does each of the NIF calls execute on the same Erlang address space as its caller?

I understand all the NIF calls will run on the same OS-process address space as the VM. What I'm really asking is whether Erlang knows how to take advantage of multi-core or multi-processor computers when processing multiple simultaneous calls to the same NIF.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum