Quixir/Processes

From Elixir Wiki
Jump to navigation Jump to search

Quixir/Processes[edit]

File:Elixir Logo.png
Elixir Logo

Processes play a vital role in the Quixir programming language. In Quixir, processes are lightweight and isolated units of execution. They provide concurrency, fault tolerance, and distribution capabilities to build highly scalable and fault-tolerant systems.

Introduction[edit]

A process in Quixir is a unit of computation that runs concurrently with other processes. Each process has its own independent memory space, known as a heap, where it stores its variables and data. Processes communicate with each other by sending and receiving messages, allowing for easy coordination and cooperation.

Creating Processes[edit]

In Quixir, processes are created using the `spawn/1` or `spawn/3` functions. The `spawn/1` function takes a function as an argument and spawns a new process to execute that function. The `spawn/3` function is similar but also allows specifying the module and function name to be executed.

Example: ```elixir pid = spawn(fn ->

 IO.puts "Hello from a new process!"

end) ```

Sending and Receiving Messages[edit]

Processes communicate by sending and receiving messages using the `send/2` and `receive/1` functions. The `send/2` function sends a message to a specified process, identified by its process identifier (PID).

Example: ```elixir pid = spawn(fn ->

 receive do
   message ->
     IO.puts "Received: #{inspect message}"
 end

end)

send(pid, "Hello!") ```

Process Linking[edit]

In Quixir, processes can be linked together to establish fault-tolerant systems. Linking two processes results in a bi-directional connection where a failure in one process will cause an exit signal to be sent to the linked process.

Example: ```elixir pid = spawn(fn ->

 receive do
   {:shutdown, reason} ->
     IO.puts "Received shutdown signal: #{reason}"
     Process.exit(self(), :shutdown)
 end

end)

Process.link(pid) ```

Supervision[edit]

Supervision is a mechanism in Quixir for building fault-tolerant systems. Instead of focusing on handling individual failures, supervision strategies allow for the monitorization and supervision of a group of related processes.

References[edit]

Template:Elixir