Goal: To be able to write a C, C++, or Java program using the shared memory programming constructs and simple mutex or synchronize statements.
Preparation:
Guidelines:
For this assignment you may share information about how to compile and execute programs in Unix. However, each of you is to write the code on your own and submit your own program for a grade.
What you turn in:
Program Specifications:
This programming assignment will involve the idea of modern-day banking using an ATM (Automatic Transaction Machine). This program works on the idea that multiple users sometimes share the same bank account. For this program, you will assume that a single account will be accessed by up to four different users. Commands that a user may want to perform to the account include adding an amount to the balance, subtracting an amount from the balance, and printing the current balance. Users will act independently and concurrently on the account. Since each user may add or subtract funds from the account, your program must use appropriate synchronization tools to ensure that the balance in the account is accurate and valid.
Write a C, C++, or Java program using the pthread library that consists of code for main, a user thread, and supporting functions as needed. The code for main must do the following:
User 1 views account balance of $970.
User 2 views account balance of $950.
User 1 views account balance of $970.
. . . and so on.
Output should only be performed for a GET command. You may assume that the files do not contain errors. When all commands from the file have been read then the thread should exit.
Grading:
For full credit, your program must run correctly on file1 only (one thread), or on file1 and file2 (two threads concurrently), or on file1 and file2 and file3 (three threads concurrently), or on all four files (four threads concurrently) on Prospero. You must follow all program specifications. Your program should be appropriately commented and formatted. This program is worth 30 points.
Extra Credit:
As specified, it is possible that the account balance can drop below zero. For up to 10 extra credit points, extend the program so that the account balance is always positive. If a user attempts to subtract money from the account in a manner that will make the balance negative, force the user to wait until the balance is large enough that the subtraction can be made safely. You may find condition variables (using pthread_cond_init, pthread_cond_lock, etc.) useful for this exercise. Try to come up with a solution that does not hang indefinitely.
Enjoy!