This project is a port of UMass's Operating Systems TAs Dave Dirnfield and Calvin Chai's C/C++ Producer/Consumer project into Rust. It is a banking application that uses threads to process transactions from a ledger file concurrently.
Rust 1.65.0+
$ cargo run <num_threads> <ledger_filepath>
id
: Theu16
identifier for thisTransaction
from_id
: Theu16
identifier of the account having its money movedto_id
: Theu16
identifier of the account receiving moneyamount
: Thef32
amount of money being movedmode
: Theenum Mode
of transaction, aDeposit
,Withdrawal
, orTransfer
accounts
: ABTreeMap
ofu16
identifiers toMutex
-lockedf32
balancesledger
: AVec
ofTransactions
num_transactions
: Theu16
number ofTransaction
s in theledger
num_succeeded
: Theu16
number ofTransaction
s that succeeded
Transaction::new(from_id: u16, to_id: u16, amount: f32, mode_id: u8) -> Transaction
Constructs and initializes a new Transaction
object
id
: The identifier for the newTransaction
objectfrom_id
: The identifier of the account having its money movedto_id
: The identifier of the account receiving moneyamount
: The amount of money being movedmode_id
: The identifier for the mode of theTransaction
, where0
,1
, and2
represent a deposit, withdrawal, or transfer, respectively.
The new Transaction
object
Bank::new(num_accounts: u16, ledger_filepath: String) -> Bank
Constructs a new Bank
object and initializes its accounts
, ledger
, num_transactions
,
and num_succeeded
num_accounts
: The number ofaccount
s to initializeledger_filepath
: The name of a ledger file containing transactions formatted<from_id> <to_id> <amount> <mode_num>
on each line
The new Bank
object
Bank::start(&mut self, num_threads: u16)
Spawns Threads
to process this Bank
's ledger
concurrently
num_threads
: The number ofThread
s to spawnarc_bank
: The atomic reference counter to thisMutex
-lockedBank
Bank::process_transaction(arc_bank: Arc<Mutex<Bank>>, thread_id: u16)
Pops a Transaction
from this Bank
's ledger
and uses a Thread
to process it concurrently
arc_bank
: The atomic reference counter to thisMutex
-lockedBank
thread_id
: The identifier of the thread processing theTransaction
Bank::deposit(&mut self, account_id: u16, amount: f32) -> bool
Deposit money into one of this Bank
's accounts
, incrementing num_succeeded
if the deposit succeeded
account_id
: The identifier of the account receiving the depositamount
: The amount of money being deposited
true
if the deposit succeeded and false
otherwise
Bank::withdraw(&mut self, account_id: u16, amount: f32) -> bool
Withdraws money from one of this Bank
's accounts
, incrementing num_succeeded
if the withdrawal succeeded
account_id
: The identifier of the account having its money withdrawnamount
: The amount of money being withdrawn
true
if the withdrawal succeeded and false
otherwise
Bank::transfer(&mut self, from_id: u16, to_id: u16, amount: f32) -> bool
Transfers money from one of this Bank
's accounts
to another, incrementing num_succeeded
if the transfer succeeded
from_id
: The identifier of the account having money transferred from itto_id
: The identifier of the account having money transferred to itamount
: The amount of money being transferred
true
if the transfer succeeded and false
otherwise