Starting from:
$35

$29

Homework #5 Solution




It's that spooky time of year when the dead walk the Earth, things go bump in the night, and the Centers for Disease Control (CDC) recognizes the need for zombie preparedness (http://www.cdc.gov/phpr/zombies.htm).




This year it is even more spooky due to the state government budget shortfall, so no anti‐zombie shots this year. Expect zombies to be running amok at a town near you!




Since Congress cannot balance their budget, we will show them




how by writing a program that performs many parallel bank account updates WITHOUT a lost update!




Write a C/C++ program that uses the PTHREAD library to create 5 threads and mutexes to synchronize them. The program must do the following:




 
The parant may initialize the mutexes, then call the 5 threads. You may want to detach the threads once they are created.




 
Each thread will open its own command file (data1.in, data2.in, etc) for reading. The file format will be:




R

+/‐N

+/‐N Note that there may be many +/‐N lines

W




R indicates the start of a critical section ‐ where you will acquire the account balance from global storage.




+/‐N indicates a deposit (+N) or withdraw (‐N) from the account.




W indicates the end of the critical section ‐ where you will write




the accout balance to global storage. At this point each thread must




also display the text:




Account balance after thread *thread_name_here* is $xxx




NOTES:

‐‐‐‐‐‐

Start with a budget of 0 (zero).




Each input file may be of different length and you may simply allow the thread to die at that point. Hence you may not want to have the parent employ a pthread_join.




The result of the single account should NOT contain any lost update. That is, the results should be the same as if a single thread processed all 5 input files on the single account (hint, for testing). However, you CANNOT force the threads into any serial execution. Once spawned they must be free from external influences.




I suggest you include a sleep() function in each thread to make sure each thread runs long enough to get the random update behavior required.




REQUIREMENTS:

‐‐‐‐‐‐‐‐‐‐‐‐‐

 
Your program must run in Streibel 109/115.




 
Your full name must appear as a comment at the beginning of your program.



 
Your source code must be named hw5‐yourname.c or hw5‐yourname.cpp




 
Email your source (subject hw5‐yourname) to rmarsh@cs.und.edu


























































































































More products