I've found that Elixir programs can run C code either via NIFs (native implemented functions) or via OS-level ports. Having read those and similar links, I'm not a hundred percent clear on when to use one or the other method (or something else entirely?), and feel it would be good to have a direct comparison available, for myself and other novices. Can anyone provide?
Running C code in Elixir/Erlang: Ports or NIFs?
4.3k Views Asked by Vivian At
1
There are 1 best solutions below
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in COMPARE
- String Comparator Operator C++
- Compare two strings in vba excel
- How do I check if a time is between two times which are stored as Strings in Ruby on Rails?
- Diff on hibernate envers revisions
- Filter List based on another list
- Compare values of two vectors
- Retrieving aggregated results from two queries
- Comparison between vectors in matlab
- Scala set element uniqueness: What to implement for comparison of user defined classes?
- Perl comparing two hashes
- Compare two files and append the values, leave the mismatches as such in the output file
- How to compare two expressions with maxima?
- Most efficient way to print differences of two arrays?
- comparing two dates. Equal isn't working as well
- Python - Using a created list as a parameter
Related Questions in ELIXIR
- Multiline comment in Elixir
- Output tabular data with IO.ANSI
- Does Elixir have coding standards or an authoritative style guide published by the language developers, like PEP8 or the Erlang Programming Rules?
- Check if a URL is valid in elixir
- Elixir exrm release crashes on eredis start_link
- Writing a library that works for BitStrings or Lists
- Elixir exrm console works but application does not start automatically
- Why do I receive a FunctionClauseError ("no function clause matching") in this Elixir program, translated from Erlang?
- Expect-like functionality to IO.gets?
- How to stub (or prevent running) of a call to a worker in my ExUnit test?
- Is Elixir's System.cmd blocking
- Elixir - Nested JSON parsing to structs
- What is the number that shows up after you define an anonymous function in elixir?
- Using System.cmd within a Poolboy worker (gen_server) causes silent failure
- How Can We Clear the Screen in Iex on Windows
Related Questions in BEAM
- Rabbitmq listens on all interfaces
- Why elixir debugger is calling built-in function instead of mine if they have same name
- Erlang Binary Leak?
- Apache Beam Streaming Write/Read BigQuery
- Apache Beam Publish Kafka Message with KafkaIO and KafkaAvroSerialization for GenericRecord
- Why is math so slow in Erlang/the BEAM VM?
- Beam VM Scheduler vs OS Scheduler
- Is gen_server restart strategy copy state?
- Why does the nif function block the Erlang VM from scheduling other processes?
- Why does the following code cause the Erlang HiPE compiler crash?
- How to implement a function to notify a node that the other node quit or not
- Why are atoms not garbage collected by the BEAM?
- merge collection with different window strategy
- Apache Beam - Convert empty Dataframe back to PCollection
- Dependency conflict when upgrading Beam version for Cloud Dataflow Template (Java SDK)
Related Questions in LANGUAGE-BINDING
- C/Python binding: pointer address modification
- Writting a wrapper for a customized lua-based scripting language
- pybind11 cmake example cannot find the main function
- Symbolic Computation Library in pure C
- Share functions between source files in multiple programming languages
- Binding Software
- Parsing/executing C# code in C++ (on Linux)?
- What kinds of C++ functions can be placed in a C function pointer?
- calling c code from lua
- Use Python to call a JavaScript function that is on a webpage
- Running C code in Elixir/Erlang: Ports or NIFs?
- extending 'incomplete' types (SWIG)
- template type as struct data member in SWIG
- Iterating over std::map in PHP with SWIG
- Are any language bindings available for NSSpeechSynthesizer Cocoa class?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
What are ports?
Ports are basically separate programs which are run separately from the Erlang VM. The Erlang VM communicates with the running port over standard input/output, and the resulting port lives behind an Erlang process that owns it and can facilitate communication between the port and the rest of your Erlang or Elixir application. Ports are "safe" in the sense that if the port crashes, it doesn't bring down the whole Erlang VM.
Porcelain might be of interest as a possible improvement and expansion over what's already provided in the
Portmodule.System.cmd/3also uses ports in its underlying implementation.What are NIFs?
Native inline functions or "NIFs" are functions defined in what are essentially shared libraries / DLLs loaded by the Erlang VM and written using some language which exposes a C-compatible ABI. NIFs are more efficient than ports (since they don't have to communicate over
STDIN/STDOUT) and are simpler in many respects (since you don't have to deal with encoding and decoding data between your Elixir and non-Elixir codebases), but they're also much less safe; a NIF can crash the Erlang VM, and a long-running NIF can potentially lock up the Erlang VM (since the scheduler can't reason about native code).What are port drivers?
Port drivers are kind of an in-between approach to integrating external code with an Erlang or Elixir codebase. Like NIFs, they're loaded into the Erlang VM, and a port driver can therefore crash or hang the whole VM. Like ports, they behave similarly to Erlang processes.
When should I use a port?
When should I use a NIF?
When should I use port drivers?
What do you recommend?
There are two aspects to weigh here:
If you want maximum safety behind a process-like interface, go with a port.
If you want maximum safety behind a module-like interface, go with a module with functions that either wrap
System.cmd/3or directly use a port to communicate with your external codeIf you want better efficiency behind a process-like interface, go with a port driver.
If you want better efficiency behind a module-like interface, go with NIFs.