You will need to do two versions of this project, one on Unix and one on Windows, but it's a pretty simple project which involves threads and mutual exclusion.
Write a program which will simulate updating a database by multiple concurrent threads.. The number of threads will be passed in as an argument. Each thread will open a file containing data indicating what that thread will do. The database will always contain 6 records, numbered 1 .. 6. Only one thread at a time can be updating a particular record, but different threads can be updating different records at the same time.
Each line in the data file has two digits, the number of the record to be updated (1..6) and the time in seconds that it will take to update that record.
For example, here is a short data file
4 2
3 3
1 5
4 1
This thread would spend two seconds updating record 4, then three seconds updating record 3, then five seconds updating record 1, then one second updating record 4. If it tries to update a record which is being updated by another thread, it has to wait until that thread completes its update.
Any one thread can only be performing one update at a time, and the updates for each thread must be done in the order specified in the file.
You should control the time with the sleep() function in Unix and the Sleep() function in Win32.
All threads should should write to a file called log.txt. Three kinds of events should be recorded, wanting to update a record, starting to update a record, and finishing the update of a record. The output must look exactly like this.
Thread n wants to update record x at time t Thread n starts to update record x at time t Thread n is finished updating record x at time tThe time values will be in seconds starting from the start of the program. Times should be correct to two decimal places.
The threads will be numbered starting at 1. Thread 1 will open a file called 1.txt, thread 2 will open a file called 2.txt etc.
Each thread should return two values, the total time spent updating (this should be the sum of the second values of each pair in the file) and the total run time, which should be the update time plus the waiting time. These have to be returned by passing a pointer to a structure, you cannot use global variables for this. The main program (not the thread) should print out these two values like this:
Thread n spent t1 time updating. Total time was t2.A program called CheckResults will be provided for you.. This program will read your output file and will confirm that mutual exclusion was maintained.
To time a program, use the clock() function.
#include <time.h> clock_t clock() /* a clock_t is a long int */
The first time that clock() is called, it returns zero but
starts a clock running. Each subsequent call to clock()
returns the time since the clock was started. The result is in
microseconds 1. To convert to seconds, divide the result
by the symbolic constant CLOCKS_PER_SEC (defined in time.h). Both of these are
integers, so to avoid truncation, convert the result to a floating
point number with casting. This works for both Unix and Win32.
Grading:
Each project will be graded independently
| Compiles (sincere attempt) | 40% |
| Create threads correctly | 10% |
| Handles Mutual Exclusion correctly | 20% |
| Writes to the log file correctly | 10% |
| Threads return appropriate values | 10% |
| Programming style, commenting | 10% |
The program is due at 11:59PM on Friday, Oct 24