$29
Write a program to put this message back together. Each of the 2 soldiers must be implemented as a thread (hence, 2 threads) and each thread will read its part of the message from a file (thus, there
will be two files ‐ one for each soldier). Each thread will concatentate on to a single global character array forming the final message.
When the message is put back together the main function will output the message to the screen. Therefore, your threads will have to watch for their respective FEOFs to know when to quit. Once both threads have quit the main function will know the message has been put back together.
The trick is to use a critical section control technique (or make one
up) to control which thread gets to append its character onto the
global character array. Note that a strict alternation method will work.
Example: �thisisatest.�
Person1 Person2
‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐
t h
i s
i s
a t
e s
t .
I suggest you test your program with a longer set of data files as we will use a test set that may be up to 1000 characters (500 in each file).
The main program can only set any initial conditions required and call the threads ‐ NOTHING ELSE! If you cheat by having the main program do anything that circumvents the role of the threads you will receive a zero (0).
Your code cannot simply read the data files into an array and seperate them ‐ that would not be a critical section problem. You must read each character (1 at a time) and append them on to the global character array.
This is tricky as your code might work once fine, but not the second time.
TEST YOUR CODE MULTIPLE TIMES!
SUGGESTIONS:
‐‐‐‐‐‐‐‐‐‐‐‐
See the man pages for PTHREAD_CREATE and PTHREAD_EXIT.
Don't forget to include the pthread library when you compile the program (‐lpthread).
REQUIREMENTS:
‐‐‐‐‐‐‐‐‐‐‐‐
The data files WILL be named data1.txt and data2.txt and will be LINUX text files with 1 data character per line (plus a carriage return and line feed).
Your program must run in Streibel 109/115.
Your full name must appear as a comment at the beginning of your program.
Your source code must be named hw4‐yourname.c or hw4‐yourname.cpp
Email your source (subject hw4‐yourname) to rmarsh@cs.und.edu