Assignment 2 - Missouri University of Science and Technology

Document Sample
Assignment 2 - Missouri University of Science and Technology Powered By Docstoc
					Assignment #2 - Mutual Exclusion
This project introduces you to parallel processing and the idea of protecting
critical areas of code from synchronization problems with mutex locks.

Your assignment is to create a banking system, with threads that simulate the
Sanders family accessing two accounts(checking and savings). The Sanders
family consists of three people:

Bruce Sanders(the father), a freewheeling biker who is gone for long periods
of time on drinking binges but who also holds the patents on a large number
of devices like hooks, suction cups, and glass. He spends a lot and makes a

Jennifer Sanders(the mother), a home maker who doesn't contribute anything
but only takes a very small amount at a time, she also keeps track of the
finances and makes sure there's enough money in the savings account.

Jill Sanders(the daughter), who is away at college and therefore constantly
drains financial resources.

To clarify, you're going to define three threads that imitate the members of
the sanders family, and their accesses to those bank accounts. Each person's
behavior will be controlled by three text files which we supply to you. Your
program should take these filenames as command line arguments.

The text file inputs are available on the internet at,

Their form is as follows:
character account amount ( example: d 1 50)
   character can be d=deposit, w=withdraw, and t=transfer
   account can be 0=checking 1=savings (for transfer, this denotes the
account you want to transfer to)
   amount is a numeric value.

Notes: You'll have to open the file, probably using ifstream. A good way to
do this would be read the entire file into a string stream, then pop out
chars and ints as you need them. Since you can guarantee the input is
formatted this way, error checking won't be necessary. That is, you can
always expect well formed groups of (char, int, int) in order to parse this

Threads are initialized with a function to run. In this case, you should
define a user function which reads in a file, then performs the actions
encoded in the file. Unlike calling other functions, threaded functions can
only accept a void pointer as their arguments. In the case of a single bit
of information, this works fine: you can merely pass the pointer, then recast
it inside the thread. If you need to pass multiple bits of information, then
you need to pass it as a struct, then recast it as a struct later. Each
thread will probably need to know what filename it is parsing, and which
thread it is.

You can see for
more info.

If the user deposits, withdraws, or transfers, we should see a message to
that effect. (e.g., "Bruce deposited x dollars. New account total: y")

You'll need to write the deposit, withdrawal, and transfer functions and
include mutex protection in any 'critical' areas. Obviously, neither party
should be able to access their bank account while the other is modifying it.
For example, if Bruce and Jill issue 'Withdraw 50' at the same time, when the
bank balance is exactly 50 dollars, only one user should be able to withdraw
that money. However, if one user is depositing/withdrawing in one account,
the other account should still be modifiable. Finally, the transfer logic
will need to acquire locks on both accounts(obviously).

In order to make threads execute in an interleaved manner, they should
voluntarily block themselves which can be done by calling usleep() for a
random amount of time after each critical section (to sleep 2 seconds, you
need to use 2000000 microseconds)

Withdraw / Overdraw logic: Because we're dealing with a college student,
overdraws are likely. Let us define overdraw behavior as follows

if(w from checking and wamount > checkingbalance) get money from savings to
cover it(initiate a transfer)
if(w or t from saving and wamount > savingsbalance and savingsbalance is +)
allow balance to go negative.
if(w or t from saving and savingsbalance is -) deny action, print warning
if(t from checking and wamount > checkingbalance and checkingbalance > 0)
allow balance to go negative, print warning.
if(t from checking and checkingbalance <0 ) deny action, print warning

When you compile, remember to use the -lpthread options as well as -
D_REENTRANT for Linux systems.

Sample run:
[ercal:/nethome/users/ercal]$./assignment2 father mother daughter
 Jennifer:You do not have 37 dollars in checking to transfer.
Bruce:14 dollars deposited in savings. New Balance: 14
Jill:Withdrawal of 30 failed. You only have 14 dollars available in savings.
Jennifer:14 dollars has been transferred from savings to checking.
Jennifer:You do not have 49 dollars in savings to transfer.
Bruce:14 dollars deposited in checking. New balance: 28
Bruce:32 dollars deposited in checking. New balance: 60
Jill:Withdrawal of 13 successful. 47 dollars remain in your checking
account.User 2:33 dollars has been transferred from checking to savings.
Jill:Withdrawal of 5 successful. 28 dollars remain in your savings account.
Jennifer:10 dollars has been transferred from checking to savings.