Can we use indices to comunicate among many processes?


I’m new to Elixir, I’m really excited about the idea of having many concurrent processes communicating through messages. Unfortunately, most of the examples I came across only show the case of generating two processes communicating with each other through knowing each other’s PIDs and I’m not sure if I can treat the PIDs as index-able ids.
So I was wondering how can I spawn for e.g 100 concurrent processes, each process should have an identifiable index for example: process1_id=1, process2_id=2…, and process100_id=100. Each process stores a list of ids of 5 other processes and has an internal function that can randomly produce a number from 1 to 100 upon receiving a “hello” message from another process in its list. Then depending on the generated number each process should send a “hello” message to another process.
To put the algorithm more concretely:
1- first generate and spawn 100 concurrent processes
2- upon generation, each process should internally hold and initialize a list of 5 random numbers from 1 to 100
e.g process1_list=[2, 81, 14, 20, 70], process2_list=[31, 21,3,99,10] and so on…
3- each process should continually sleep for 10 seconds then generate a random number from 1 to 100 and accordingly send a “hello” message to another process that have an id equal to the generated number.
4- when a process receives a “hello” message from another process, it should check their internal list of the initially 5 generated number if the id of the sender is in the list then the receiver should immediately (i.e not after 10s) generate another random number from 1 to 100 and send a “hello” message to another process. If the id of the sender was not in the list then the receiver should do nothing immediately but as default should generate a random number from 1-100 after each 10 second pass.

I deeply appreciate if anyone can guide me on how can I implement the above efficiently in Elixir

Many thanks in advance


Certainly, an ETS table sounds like the perfect use for this, or name them with gproc (Registry?) with something like {:my_process, 42} or so would probably be the best way. Fairly simple code to make but this is an excellent learning opportunity so you should try to write it yourself. If you need help, just ask! :slight_smile:

1 Like

thanks a lot for your reply. I will try to learn more about ETS but since I’m new to Elixir and to be honest currently I’m not sure if I’m switching to Elixir or not so I really wanted to see a concrete example that showcases the capability of the language for this example. As I said most of the tutorials online don’t go beyond showcasing 2 communication between two processes!

Look at the examples in the Registry documentation, they should suffice:


I have also came across this video about Registry which seems to be very relevant for what I was looking for. I’ll put the link here just in case other beginners like me were looking for something similar.