Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- init_acc() ->
- spawn(u8, konto, [0]).
- send_queries(Server, Amount, From, To) ->
- From ! {transfer_to, Amount, Server},
- To ! {transfer_from, Amount, Server}.
- send_commit(From, To) ->
- From ! {commit},
- To ! {commit},
- {success, transaction_successfully_commited}.
- send_abort(From, To) ->
- From ! {abort},
- To ! {abort},
- {failure, transaction_successfully_failed}.
- divine_intervention(Amount, To) ->
- To ! {transfer_from, Amount, self()},
- receive
- {ready} -> To ! {commit}
- end,
- get_balance(To).
- get_balance(Konto) ->
- Konto ! {balance, self()},
- receive
- Balance -> Balance
- end.
- transfer(Amount, From, To) ->
- send_queries(self(), Amount, From, To),
- receive
- {failed} ->
- {error, account_failure};
- {ready} ->
- receive
- {failed} -> {error, account_failure};
- {ready} ->
- send_commit(From, To),
- B1 = get_balance(From),
- B2 = get_balance(To),
- io:format("Konto 1: ~w~nKonto 2: ~w~n", [B1, B2])
- end
- end.
- konto(Balance) ->
- receive
- {transfer_to, Amount, PID} ->
- if
- Amount =< Balance ->
- PID ! {ready},
- transfer_to(Balance, Amount);
- Amount > Balance ->
- PID ! {failed},
- transfer_to(Balance, 0)
- end;
- {transfer_from, Amount, PID} ->
- PID ! {ready},
- transfer_from(Balance, Amount);
- {balance, PID} -> PID ! Balance,
- konto(Balance)
- end.
- transfer_to(Balance, Amount) ->
- receive
- {commit} ->
- konto(Balance - Amount);
- {abort} ->
- konto(Balance)
- end.
- transfer_from(Balance, Amount) ->
- receive
- {commit} ->
- konto(Balance + Amount);
- {abort} ->
- konto(Balance)
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement