Basic concurrency Functions in Erlang
1) Pid =spwan (Mod,func,args) creates a new process to perform the Apply (Mod,func,args) and runs in parallel with the calling process, using the latest code definition module.
2) pid! Message sends message,! to PID process asynchronously For the Send operator
3 Receiving ... end receive message
Copy Code code as follows:
Receive
Pattern1[when guard1]-> Expression1;
pattern2[whenguard2]->expression2;
...
Aftertime->
Expressions
End.
The built-in function Erlang:system_info (Process_limit) finds the maximum number of processes allowed, and defaults to 262144.
The built-in functions registered by the process are:
Register (ANATOM,PID) with name registration Pid
Uregister (ANATOM) Logoff Association Registration
Whereis (anatom)->pid|undefined Check whether the Pid is registered
Registered ()->[anatom::atom ()] Returns a list of all registered processes in the system.
Concurrent Program Templates:
Copy Code code 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 the loop () is called again, a process called tail recursion, which can be cycled without consuming stack space.
The error handling of Erlang concurrency is based on remote monitoring and processing errors, focusing on remediation rather than prevention, with almost no defensive code, and only after the error cleans up the system's code, allowing other processes to fix errors and allow them to crash.
the benefits of a program crashing immediately when an error occurs:
1 Do not write defensive code, the direct collapse of simple
2 others to repair
3 does not worsen the error
4 The first time to raise the flag signal
5 repair without worrying about the cause of the heavy cleaning
6 simplifies the system architecture
Monitoring is similar to connection, but monitoring is one-way, and if a monitored process hangs, it sends a "downtime" message to the monitoring process instead of an exit signal. The basic error handling functions are:
Copy Code code 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 model: Distributed Erlang and socket based distributed models. Distributed Erlang runs on trusted networks, usually on the same LAN cluster, and is protected by firewalls. The distributed model based on socket is based on TCP/IP unreliable network.
The main problem with distributed Erlang is that the client can decide to split a variety of processes on the server, for you have all the 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 4369 ports are developed for both TCP and UDP, the port is reserved for EPMD (Erlang port mapping daemon)
2 Select 1 or a contiguous port for distributed Erlang to ensure that these ports are open, for example:
Copy Code code as follows:
$erl-name ...-setcookie ...-kernelinet_dist_listen_min min \
Inet_dist_listen_maxmax
RPC provides a number of remote invocation services, and functions in global can be used to register names in distributed systems and maintain a fully connected network.
Erlang clustering is a set of interconnect nodes with the same cookie. Three ways to create cookies:
1 storing the same cookie in the file $home/.erlang.cookie
2 when Erlang is started, you can use –setcookie, such as
Copy Code code as follows:
$erl-setcookieabcdefg2048
3 built-in function Erlang:set_cookie (node (), C) specified in the program
Erlang communicates with an external program through an object named port, and if a port sends a message, the message is sent to an external program that is connected to the port, and the message from the external program becomes the Erlang message from the port. The process of creating a port becomes a connected process of the port, all messages destined for the port must indicate the PID of the connected process, and all messages from the external program will be sent to the connected process.