<div dir="ltr">I need to make my own function my_spawn(M,F,A) which work similar to spawn(M,F,A) but if a spawned process dies, I need to print a message telling how long a process lived.<div><br></div><div><br></div><div>My attempt looks like  </div><div><br></div><div><div>-export([my_spawn/3]).</div><div><br></div><div>my_spawn(Mod, Func, Args) -></div><div>  Pid = spawn(Mod, Func, Args),</div><div>  Ref = monitor(process, Pid),</div><div>  Start = os:timestamp(),</div><div>  receive</div><div>    {'DOWN', Ref, process, Pid, _Why} -></div><div>      io:format("process died after ~p microseconds.~n", [timer:now_diff(os:timestamp(), Start)])</div><div>  end,</div><div>  Pid.</div></div><div><br></div><div><br></div><div>so when I run this, I see  </div><div><br></div><div><div>1> Pid = error_handling:my_spawn(area_server1, area, []).</div><div>process died after 363 microseconds.</div><div><br></div><div>=ERROR REPORT==== 12-Jan-2015::15:06:53 ===</div><div>Error in process <0.33.0> with exit value: {undef,[{area_server1,area,[],[]}]}</div><div><br></div><div><0.33.0></div><div>2> </div></div><div><br></div><div><b>Problems</b><b>?</b></div><div>- I am learning both Erlang and concurrency for the first time, so I see following potential problems with this approach</div><div><br></div><div>a.) What if spawned process failed in the first place, so I get no Pid</div><div>b.) What if command fails executing either "Ref = .." or "Start = .."?</div><div><br></div><div>In such scenarios the program will not work effectively, what are the recommended ways to solve such thing?</div><div><br></div><div>Also, do you see any potential race conditions here? I take a guess saying no because even if multiple process trying to execute the same code, they will return different Pid anyway, is that understanding correct?</div><div><br></div><div>Thanks a lot</div><div>+ Harit</div></div>