পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 31, 2017

Erlang - Drivers

Sometimes we want to run a foreign-language program inside the Erlang Runtime System. In this case, the program is written as a shared library that is dynamically linked into the Erlang runtime system. The linked-in driver appears to the programmer as a port program and obeys exactly the same protocol as for a port program.

Creating a Driver

Creating a linked-in driver is the most efficient way of interfacing foreign-language code with Erlang, but it is also the most dangerous. Any fatal error in the linked-in driver will crash the Erlang System.
Following is an example of a driver implementation in Erlang −

Example

-module(helloworld). 
-export([start/0, stop/0]). 
-export([twice/1, sum/2]). 

start() ->
   start("example1_drv" ). 
start(SharedLib) ->
   case erl_ddll:load_driver("." , SharedLib) of 
   ok -> ok; 
      {error, already_loaded} -> ok; 
      _ -> exit({error, could_not_load_driver}) 
   end, 
   
   spawn(fun() -> init(SharedLib) end). 

init(SharedLib) -> 
   register(example1_lid, self()), 
   Port = open_port({spawn, SharedLib}, []), 
   loop(Port). 

stop() -> 
   example1_lid ! stop. 

twice(X) -> call_port({twice, X}). 
sum(X,Y) -> call_port({sum, X, Y}). call_port(Msg) -> 
   example1_lid ! {call, self(), Msg}, receive 
      {example1_lid, Result} -> 
      Result 
   end. 

LINKED-IN DRIVERS 223 
loop(Port) -> 
receive 
   {call, Caller, Msg} -> 
   Port ! {self(), {command, encode(Msg)}}, receive 
   {Port, {data, Data}} ->
   Caller ! {example1_lid, decode(Data)} 
   end, 

loop(Port); 
stop -> Port ! 
   {self(), close}, 
   receive 
      {Port, closed} -> 
      exit(normal) 
   end; 
   
      {'EXIT', Port, Reason} -> 
      io:format("~p ~n" , [Reason]), 
      exit(port_terminated) 
   end. 

encode({twice, X}) -> [1, X]; 
encode({sum, X, Y}) -> [2, X, Y]. decode([Int]) -> Int.
Please note that working with drivers is extremely complex and care should be taken when working with drivers.

No comments:

Post a Comment