简体   繁体   中英

Erlang register error

I'm writing a program which will take two strings and concatenate them as a shared dropbox stimulation. I'm using code from a different application, which did a similar thing with a joint bank account, so the errors may be because I haven't changed some line of code properly but I just can't work out what's wrong.

The code is written in two separate files and they link together, the basic dropbox is first and then the code which links that and displays the answer is below.

-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.

This is the error which I'm getting, all of the other ones I've managed to work out but I don't quite get this one so I'm not sure how to solve it.

** 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)

Also I know that this is going to come up with errors due to concurrency issues, I need to show these errors to prove what's wrong before I can fix it. Some of the functions haven't been changed from the bank program hence balance etc.

As per the documentation , register can fail with badarg for a number of reasons:

  • If PidOrPort is not an existing local process or port.
  • If RegName is already in use.
  • If the process or port is already registered (already has a name).
  • If RegName is the atom undefined .

In this case I suspect it's the second reason, that there's already a process with the name account_process , from a previous run. You could try restarting the Erlang shell, or you could change the spawn call in dropbox:start to spawn_link , which would cause the old process to crash in case of any error in the shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM