Question: Implement a multi-threaded bank simulation using the Pthreads package under the Linux operating system. You may implement your project using either C or C++. Scenario

Implement a multi-threaded bank simulation using the Pthreads package under the Linux operating system.

You may implement your project using either C or C++.

Scenario

A set of N customers have accounts at the local bank, each funded with 10000 dollars. The bank has T tellers, and the customers wait in a single line for the next available teller. If multiple tellers are available, the one with the smallest teller ID (index) is chosen. Once a customer gains access to a teller, that customer initiates between 1 and L transactions. Half of these transactions are deposits of between .01 and one thousand dollars, one quarter are withdrawals of between .01 and one thousand dollars, and one quarter are transfers of money from the customer's account to another account at the bank. If a withdrawal or transfer would cause a customer's balance to fall below zero, it is aborted. After being serviced, the customer immediately gets back in line to be serviced again.

The simulation continues for D days. Exactly N transactions occur during each day. For simplicity's sake, we assume all customers wait patiently overnight until the bank opens again, when they continue making transactions. At the end of each day the bank calculates the interest earned by each of the N accounts by 3*balance/10000; this interest is added to the account immediately. The simulation must keep track of the net transactions (withdrawals deposits) for each teller, total interest paid, the net cash in pocket for each customer (withdrawals deposits), and, of course, the account balance for each customer.

Implementation

Each customer will be implemented as a pthread with system-wide scope of contention. The simulation will be invoked with 4 command line arguments: N, D, L and T. If there are not 4 command line arguments, or if the 4 arguments are not positive integers, the simulator should print a usage message, describing the proper number and purpose of command line parameters, to standard error and end. Otherwise, the initial thread should use the command line values to initialize the data structures needed for the simulation, and then create N threads representing the customers.

The initial thread then waits until N transactions have occurred, and then calculates the interest for each account and updates the account balances. The tellers are not represented as threads; they are resources that threads need to acquire to initiate transactions. At the end of the last day of the simulation the initial thread prints account balance and net change in pocket cash for each customer, net transactions for each teller, and 4 totals: total account balance, total net change in pocket cash, total net teller transactions, and total interest paid.

You will need to use mutexes and (possibly) condition variables to synchronize access to the various resources in the system. A mutex for the initial waiting line would make sense; the various accounts and statistics need to be protected from a race condition; interest calculations must be made after the Nth transaction of the day, before subsequent transactions can occur.

Customers

The code for a customer is given below: Assume, the command line parameters N, D, L and T are stored in global variables of the same name.

while (true) { transCount = rand()%L + 1; for (int i = 0; i < transCount; i++){ transCode = getTransactionType(); transAmount = getTranactionAmount(); doTransaction( transCode, transAmount, accountArray, pocketArray, tellerArray, CID, TID); } }

int getTransactionType(){ int temp = rand()%4; if (temp == 3) temp = 2; return temp;// 0 = transfer, 1 = withdrawal, 2 = deposit }

double getTransactionAmount(){ return (rand()%100000)/100.0; }

void doTransaction( int transCode, double transAmount, double* accountArray, double* pocketArray, double* tellerArray, int CID, int TID){ int target = -1; double account, pocket, netAmount, teller, targetAccount; target = rand() % N; //you may insert synchronization code here

pocket= pocketArray[CID]; account= accoutArray[CID]; teller= tellerArray[TID]; targetAccount= accoutArray[target]; if(transCode > 0){ netAmount = (2 * transCode - 3) * transAmount; if((accountArray[CID] + netAmount) > 0){ pocket = pocketArray[CID] - netAmount; account = accountArray[CID] + netAmount; teller = tellerArray[TID] + netAmount; } } else if((accountArray[CID] - transAmount) > 0){ account = accoutArray[CID] - transAmount; targetAccount = accountArray[target] + transAmount;

} sleep(2); //delay if(transCode > 0){ pocketArray[CID] = pocket; accoutArray[CID] = account; tellerArray[TID] = teller; } else{ accoutArray[CID] = account; accoutArray[target] = targetAccount; } //you may insert synchronization code here

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!