Homework 1
Due Date
April 11th 2005: 5:00PM
Description
You will implement a client-server system which operates a bank.
There will be a single server which will maintain a single account.
Intially the account balance is 0. There can be many clients who can
request service simultaneously from this server. The services are of
three types:
Deposit : The client might make a deposit into the account and
the account will be credited that much amount. The deposit must be
a positive floating point number. The server will acknowledge
successful deposit by returning the current balance to the client.
Withdraw : The client can make a withdrawal of some positive
floating amount. The account will be debited that much amount. If
the account does not have sufficient balance, then an error should
be returned to the client, otherwise the current balance is
returned.
Balance : The client can ask for the balance and the
server should reply with the current account balance.
Requirements
All code must be written in C. Network communication will use
reliable TCP sockets.
Server
The
server will be called server and invoked as follows
$ server -p port -d delay
The arguments can be specified in any order. To parse arguments, use
getopt. See man 3 getopt for more details. By default if
port is not specified then server will listen on port 8000. The delay
is an integer which specifies the time in milliseconds required to
complete the deposit/withdraw/balance operations. You will be holding
the mutex lock during this delay. By default the
delay is 100msec. Use usleep to implement delay (see man 3
usleep ) for details.
Client
The client will be called client and invoked as follows:
$ client -h servername -p port -d deposit -w withdraw -b
The servername specifies which computer is running the server and is
compulsory. If port is not specified, use port 8000. Only one of -d,
-w and -b options can be specified. After the client is done, it
should print the balance returned by the server on stdout or -1
if an error occurred.
Other Issues
- All code must be written in C on the csil machines. We shall
compile and run code there. If you do not have an account there, then
please go to the CS office and get an account.
- For a tutorial on sockets see Socket
client server tutorial from RPI. For more useful info, also see
man 7 socket, man 7 ip
- Sun Microsystems has an excellent multithreaded
programming guide , which discusses multithreaded programming
using pthreads and Solaris native threads. The pthread interface for
linux is described in the man pages as well.
- Floating point and integer representations differ from
architecture to architecture. So you can't just transmit the floating
point number through the network without converting it into a machine
independent format. The simplest and most inefficient way is to write
it out into a char string and send the char buffer. A better and more
compact way is to use Sun's External Data Representation standard.
See man xdr for details. The choice is yours.
- For every request by a client, the server must spawn a new thread
to handle it. To make sure that the account balance is always
consistent, the balance must be protected with a mutex lock. Remember
that the balance operation requires a read lock on the account
balance, while deposit and withdraw operations require a write lock.
Since we want the implementation to have high levels of concurrency,
you will have to use reader-writer locks. pthreads do have a reader
writer lock interface, but as of now, linux doesn't seem to have an
implementation of them. Most OS books tell you how to implement
reader-writer locks using simple mutex locks.
If you are lazy, you can use the pthread read-write locks. To do this
either add the line
#define _GNU_SOURCE
at the top of your file before
including any headers or add -D_GNU_SOURCE
to the C compiler options.
gcc -D_GNU_SOURCE my.c -lpthread
The pthread read write locks are documented in the Sun multithreaded programming guide mentioned above.