Percent @author Rolong<rolong at vip dot qq dot Com>-module (Process1).-compile (Export_all).-define (I (F), Io:format (f++ "~n", [])).-define (I (F, a), Io:format (f++ "~n", a)). Percent usage:%%%% START process: Pid = Process1:start (fun). Percent Fun = fun1 | Fun3 | Fun3.%%%% Send Message Pid! message.%% Message = term ()-Start (fun)-spawn (? Module,fun,[]). Percent '--------fun--------percent ' fun1 process immediately ends with erlang:is_process_alive (Pid) Check if the process exists fun1 ()? I ("I ' M fun1.~n").%%.%% ' fun2 process received a message after the end of the percent with the erlang:is_process_alive (PID) Check whether the process has a percent sent message: Pid! Messagefun2 (), receive MSG? I ("I ' M fun2, received:p", [MSG]) end. %%.%% ' FUN3 process does not end the percent end method: percent exit (Pid, Reason) Fun3 (), receive MSG? I ("I ' M fun3, received:p", [MSG]), percent callback itself fun3 () end.%%.%% ' fun4 add End method The percent process receives stop message after end of percent end method: percent 1, exit (PID, Reason) percent 2, pid! Stopfun4 (), receive stop? I ("Fun4 received stop."), percent no longer callback itself, process end Ok Msg? I ("I ' M fun4, received:p", [MSG]), percent callback itself fun4 () end.%%.%% ' fun5 increase ask function percent Ask method: Pid! Whofun5 (), receive who? I ("I ' M fun5~n"), Fun5 (); Stop-OK; Msg? I ("I ' M fun1, receive:p", [MSG]), fun5 () end.%%.%% ' fun6 process will not process any messages other than stop processing a percent unhandled message accumulates in the process message queue with ERLANG:PR Ocess_info (Pid) View process information fun6 (), receive stop, OK end.%%.%% ' fun7 increase timeout processing, expire after timeout process in 8 seconds without receiving any messages in the end fun7 ()- > Receive MSG? I ("I ' M fun7, receive: ~p", [MSG]), FUN7 () after 8000? I ("I ' m fun7, timeout, stop.~n") end.%%.%% ' fun8 increase timeout processing, time-out does not end after the percent process does not receive any messages within 8 seconds of the end of Fun8 (), Receive MSG ? I ("I ' M fun8 (~w), receive: ~p", [Self (), MSG]), Fun8 (). I ("I ' M fun8 (~w), 5s elapsed, waiting for you ...", [Self ()]), FUN8 () End.%%.%%.fun end%% vim:set Filetype=erlang foldmarker=%% ', percent. Foldmethod=marker:
Getting Started with Erlang: process instance operations and Exercise 1