Can Elixir / Erlang copy a process, including its memory?

I am considering a solution to the problem using Elixir, mainly because of the ability to generate a large number of processes cheaply.

In my scenario, I would like to create some “original” processes that load certain, immutable data into memory, and then make copies of these processes as needed. All copies will use the same basic data , but they will perform different read-only tasks; for example, imagine that in one “original” there is the text “War and Peace” in memory, and each copy of this original does a different analysis in the text.

My questions:

  • Is it possible to copy an existing process, memory contents and everything in Elixir / Erlang VM?
  • If so, does each copy consume the same amount of memory as the original, or can they share the memory, as Unix processes with a copy-to-write strategy do? (And in this case there will be no subsequent entries.)
+6
source share
3 answers

There is no built-in way to copy processes. The easiest way to do this is to start the “original” process and “copies” and send all the relevant data in the messages in the copy. Processes do not share data, so there is no more efficient way to do this. Putting data in ETS tables partially helps with sharing, because data in ETS tables is copied to the process when they are used, however you do not need to have all the data in the process heap.

+6
source

There is no process-specific data in the Erlang process, except for the data stored in the variables (and the process dictionary ), so to create a copy of the process memory, just create a new process by passing all the relevant variables as arguments to the function.

In general, memory is not shared between processes; everything is copied. The exceptions are ETS tables (although data is copied from ETS tables when processes read it), and binary files larger than 64 bytes. If you store "War and Peace" in binary format and send it to each workflow (or transfer it together when you create these workflows), then the processes will share memory only by copying it if they want to change the binary file. For more details, see In the chapter in the binaries in the Erlang performance guide.

+5
source

You are thinking of Erlang / Elixir processes similar to Unix processes. They are not there at all, I really wish they had a different name, because they really are neither threads nor processes in the standard sense of Unix. It took me a while to wrap my head around the differences.

You have to throw away all your preconceived ideas about processes, they are all wrong. Eprocesses have the following characteristics.

  • They are cheap and fast. Use a lot, there is always more.

  • They have no resources [1]. (Even writing to stdout is a message to another Eprocess.)

  • IPCs (or messages) are very fast with relatively low overhead compared to standard Unix IPCs.

What I would like to try in your case is to create a server that managed the data, and each working analyst should tell the server that it is needed. This is perfectly acceptable for Eprocess to be a more or less shared memory manager.

For me, the most useful way to think about Eprocesses is with objects with their own thread of execution.

[1] Well, there is an ETS table, but it’s best to think of them as a resource exchange until you have to.

+1
source

Source: https://habr.com/ru/post/983747/


All Articles