<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Greetings,<br class=""><br class="">The error happens the second time any of the three processes call erlang:register/2. They already registered themselves the first time. If you want this structure, but not the error, you have to do the register before starting the loop. Eg, split the functions into two. One that do the register and then calls the second that does the loop.<br class=""><br class="">bengt<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 9 Mar 2019, at 14:41, I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div dir="ltr" class="">Hi Attila Rajmund Nohl,<div class=""><br class=""></div><div class="">After i implement your suggestion, i get error when execute any of function call_the_*_p()</div><div class="">for example:<br class=""><br class=""></div><div class=""><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">3> newbie:starter().</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><0.112.0></span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">4> newbie:call_the_second().</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">=ERROR REPORT==== 9-Mar-2019::20:35:15.069388 ===</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">Error in process <0.113.0> with exit value:</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">{badarg,[{erlang,register,[pidsecondp,<0.113.0>],[]},</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">         </span>{newbie,the_second_p,0,</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">                 </span>[{file,"/Users/okaprinarjaya/Oprek/Erlang-Oprek/oprek-lagi/src/newbie.erl"},</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">                  </span>{line,17}]}]}</span></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"></span><br class=""></div><div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">{<0.113.0>,"the_second_p()","Halo second p!"}</span></div></div><div class=""><br class=""></div><div class="">This is my new modified code <a href="https://pastebin.com/iYCxkkuz" class="">https://pastebin.com/iYCxkkuz</a></div><div class=""><br class=""></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Jum, 8 Mar 2019 pukul 21.03 Attila Rajmund Nohl <<a href="mailto:attila.r.nohl@gmail.com" class="">attila.r.nohl@gmail.com</a>> menulis:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" target="_blank" class="">okaprinarjaya@gmail.com</a>> ezt írta<br class="">
(időpont: 2019. márc. 8., P, 12:11):<br class="">
><br class="">
> Hi Folks,<br class="">
><br class="">
> I need your help to review my code. I create and linking 3 process manually without using recursive. And inside p1 and p2 i using IF statement to check to make sure spawning process will only once.<br class="">
><br class="">
> I mean, is part code below is a good idea?<br class="">
><br class="">
> IsPidExists = whereis(xxx),<br class="">
>   if IsPidExists =:= undefined -><br class="">
>     Pid = spawn_link(?MODULE, the_p, []),<br class="">
>     register(xxx, Pid);<br class="">
>     true -> true<br class="">
>   end,<br class="">
<br class="">
Generally this is not a good idea, there's a race condition between<br class="">
checking that the process is registered (the whereis/1 call) and<br class="">
registering the new process. A better idea is to start the process and<br class="">
let the process itself to register. If register fails, it means that<br class="">
there's already a process registered, so the just started process can<br class="">
terminate. So your code could look like something like this:<br class="">
<br class="">
starter() -><br class="">
  spawn(?MODULE, the_first_p, []).<br class="">
<br class="">
the_first_p() -><br class="">
  register(pidfirstp, self()),<br class="">
  spawn_link(?MODULE, the_second_p, []),<br class="">
  ...<br class="">
  the_first_p().<br class="">
<br class="">
Actually if the register call fails, it throws a badarg and the<br class="">
process dies automatically, simplifying the code.<br class="">
</blockquote></div>
_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></body></html>