Programming homework 4: Threads and Locking
Due Date: Thursday, April 30
You are to do this project BY YOURSELF.
This project must be implemented in C.
Part 1: Timing
You should write a simple program to measure the performance difference of creating a process as compared to creating a thread. This program, which you should name "threadperf", takes two parameters: the letter 't' or 'p' (for creating threads or processes) the number of threads or processes to cerate.
The program should execute a loop that (a) creates a thread with pthread_create() or a process with fork() and (b) waits for the thread/process to finish with pthread_join() or wait().
You should turn in this program with a readme file that includes measurements of the performance difference. This can be on any machine, including your private machines. The readme should state what kind of computer it is, how many processors it has, and how long the test takes for one million threads and one million processes. You can time your program with the Unix time command-line utility.
Part 2: Locking
You will be implementing a simple program to keep track of bank balances. Your program will simulate a random set of deposits and withdrawls.
Your program will read from the command line:
Your program will then start separate threads. Each one will generate a random number between [- max transaction size, + max transaction size] with the c srandom() function and the supplied random seed. If the number is negative, it is a withdrawal and your program must make sure the current balance is high enough. If the number is positive, it is a deposit and is always safe. Your program should acquire a lock, perform the transaction, then release the lock.
Once a thread has performed its transactions, it should exit. The program should terminate when all the thread have completed by printing out the final bank balance.
Your program should be named "bank". It should take the six parameters identified above, all as integers. The output of the program should be a single line:
Final bank balance: <balance>
Compiling a multi-threaded program with POSIX threads on Linux requires two things. First, you need to include the pthread header file pthread.h in your code. Second, when compiling, you need to link with the pthread library -lpthread . That's about it.
What to turn in
Please turn in your source code and a makefile that builds both programs to ~cs537-1/handin/<your login>/p4.