<div dir="ltr"><div dir="ltr">Hi Attila Rajmund Nohl,<div><br></div><div>After i implement your suggestion, i get error when execute any of function call_the_*_p()</div><div>for example:<br><br></div><div>





<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">3> newbie:starter().</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><0.112.0></span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">4> newbie:call_the_second().</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">=ERROR REPORT==== 9-Mar-2019::20:35:15.069388 ===</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">Error in process <0.113.0> with exit value:</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">{badarg,[{erlang,register,[pidsecondp,<0.113.0>],[]},</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">         </span>{newbie,the_second_p,0,</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><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></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">                  </span>{line,17}]}]}</span></p>
<p class="gmail-p2" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:13px"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">{<0.113.0>,"the_second_p()","Halo second p!"}</span></p></div><div><br></div><div>This is my new modified code <a href="https://pastebin.com/iYCxkkuz">https://pastebin.com/iYCxkkuz</a></div><div><br></div></div></div><br><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">attila.r.nohl@gmail.com</a>> menulis:<br></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">okaprinarjaya@gmail.com</a>> ezt írta<br>
(időpont: 2019. márc. 8., P, 12:11):<br>
><br>
> Hi Folks,<br>
><br>
> 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>
><br>
> I mean, is part code below is a good idea?<br>
><br>
> IsPidExists = whereis(xxx),<br>
>   if IsPidExists =:= undefined -><br>
>     Pid = spawn_link(?MODULE, the_p, []),<br>
>     register(xxx, Pid);<br>
>     true -> true<br>
>   end,<br>
<br>
Generally this is not a good idea, there's a race condition between<br>
checking that the process is registered (the whereis/1 call) and<br>
registering the new process. A better idea is to start the process and<br>
let the process itself to register. If register fails, it means that<br>
there's already a process registered, so the just started process can<br>
terminate. So your code could look like something like this:<br>
<br>
starter() -><br>
  spawn(?MODULE, the_first_p, []).<br>
<br>
the_first_p() -><br>
  register(pidfirstp, self()),<br>
  spawn_link(?MODULE, the_second_p, []),<br>
  ...<br>
  the_first_p().<br>
<br>
Actually if the register call fails, it throws a badarg and the<br>
process dies automatically, simplifying the code.<br>
</blockquote></div>