Process ring implemented in Elixir using processeses.
This project implements a process ring in Elixir. A process ring is a system of concurrent processes, where each process is linked to its next neighbor in a circular manner. The processes communicate by passing messages in a ring fashion.
To create the process ring, follow these steps:
-
Open process_ring.exs using iex:
iex process_ring.exs
-
Create a list of processes using create_processes():
process_list = ProcessRing.create_processes(10) # Replace 10 with the desired number of processes
-
Create a process ring using create_ring()
ring = ProcessRing.create_ring(process_list) # Use the list of processes created previously
-
Start the ring:
ProcessRing.start_ring(ring)
ProcessRing.ex
: Contains the main module defining functions for creating processes, building the ring, starting the ring, and handling message passing.
Creates processes with the given size.
Creates a ring from a list of processes, setting their positions on the ring.
Starts the ring by sending a message to the first process.
Listens for messages and forwards them to the next process in the ring.
Sends a message to the given process ID, displaying a log. If the position is 0, it indicates the start of a new cycle.
Certainly! I've refined the example session and separated it into sections with explanations:
iex(1)> process_list = ProcessRing.create_processes(10)
[#PID<0.116.0>, #PID<0.117.0>, #PID<0.118.0>, #PID<0.119.0>, #PID<0.120.0>, #PID<0.121.0>, #PID<0.122.0>, #PID<0.123.0>, #PID<0.124.0>, #PID<0.125.0>]
In this step, we create a list of 10 processes. Each process is represented by a unique PID.
iex(2)> ring = ProcessRing.create_ring(process_list)
[
%{pid: #PID<0.116.0>, position: 0, next_process: #PID<0.117.0>},
%{pid: #PID<0.117.0>, position: 1, next_process: #PID<0.118.0>},
%{pid: #PID<0.118.0>, position: 2, next_process: #PID<0.119.0>},
%{pid: #PID<0.119.0>, position: 3, next_process: #PID<0.120.0>},
%{pid: #PID<0.120.0>, position: 4, next_process: #PID<0.121.0>},
%{pid: #PID<0.121.0>, position: 5, next_process: #PID<0.122.0>},
%{pid: #PID<0.122.0>, position: 6, next_process: #PID<0.123.0>},
%{pid: #PID<0.123.0>, position: 7, next_process: #PID<0.124.0>},
%{pid: #PID<0.124.0>, position: 8, next_process: #PID<0.125.0>},
%{pid: #PID<0.125.0>, position: 9, next_process: #PID<0.116.0>}
]
Here, we create a process ring using the previously generated list of processes. Each entry in the list represents a process in the ring, with information about its PID, position, and the PID of its next neighbor.
iex(3)> ProcessRing.start_ring(ring)
[.] #PID<0.115.0> SENT A MESSAGE TO #PID<0.116.0>
:ok
[+] New Cycle Started.
[.] #PID<0.116.0> SENT A MESSAGE TO #PID<0.117.0>
[.] #PID<0.117.0> SENT A MESSAGE TO #PID<0.118.0>
[.] #PID<0.118.0> SENT A MESSAGE TO #PID<0.119.0>
[.] #PID<0.119.0> SENT A MESSAGE TO #PID<0.120.0>
[.] #PID<0.120.0> SENT A MESSAGE TO #PID<0.121.0>
[.] #PID<0.121.0> SENT A MESSAGE TO #PID<0.122.0>
[.] #PID<0.122.0> SENT A MESSAGE TO #PID<0.123.0>
[.] #PID<0.123.0> SENT A MESSAGE TO #PID<0.124.0>
[.] #PID<0.124.0> SENT A MESSAGE TO #PID<0.125.0>
[.] #PID<0.125.0> SENT A MESSAGE TO #PID<0.116.0>
[+] New Cycle Started.
[.] #PID<0.116.0> SENT A MESSAGE TO #PID<0.117.0>
[.] #PID<0.117.0> SENT A MESSAGE TO #PID<0.118.0>
[.] #PID<0.118.0> SENT A MESSAGE TO #PID<0.119.0>
...
Finally, we start the process ring, and you can observe the messages being passed between processes. A new cycle starts after each round, demonstrating the circular communication pattern.