Going through this link
To identify a process,we would be using a Pid. When should a ref be used?
I see this often while message sending/receiving but unable to interpret what role it plays in message interaction.
spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list.
Erlang has something called Process Dictionary which is a sort of Key/Value store that belongs to each Erlang process. We can use it to store values in a simple manner inside Erlang programs while avoiding the limitations of Pure Functional Programming. The anti-flames suit is sold separately tho.
A process can terminate itself by calling one of the BIFs exit(Reason), erlang:error(Reason), erlang:error(Reason, Args), erlang:fault(Reason) or erlang:fault(Reason, Args). The process then terminates with reason Reason for exit/1 or {Reason,Stack} for the others.
This is called as is_process_alive(Pid). A Pid must refer to a process at the local node. It returns true if the process exists and is alive i.e., whether it is not exiting and has not exited. Otherwise, returns false.
To quote documentation
A reference is a term which is unique in an Erlang runtime system, created by calling make_ref/0.
This means that this is special data type, it's not an integer, not a list, and not a binary. Especially with unique
prosperity. It's designed mostly to recognize some places in code. make_ref()
, no matter where it is called, will return new value (in boundaries of it's size of course). And just like Fred describes in it's book, it is great for tagging messages, and recognizing if message we receive is in response to one we just send.
In complement to other responses, beware that a ref:
{node(),make_ref()}
.No discrepancy with the documentation, but I made some wrong assumption in the past ... :o). Thanks @Robert for correction!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With