<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="">If the intention is to always run these functions manually, one by one, then it is okay. If you want to be able to do this: <br class="">procslinked:starter(), procslinked:call_the_third().<br class="">you might get into trouble since you send init_done before you spawn the next process. Add a timer:sleep(60000) right before every spawn_link( )<br class="">and you will see the problem.<br class=""><br class="">One solution is to send init_done after spawn_link().<br class=""><div><br class="">Best Wishes,<br class="">bengt<br class=""><br class=""><blockquote type="cite" class=""><div class="">On 13 Mar 2019, at 08:46, 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,<div class=""><br class=""></div><div class="">This is my updated code: <a href="https://pastebin.com/D9SAPUG8" class="">https://pastebin.com/D9SAPUG8</a> please review again.</div><div class=""><br class=""></div><div class="">Please give me the pseudo code clue if there's more incorrectness to fix in my code.</div><div class=""><br class=""></div><div class="">And this is the output when i run my erlang program</div><div 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">Erlang/OTP 21 [erts-10.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]</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">Eshell V10.2<span class="gmail-Apple-converted-space">  </span>(abort with ^G)</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">1> c(procslinked).</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">Recompiling /Users/okaprinarjaya/Oprek/Erlang-Oprek/oprek-lagi/src/procslinked.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">/Users/okaprinarjaya/Oprek/Erlang-Oprek/oprek-lagi/src/procslinked.erl:2: Warning: export_all flag enabled - all functions will be exported</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">{ok,procslinked}</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">2> procslinked: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">Receive init_done from P1</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">Receive init_done from P2</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">Receive init_done from P3</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.91.0>,okbeud}</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">3> procslinked:call_the_first().</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.91.0>,"the_first_p()","Halo first p!"}</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> procslinked: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">{<0.92.0>,"the_second_p()","Halo second p!"}</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">5> procslinked:call_the_third().</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.93.0>,"the_third_p()","Halo third p!"}</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">6> procslinked:call_the_first().<span class="gmail-Apple-converted-space"> </span></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.91.0>,"the_first_p()","Halo first p!"}</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">7> procslinked: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">{<0.92.0>,"the_second_p()","Halo second p!"}</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">8> procslinked:call_the_third().<span class="gmail-Apple-converted-space"> </span></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.93.0>,"the_third_p()","Halo third p!"}</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">9><span class="gmail-Apple-converted-space"> </span></span></div></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></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Sel, 12 Mar 2019 pukul 02.13 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 race is that starter/0 will return before any of the three processes have started. If you add a timer:sleep( 60000 ) before the calls to register/0 you will see this even if you do call_the_first/0 manually.<br class=""><br class="">The pseudo code would be<br class="">My_pid = erlang:self(),<br class=""><span style="background-color:rgb(255,255,255)" class="">P =</span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif" class="">  </span><span class="gmail-m_-681099599529850578kw3" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(250,111,255)">spawn</span><span class="gmail-m_-681099599529850578br0" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(16,154,184)">(</span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif" class="">?</span><span class="gmail-m_-681099599529850578re4" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(105,65,253)">MODULE</span><span class="gmail-m_-681099599529850578sy1" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(107,184,16)">,</span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif" class=""> </span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif" class="">the_first_p</span><span class="gmail-m_-681099599529850578sy1" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(107,184,16)">,</span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255);font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif" class=""> </span><span class="gmail-m_-681099599529850578br0" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(16,154,184)">[</span><span class="gmail-m_-681099599529850578br0" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;background-color:rgb(255,255,255)"><font color="#333333" class="">My_pid</font></span><span class="gmail-m_-681099599529850578br0" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(16,154,184)">]</span><span class="gmail-m_-681099599529850578br0" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif;color:rgb(16,154,184)">),<br class=""></span><span class="gmail-m_-681099599529850578sy1" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace,serif">receive<br class="">{P, init_done} -> ok<br class="">end.</span><br class=""><div class=""><br class=""></div>And then sending {erlang:self(), init_done} to My_pid (the process that started the_first_p).<br class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 11 Mar 2019, at 12:03, I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" target="_blank" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="gmail-m_-681099599529850578Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="">Hi Andreas Schultz,</div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">>> The reason is that there is no guarantee that `call_the_third()` will be executed after any of the spawn has succeeded or the register was invoked.<br class=""><div class="">1. Did you mean this code? <a href="https://pastebin.com/ECf3jhZ2" target="_blank" class="">https://pastebin.com/ECf3jhZ2</a><br class="">2. and did you mean spawn_link/3 and register/2 is racy? </div><div class="">3. I've test this code many times, and never get error in each call_the_* function, even call_the_third/0 function. <br class=""><br class="">Here: <a href="https://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency" target="_blank" class="">https://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency</a>, and here: <a href="https://learnyousomeerlang.com/more-on-multiprocessing" target="_blank" class="">https://learnyousomeerlang.com/more-on-multiprocessing</a> and <br class="">here: <a href="https://learnyousomeerlang.com/errors-and-processes" target="_blank" class="">https://learnyousomeerlang.com/errors-and-processes</a> teach me to do that. Oh My God, where in this earth i can read the best erlang process tutorial ? so i can use erlang in the right way.</div><div class=""><br class=""></div><div class="">>> In order to get ordering guarantees you need to wait for the processes to be past the init stage. </div><div class="">>> The normal way to do that is to wait for a message from the init function of you child process.</div><div class="">Please give me a short pseudo-code clue to do this. I do really shocked because of this code: <a href="https://pastebin.com/ECf3jhZ2" target="_blank" class="">https://pastebin.com/ECf3jhZ2</a> still not correct after 2 times revision. </div><div class=""><br class=""></div><div class="">Thank you :)</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Sen, 11 Mar 2019 pukul 14.55 Andreas Schultz <<a href="mailto:andreas.schultz@travelping.com" target="_blank" class="">andreas.schultz@travelping.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 dir="ltr" class="">You do realize that your code is still "racy" ?<div class=""><br class=""></div><div class="">Invoking it like</div><div class=""><br class=""></div><div class="">    2> procslinked:starter(), procslinked:call_the_third().</div><div class=""><br class=""></div><div class="">has a good chance of not producing the expected result. Most times it will simply fail with:</div><div class=""><br class=""></div><div class=""><div class="">    ** exception error: bad argument</div><div class="">         in function  procslinked:call_the_third/0 (procslinked.erl, line 54)</div></div><div class=""><br class=""></div><div class="">The reason is that there is no guarantee that `call_the_third()` will be executed after any of the spawn has succeeded or the register was invoked.</div><div class="">In order to get ordering guarantees you need to wait for the processes to be past the init stage. The normal way to do that is to wait for a message from the init function of you child process.</div><div class=""><br class=""></div><div class="">BTW: this is exactly what proc_lib:init_ack does, but I guess you are trying to implement that yourself as a learning exercise.</div><div class=""><br class=""></div><div class="">Andreas<br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" target="_blank" class="">okaprinarjaya@gmail.com</a>> schrieb am So., 10. März 2019 um 23:38 Uhr:<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 dir="auto" class="">Hi Bengt,<div dir="auto" class=""><br class=""></div><div dir="auto" class="">Thank you very much for all of your hints and suggestions. It's clear now. </div><div dir="auto" class=""><br class=""></div><div dir="auto" class=""><br class=""></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Sen, 11 Mar 2019 05.20, bengt <<a href="mailto:cean.ebengt@gmail.com" target="_blank" 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 class="">I think your code is about learning erlang. Do that first. OTP is the next step.<br class=""><br class="">Yes, splitting a gen_server into callback module and interface module is not the OTP way (as described in documentation), so while good for a beginner it can be considered bad style by experienced programmers.<br class=""><br class="">Sorry, but I have not seen any tutorial about how to avoid thinking the same function/module is always in the same process. It is a very fundamental concept (them not being) in Erlang, so just keep it in mind. One way would be to try and make all your three functions the same function, just have different arguments to the loop. Then you must think about it.<br class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 10 Mar 2019, at 11:11, I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" rel="noreferrer" target="_blank" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="">Hi Bengt,<div class=""><br class=""></div><div class="">Thank you for your review :)</div><div class=""><br class=""></div><div class="">I don't quite understand with some of your thought. Then i hope you not mind to give more explanation.</div><div class=""><br class=""></div><div class="">>> Seasoned Erlang programmers will tell you this is not the OTP way, or a waste of modules</div><div class="">Did you mean my code here: <a href="https://pastebin.com/ECf3jhZ2" rel="noreferrer" target="_blank" class="">https://pastebin.com/ECf3jhZ2</a> wasn't the OTP way? <br class="">I learn from here: <a href="https://learnyousomeerlang.com/errors-and-processes" rel="noreferrer" target="_blank" class="">https://learnyousomeerlang.com/errors-and-processes</a> and i do really a beginner at programming with erlang.</div><div class=""><br class=""></div><div class="">>> So only do this until you have a better understanding.</div><div class="">What is that? Did you mean <i class="">doing one module with the interface functions to a gen_server and one module for the gen_server callbacks.?</i></div><div class="">or what?<br class=""><br class=""></div><div 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.</div><div class=""> Any tutorials / reference for me to avoid this mistake ?</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=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Min, 10 Mar 2019 pukul 14.49 bengt <<a href="mailto:cean.ebengt@gmail.com" rel="noreferrer" target="_blank" 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 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 class=""><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" rel="noreferrer" target="_blank" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271Apple-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" rel="noreferrer" target="_blank" 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" rel="noreferrer" target="_blank" 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 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" rel="noreferrer" target="_blank" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-m_1841625249326328505gmail-s1" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-m_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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_-681099599529850578gmail-m_1125207073685011277m_-1852509069998413972m_-2910958684381146787gmail-m_4133803218338393271gmail-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" rel="noreferrer" 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" rel="noreferrer" 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" rel="noreferrer" 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" rel="noreferrer" 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=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" rel="noreferrer" target="_blank" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></div>_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" rel="noreferrer" target="_blank" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></div>_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" rel="noreferrer" target="_blank" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><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" rel="noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
</blockquote></div></div></div>-- <br class=""><div dir="ltr" class="gmail-m_-681099599529850578gmail-m_1125207073685011277gmail_signature"><div dir="ltr" class=""><span style="font-family:monospace,monospace" class="">-- <br class="">Dipl.-Inform. Andreas Schultz<br class=""><br class="">----------------------- enabling your networks ----------------------<br class="">Travelping GmbH                     Phone:  +49-391-81 90 99 0<br class="">Roentgenstr. 13                     Fax:    +49-391-81 90 99 299<br class="">39108 Magdeburg                     Email:  <a href="mailto:info@travelping.com" target="_blank" class="">info@travelping.com</a><br class="">GERMANY                             Web:    <a href="http://www.travelping.com/" target="_blank" class="">http://www.travelping.com</a><br class=""><br class=""></span><div class=""><span style="font-family:monospace,monospace" class="">Company Registration: Amtsgericht Stendal        Reg No.:   HRB 10578</span></div><span style="font-family:monospace,monospace" class="">Geschaeftsfuehrer: Holger Winkelmann          VAT ID No.: DE236673780<br class="">---------------------------------------------------------------------</span></div></div>
</blockquote></div>
</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>