Port drivers vs Ports

Romain Lenglet rlenglet@REDACTED
Thu Aug 17 03:54:43 CEST 2006


Thinus Pollard wrote:
> A Port runs in its own OS process, right? So you can stream
> data from one process to the other.
>
> A Port Driver attaches itself to the erlang runtime system.
> After a few test I found that while the code inside the port
> driver is executing, the erlang runtime system is hanging,
> waiting for the port driver to return. It makes sense to me at
> least, please correct me if I'm wrong.
>
> This would imply that I can't stream data from the port driver
> to erlang, since it is the same os process and while the port
> driver is streaming, the erlang part of the process can't
> accept the data, since it's blocked. Correct?

Yes, since there is only one thread for a whole Erlang node (in 
versions < R11B), when that thread executes a driver function it 
cannot schedule processes. If you need to execute in parallel, 
use asynchronous tasks in your driver (cf. function 
driver_async), and setup the VM to start several driver threads 
to execute those asynchronous tasks (+A option of the erl 
command).
Or, you can also use the SMP version of the emulator. In that 
case, several threads are used in the VM to schedule the 
processes and execute driver functions. When a thread is 
executing a driver function, the other threads can still 
schedule the processes.
But anyway, if your driver functions take some time to execute or 
are blocking, you *should* use asynchronous tasks.

> When I do a driver_output(port, &result, length_of_res), do
> the data get copied or does the erl_driver pass the result
> back in by reference?

If your result term contains binaries, as created by calling 
alloc_binary, the binaries are passed by reference, AFAIK. The 
other terms are copied.

> Also, If I want to output back to erlang more than once during
> a call, does the messages (driver_output) get queued so that
> my erlang process can pick them up after the port driver has
> returned, or is it possible that they can get lost if I send
> to many messages?

>From what I have understood from the code, they are normal 
messages. Therefore, they get queued.

> Last Question: Which of the two communication methods are
> preferred / gives the best performance, erl_interface or
> communicating via character lists/arrays.

It depends. Using driver_output et al. you can encode/decode only 
a limited set of term types. For instance, you cannot 
encode/decode pids or refs. Or, you can use erl_interface on top 
of driver_output et al. But that gets tricky.

<shameless-advertising>
That is what is done in Dryverl. Those details are hidden to you.
Dryverl solves your port driver programming needs. And it will 
make the coffee in soft real time in a future release. ;-)
</shameless-advertising>

> I need a port/port driver for connecting to a database.

-- 
Romain LENGLET



More information about the erlang-questions mailing list