erlang并发编程

1.erlang中创建进程(非操作系统线程,比其要轻量很多)非常方便,运用spawn函数即可 spawn(Fun) -> pid() spawn(Node, Fun) -> pid() spawn(Module, Function, Args) -> pid() 使用eg:spawn(io,format,["123"]). 123<0.45.0> ------------------------------------------------------------------------------------------- 2.erlang原生方法中的erlang模块有很多可以查看进程的方法: erlang:system_info(process_count), erlang:memory(processes) 查看进程的相关信息 erlang:process_info(Pid) -> InfoResult Types: Pid = pid() Item = atom() Info = term() InfoTuple = {Item, Info} InfoTupleList = [InfoTuple] InfoResult = InfoTupleList | undefined 使用eg: {memory,Mem}=erlang:process_info(H,memory), {current_function,{Module, Function, Args}}=erlang:process_info(H,current_function), erlang:process_info(Owner, registered_name) ------------------------------------------------------------------------------------------- 3.进程之间通过消息进行信息交换,消息发送的字符是! run() -> Pid =spawn(fun girOnly/0), Pid ! self(), %%1.给自己发信息,发送自己的进程pid receive sir -> no; madam -> ok %%3.收到madam,就返回ok end. girOnly() -> receive From -> From ! madam %%2.接收到的是地址pid,就发送给自己madam end. ------------------------------------------------------------------------------------------- 4.进程终止&监督&退出信息捕捉 进程是相互独立的,进程崩溃会产生一个退出信息,与之链接的进程都会相应的接收到崩溃消息并一起退出。但是我们希望进程退出的时候做相应的处理: OTP实现容错的主要途径之一就是改写退出信号默认的传播行为。通过设置trp_exit进程标记,可以使进程不再服从外来的退出信号,而是将之捕捉。 这种情况下,进程接收到信息后会先将其转为{'Exit',Pid,Reason}的消息,然后这条消息会像普通消息一样被丢入信箱,捕捉到信号的进程就能分类进行处理了。 这类进程有时被称为系统进程。该类进程阻断了与之链接的其他进程和外界之间的联系,因而可以汇报故障&重启故障的子系统,我们称之为“监督者”。 -module(xml_server). -behaviour(gen_server). -export([start_link/0,client_port/0]). -export([init/1,handle_call/3,handle_cast/2,handle_info/2,code_change/3,terminate/2]). start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). init([]) -> process_flag(trap_exit, true), 。。。 {ok, []}. elang的进程链接与监督者共同提供了一种“重启”机制。OTP允许监督者按预设的方式和次序来启动进程。我们还可以告知监督者如何在单个进程故障时重启其他 进程、一段时间内尝试重启几次后放弃重启等。 ------------------------------------------------------------------------------------------- 5.OTP提供的监督者行为 (四种重启策略:) one_for_one,simple_one_for_one :没有依赖(只启动这个)不过它们的集体失败会导致整个application的shutdown rest_for_one:线性依赖的进程,1->2->3->4->5, 2死了,会启动2,3,4,5,不会启动1; one_for_all:完全相互依赖的进程。 Module:init(Args) -> Result Types: Args = term() Result = {ok,{{RestartStrategy,MaxR,MaxT},[ChildSpec]}} | ignore ?RestartStrategy = one_for_all | one_for_one | rest_for_one | simple_one_for_one ?MaxR = MaxT = int()>=0 ?ChildSpec = child_spec() child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules} Id = term() StartFunc = {M,F,A} M = F = atom() A = [term()] Restart = permanent | transient | temporary Shutdown = brutal_kill | int()>=0 | infinity Type = worker | supervisor Modules = [Module] | dynamic Module = atom() 使用eg: -module(chat_supervisor). -behaviour(supervisor). -export([start/0,init/1]). start() -> {ok,Pid} = supervisor:start_link({local,?MODULE},?MODULE,[]), unlink(Pid), {ok,Pid}. init([]) -> {ok,{ {one_for_one,3,3600}, [ %数据库管理进程 {spec_chat_data1,{chat_data,start,[]},permanent, brutal_kill, worker,[chat_data]}, %服务端监控进程 {spec_chat_server1,{server,start_link,[]},permanent,brutal_kill,worker,[server]} ]}}.
原文地址:https://www.cnblogs.com/weidongprefer/p/6637874.html