English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Processes

The granularity of concurrency in Erlang is a process. A process is an activity that runs concurrently with other processes and is independent of other processes./Tasks. These processes in Erlang are different from the processes and threads that most people are familiar with. Erlang processes are lightweight, run in isolation from other processes (memory), and are scheduled by Erlang's virtual machine (VM). The creation time of a process is very short, and the memory usage of a newly generated process is very small. A single Erlang VM can run millions of processes.

Create a process using the spawn method. The general syntax of this method is as follows.

Syntax

spawn(Module, Name, Args)

Parameters

  • Module −This is the predefined atom value, which must be ?MODULE.

  • Name −This is the name of the function to be called when defining the process.

  • Args −These are the parameters that need to be sent to the function.

Return Value

Returns the process ID of the newly created process.

The following program demonstrates an example of the spawn method.

-module(helloworld). 
-export([start/0, call/2]). 
call(Arg1, Arg2) -> 
   io:format("~p ~p~n", [Arg1, Arg2]). 
start() -> 
   Pid = spawn(?MODULE, call, ["hello", "process"]), 
   io:fwrite("~p",[Pid]).

The following points should be noted about the aforementioned program.

  • A function called call is defined, which will be used to create a process.

  • The spawn method uses the parameters hello and process to call the call function.

Output Result

When we run the above program, we will get the following results.

<0.29.0>"hello" "process"

Now, let's look at other functions available for processes.

Serial NumberMethods and Descriptions
1

is_pid

This method is used to determine if a process id exists.

2

is_process_alive

This is called is_process_alive(Pid). A Pid must refer to a process on the local node.

3

pid_to_list

It converts a process id to a list.

4

registered

Returns a list containing the names of all registered processes.

5

self

One of the most commonly used BIFs, it returns the pid of the calling process.

6

register

Used to register a process in the system.

7

whereis

It is called whereis (Name). It returns the pid of the process registered with that name.

8

unregister

Used to unregister a process from the system.