<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="">Conflating the function (or even more often, module) and the process is a common mistake in the beginning. When my fellow programmers are experienced, but not used to process oriented programming, I have found that it helps to put the code that is run in different process in different modules. Ex: one module with the interface functions to a gen_server and one module for the gen_server callbacks. Seasoned Erlang programmers will tell you this is not the OTP way, or a waste of modules. So only do this until you have a better understanding.<br class=""><br class="">bengt<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 10 Mar 2019, at 04:57, 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 Bengt,<div class=""><br class=""></div><div class="">Thank you for the idea, <br class=""><br class="">I've implement your idea, from performance side, i think using two version of function will have better performance because there's no need to checking using IF statement.</div><div class=""><br class=""></div><div class="">I worry at first to implement this idea, because i think different version of function will have different PID, hahaha it turns out exactly have same PID. <br class="">this is  my new code <a href="https://pastebin.com/ECf3jhZ2" class="">https://pastebin.com/ECf3jhZ2</a> . Please review if there's any improvement needs. </div><div class=""><br class=""></div><div class="">Thank you </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><br class=""></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Min, 10 Mar 2019 pukul 05.23 bengt <<a href="mailto:cean.ebengt@gmail.com" class="">cean.ebengt@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"><div style="overflow-wrap: break-word;" 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 class=""><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" target="_blank" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="gmail-m_1841625249326328505Apple-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_1841625249326328505gmail-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-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_1841625249326328505gmail-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-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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-m_1841625249326328505gmail-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" target="_blank" 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" target="_blank" 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" target="_blank" class="">erlang-questions@erlang.org</a><br class=""><a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" target="_blank" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></body></html>