Goal: To be able to write a C, C++, or Java program using
the shared memory programming constructs
Preparation:
- Study the class notes on how to use the pthread
library calls, including: pthread_create,
pthread_join,
pthread_exit,
pthread_mutex_init, pthread_mutex_lock,
pthread_mutex_unlock, and
calls using condition variables.
- If you want to write your code using Java then you will need to read
the material on Java threads at http://java.sun.com.
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:
- Email to me a copy of your source code.
I may ask for a demonstration of your program.
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:
- Accept a command line parameter which is the number of users
(i.e., threads)
that the program will handle. Ensure that the parameter is a
valid integer between 1 and 4. If the command line parameter is
invalid, print out a usage message and exit.
- For the purpose of this exercise, the commands that each "user"
will enter will come from a data file in the current directory.
The main program should open a file for each user, from 1 up to
4. You may hardcode the filenames for this exercise, if you
wish, or you may specify the filenames at run time with
command-line arguments. Do not put prompts into your
program. The filenames should default to
"file1.dat", "file2.dat", "file3.dat", and "file4.dat".
Sample data files are located at
file1.dat,
file2.dat,
file3.dat, and
file4.dat.
The main program should open the files before any thread
operations are performed.
- Main should declare an initial balance for the account
of $1000.
- The main program should create a thread for each "user". Pass as
a parameter to the thread a thread identifier (1, 2, 3, or 4).
You may also optionally pass as a parameter the open file pointer
of the file that
represents the commands that the thread will execute. Thread 1
should read the first file specified, thread 2 should read the
second file, and so on.
- The main program should wait for the user threads to complete
before it exits. Before exiting it should close the open files.
- The final account balance should be printed before main exits.
-
After you have the basic program running, 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. To do this part you should use condition variables (using
pthread_cond_init, pthread_cond_lock, etc.) useful
for this exercise. A correct solution will not
hang indefinitely.
The code for the user thread must read from the file
according to its thread identifier.
Until all lines
in the file are read, the user thread will read the next command
and perform
it. Valid commands
are to ADD amount, SUB amount, and GET a balance. The
commands should do the following:
- ADD amount should add the amount to the account balance.
- SUB amount should subtract the amount from the account balance.
- GET should display the current account balance. Sample ouput is:
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 10 points.