DEV Community

Cover image for What's a process in Elixir?
Adolfo Neto
Adolfo Neto

Posted on

What's a process in Elixir?

To learn what a process is in Elixir, you must first know what a function is.

For example, IO.puts/1 is a function that writes something to the screen. The function is called puts, it is in the IO module (input I and output O), and it takes one argument:

iex(1)> IO.puts("Adolfo")
Adolfo
:ok
Enter fullscreen mode Exit fullscreen mode

If all goes well, it returns the :ok atom .
One function that lets you spawn a process is Kernel.spawn/1.

Again, the name is spawn. It's part of the Kernel module and takes one argument.

Can I do

Kernel.spawn(IO.puts("Adolfo"))
Enter fullscreen mode Exit fullscreen mode

?
No!

Let's start simple: every function that is part of the Kernel module doesn't need the module name before it.
You just call

elixir
spawn(IO.puts("Adolfo"))
`

It will still be wrong, but with fewer letters.

What Kernel.spawn/1` gets is an arity 0 function, i.e., one that takes no arguments.
How do we do that?
Like this:

fn -> 1 end
Enter fullscreen mode Exit fullscreen mode

The above function has no name (anonymous) and returns 1.

But see in the image that if you spawn it, nothing interesting happens:

Image description

First, see that

fn -> 1 end
Enter fullscreen mode Exit fullscreen mode

returned some sort of "code" that identifies the function:

#Function<43.3316493/0 in :erl_eval.expr/6>
Enter fullscreen mode Exit fullscreen mode

and

spawn(fn -> 1 end)
Enter fullscreen mode Exit fullscreen mode

returned a PID, a Process IDentifier:

#PID<0.120.0>
Enter fullscreen mode Exit fullscreen mode

I can assign this PID to a variable:

iex(1)> pid = spawn(fn -> 1 end)
#PID<0.110.0>
Enter fullscreen mode Exit fullscreen mode

And then ask if the process that was spawned is alive:

iex(2)> Process.alive?(pid)
false
Enter fullscreen mode Exit fullscreen mode

It is not alive because it was a very fast function, which only returned 1.
I can, for example, make the process "sleep" for 10 seconds before returning 1.

iex(3)> pid = spawn(fn -> Process.sleep(10000); 1 end)
#PID<0.113.0>
Enter fullscreen mode Exit fullscreen mode

If I quickly ask if the process, whose identifier is in the pid variable, is alive, the answer is yes.

iex(4)> Process.alive?(pid)
true
Enter fullscreen mode Exit fullscreen mode

But if I ask again after 10 seconds, the answer is no.

iex(5)> Process.alive?(pid)
false
Enter fullscreen mode Exit fullscreen mode

If I do this here

iex(6)> pid = spawn(fn -> Process.sleep(10000); IO.puts("Adolfo") end)
#PID<0.117.0>
Adolfo
Enter fullscreen mode Exit fullscreen mode

it will take 10 seconds for "Adolfo" to be written to the screen.

Whereas if I do the following, it will be immediate for "Adolfo" to appear on the screen.

iex(7)> pid = spawn(fn -> IO.puts("Adolfo") end)
Adolfo
#PID<0.119.0>
Enter fullscreen mode Exit fullscreen mode

Anyway, this is just the basics of the basics. Read more at
https://elixirschool.com/pt/lessons/intermediate/concurrency

Or in the Getting Started of the Elixir language
https://elixir-lang.org/getting-started/processes.html

I did all this without even mentioning send and receive.

Finally: a process in Elixir is a processing unit that performs a function.

Top comments (6)

Collapse
 
alfredbaudisch profile image
Alfred Reinold Baudisch • Edited

That's a really good, concise writeup, Adolfo. I'll follow people to this article when they ask me about this.

By the way, I came up to your post via RSS, as I am consuming both of your dev.to RSS feeds.

Collapse
 
adolfont profile image
Adolfo Neto

Thanks!

Collapse
 
adolfont profile image
Adolfo Neto

"A process is an isolated entity where code execution happens and they are the base to design software systems taking advantage of the BEAM ." erlang-solutions.com/blog/understa...

Collapse
 
vuyonyembezi profile image
Vuyo Nyembezi

In Elixir, all code runs inside processes. Processes are isolated from each other, run concurrent to one another and communicate via message passing. Processes are not only the basis for concurrency in Elixir, but they also provide the means for building distributed and fault-tolerant programs.

They are the guards of functionality they keep everything going

Collapse
 
adolfont profile image
Adolfo Neto

Thanks! Excellent comment!

Collapse
 
adolfont profile image
Adolfo Neto

"Multiple processes or threads on a single time-sliced CPU might exhibit concurrency but not parallelism. Parallelism is about multiple tasks that literally run at the same time on hardware with multiple computing resources like a multi-core CPU." https://blog.finiam.com/blog/how-to-build-concurrent-and-resilient-service-in-elixir?utm_medium=email&utm_source=elixir-radar