2016-02-19 165 views
0

我正在寫一個程序,它將採取兩個字符串並將它們連接爲共享的Dropbox刺激。我使用的是其他應用程序的代碼,它與聯合銀行帳戶做了類似的事情,所以這些錯誤可能是因爲我沒有正確更改某行代碼,但我無法弄清楚什麼是錯誤的。Erlang註冊錯誤

該代碼被寫入兩個單獨的文件,它們鏈接在一起,基本Dropbox是第一個,然後鏈接的代碼,並顯示答案如下。

-module(dropbox). 
-export([account/1, start/0, stop/0, deposit/1, get_bal/0, set_bal/1]). 

account(Balance) -> 
    receive 
     {set, NewBalance} -> 
      account(NewBalance); 
     {get, From} -> 
      From ! {balance, Balance}, 
      account(Balance); 
     stop -> ok 
    end. 

start() -> 
    Account_PID = spawn(dropbox, account, [0]), 
    register(account_process, Account_PID). 

stop() -> 
    account_process ! stop, 
    unregister(account_process). 

set_bal(B) -> 
    account_process ! {set, B}. 

get_bal() -> 
    account_process ! {get, self()}, 
    receive 
     {balance, B} -> B 
    end. 

deposit(Amount) -> 
    OldBalance = get_bal(), 
    NewBalance = OldBalance ++ Amount, 
    set_bal(NewBalance). 

-module(dropboxtest). 
-export([start/0, client/1]). 

start() -> 
    dropbox:start(), 
    mutex:start(), 
    register(tester_process, self()), 
    loop("hello ", "world", 100), 
    unregister(tester_process), 
    mutex:stop(), 
    dropbox:stop(). 

loop(_, _, 0) -> 
    true; 
loop(Amount1, Amount2, N) -> 
    dropbox:set_bal(" "), 
    spawn(dropboxtest, client, [Amount1]), 
    spawn(dropboxtest, client, [Amount2]), 
    receive 
     done -> true 
    end, 
    receive 
     done -> true 
    end, 
    io:format("Expected balance = ~p, actual balance = ~p~n~n", 
       [Amount1 ++ Amount2, dropbox:get_bal()]), 
    loop(Amount1, Amount2, N-1). 

client(Amount) -> 
    dropbox:deposit(Amount), 
    tester_process ! done. 

這是我得到,所有我已經設法找出其他的人的錯誤,但我不完全得到這一個,所以我不知道如何解決它。

** exception error: bad argument 
in function register/2 
    called as register(account_process,<0.56.0>) 
in call from dropbox:start/0 (dropbox.erl, line 16) 
in call from dropboxtest:start/0 (dropboxtest.erl, line 5) 

我也知道這是要拿出因併發問題的錯誤,我需要表現出這些錯誤,證明了什麼問題之前,我可以修復它。有些功能還沒有從銀行程序更改,因此平衡等

回答

4

the documentationregister可以badarg失敗有很多原因:

  • 如果PidOrPort是不是現有的本地進程或端口。
  • 如果RegName已被使用。
  • 如果進程或端口已經註冊(已經有一個名稱)。如果RegName是原子undefined

在這種情況下,我懷疑這是第二個原因,因爲已經有一個名爲account_process的流程來自上一次運行。您可以嘗試重新啓動Erlang shell,或者您可以將中的spawn調用更改爲spawn_link,這會在shell發生任何錯誤的情況下導致舊進程崩潰。

+0

我知道這會很簡單!謝謝! –