SoFunction
Updated on 2025-04-08

Introduction to concurrency programs in Erlang

Basic concurrency functions in Erlang

1) Pid = spwan(Mod,Func,Args) Create a new process to execute apply (Mod,Func,Args), run side by side with the calling process, and use the latest code to define the module.
2) Pid! Message Send Message asynchronously to the Pid process! For sending operator
3) Receive … end Receive message

Copy the codeThe code is as follows:

 receive
           Pattern1[when Guard1]-> Expression1;
           Pattern2[whenGuard2]->Expression2;

         aftertime->
                   Expressions
         end.

The built-in function erlang:system_info(process_limit) can find the maximum number of processes allowed, and the default is 262144.
The built-in functions for process registration are:
register (AnAtom, Pid) register Pid with name
uregister (AnAtom) Log out of the associated registration
whereis(AnAtom)->Pid|undefined Check whether Pid is registered
registered()->[AnAtom::atom()] returns a list of all registered processes in the system.
 
Concurrent program template:
Copy the codeThe code is as follows:

-module(ctemplate).
-compile(export_all).
 
start() ->
         Spwan(?MODULE,loop,[]).
 
rpc(Pid,Request) ->
         Pid! {self(),Request},
         receive
                   {Pid,Respone}->
                            Response
         end.
 
loop(X) ->
         receive
                   Any->
                            Io:format(“Received:~p ~n”, [Any]),
                            loop(X)
         end.

Whenever a message is received, it is processed and loop() is called again. This process is called tail recursion and can be looped all the time without consuming stack space.
 
Erlang concurrent programs' error handling is based on remote monitoring and handling of errors, focusing on remediation rather than prevention, with almost no defensive code, only cleaning up the system's code after the error, that is, let other processes fix errors and let them crash.
 
Advantages of program crashing immediately when error occurs:
1) It is easy to crash directly without writing defensive code
2) Someone else will fix it
3) Will not deteriorate the error
4) Raise the flag as soon as possible
5) Don’t worry about the reason when repairing it is cleaning
6) Simplified the system architecture
Monitoring is similar to connection, but monitoring is one-way. If the monitored process fails, a "download" message will be sent to the monitoring process instead of an exit signal. The basic error handling functions are:
Copy the codeThe code is as follows:

-spec spwan_link(Fun) ->Pid
-spec spwan_monitor(Fun)-> {Pid,Ref}
-spec process_flag(trap_exit,true)
-spec link(Pid) ->true
-spec unlink(Pid) -> true
-spec erlang:monitor(process,Item) ->Ref
-spec exit(Why) -> none()

Distributed models: distributed erlang and socket-based distributed models. Distributed erlang runs on trusted networks, usually on clusters of the same LAN, and is protected by a firewall. The distributed model based on socket is based on TCP/IP untrusted network.
The main problem with distributed Erlang is that clients can split multiple processes on the server at their own discretion, suitable for you to own all machines and want to control them on a single machine. The lib_chan module allows users to explicitly control which processes their machines split.
 
To execute concurrent programs on the Internet:

1) Ensure that port 4369 is developed for both TCP and UDP, and this port is reserved for epmd (Erlang port mapping daemon)
2) Select 1 or a continuous port for distributed erlang to ensure that these ports are open, for example:

Copy the codeThe code is as follows:

$erl  -name …-setcookie …  -kernelinet_dist_listen_min Min \
           Inet_dist_listen_maxMax

Rpc provides many remote call services, and functions in global can be used to register names in distributed systems and maintain a fully connected network.

An Erlang cluster is a group of interconnected nodes with the same cookies. There are three ways to create cookies:
1) Store the same cookies in file $HOME/.
2) When Erlang starts, you can use –setcookie, for example

Copy the codeThe code is as follows:

$erl  -setcookieABCDEFG2048

3) The built-in function erlang:set_cookie(node(),C) is specified in the program

Erlang communicates with external programs through an object named port. If you want to send a message on the port, the message will be sent to an external program connected to the port, and the message from the external program will become an Erlang message from the port. The process that creates the port becomes a connected process of the port. All messages sent to the port must indicate the PID of the connected process, and all messages from external programs will be sent to the connected process.