MultiProcessing
What is a process? It is the execution of the passive collection of instructions that a computer program contains. A process contains the program code and its activity.
A process can be in different states, the send, receive and execution. Take for instance a locker room, you can store items and remove items from the locker room. Let's come up with a flow of how we can handle the execution of those steps, using Erlang processes.
First, we will create a list of items(user activity)that our locker room(A module that already exists in your program)can hold. Next will need to put/store (An already existing function in our module)the items in the locker, how do we execute that step? send a {pid msg} of the item you want to appear under the stored items, which is received in our mailbox;
store(Pid, Item) ->
Pid !
{self(), {store, item}},
receive
{Pid, Msg} ->
Msg
end.
The item is stored in our mailbox and the system now awaits any calls from the users to remove/take any item from the locker room. The item sent is matched to an already existing item if one is available if not we return an error msg.
take(Pid, Item) ->
Pid !
{self(), {take, Item}},
receive
{Pid, Msg} ->
Msg
end.
Note:
Messages in the mailbox are retrieved in the order which they are received, more like a first come first serve basis, but in a case where a certain message 2(PID2) is retrieved and (PID 1) message 1 has not been retrieved priority is given to message 2(PID 2). Therefore our mailbox now will have Msg1, Msg3, Msg 4 …..
In a case where the mailbox has too many msg’s and hence slowing down the execution of different activities, we can create a function that deletes a msg or stores it at a different location for future reference.
As you were 😎