Reputation: 81
I'm aware of the spawn
function but I'm not sure that I'm using it correctly. What I need to do is read through a database and have all elements create a process to communicate with all other elements.
For example, say we have elements a
, b
, c
, d
, e
:
Then
a
must have a process to communicate with b
, c
, d
, and e
.
b
must communicate with a
, c
, d
, and e
.
c
must communicate with a
, b
, d
, and e
, and so on....
What I do is loop though these elements and send them to my spawn
function, but I don't see how this is "concurrent" programming.
Should/Can I have a spawn
function for each individual element in my database?
Upvotes: 0
Views: 318
Reputation: 918
A little hard to understand the question and your task. As soon as you have two or more processes running at the same time you have concurrent programming.
You get N elements from the database and for each element you start a Process X. Do you want each of these processes to have children processes for each other element in N, so each X has N-1 children processes? You can have each process X spawn N-1 processes as children. As you have N number of X processes, each running in parallel, it is concurrent. Each one can concurrently spawn their children processes and each child process can communicate with each other child process concurrently. In your case you have N * (N-1) processes running concurrently. Erlang scheduler will take care of when the actual processes tasks get executed, so if you only have one processor they will only appear to be running concurrently because the scheduler will be switching between running the processes really fast, if you have 2 cores than 2 will be running concurrently... but the good thing is that you don't need to think about that, because you wrote the program concurrently and if you run it on hardware that has multiple processors Erlang will use them.
Upvotes: 0
Reputation: 48589
Concurrent programing may occur when process a
sends a message to process c
at the same time that process b
sends a message to process e
. If your computer has two cores, then both process a
and process b
can execute at the same time--thereby sending messages at the same time.
The easiest way to understand concurrent programming is when you need to request a bunch of webpages. You begin by creating a function that requests one webpage. Then you call that function a bunch of times.
download("http:/www.xyz.com")
download("http:/www.abc.com")
...
...
download("http:/www.ghf.com")
Well, waiting for a response from a web server takes ages compared to computer processing speeds, so your code sits there doing nothing while it waits for the server to send the response back for the first request, then the second request is sent, and once again your program has to sit there waiting for a response from the web server, etc., etc.
With concurrent programming, you can send all the requests in rapid fire and then your code essentially only waits once for a response, and then your code can get busy doing something else. But note that for concurrent programming to be faster, there has to be waiting involved when your program executes serially.
In your problem description, you mention that certain entities need to communicate with each other. How? By sending erlang messages with !
? If so, then your problem description requires that a,b,c,d,e all be separate processes. Because erlang message passing is essentially like executing some simple arithmetic, passing messages at the same time won't be much more efficient than sending messages one after the other.
Upvotes: 1