Hello! I am a university student and need help completing an assignment about thread synchronization in C. The assignment is basically about a program that copies a given text file to another text file (character by character) using threads. The program has a circular array that stores a certain number of the struct:
typedef struct{
char data;
off_t offset;
}BufferItem;
It also has two sets of threads: IN threads and OUT threads. The purpose of IN threads are to read from the source file a character and its offset and then store the information in the array using the struct BufferItem. The OUT thread on ther hand reads from the array and writes the stored character into the target/copy file using its corresponding offset.
Upon creation both threads go to sleep for a random time between 0 and 0.1 sec.
Along the way, each thread writes some information to two log files, so that their activities could be traced. Since the threads would reading and writing on a shared memory, thread synchronization is required. Therefore the IN threads have three critical sections where they cannot be preempted and thus have CPU guaranteed, this is achieved using mutex locks. The same is the case for OUT threads.
The above program(copy.c) will be compiled with:
%cc -Wall -o cpy copy.c -lpthread
It will be invoked as follows:
cpy <nIn> <nOut> <file> <copy> <bufSize><IN_Log><OUT_log>
<nIn> is the number of IN threads to create. There should be atleast 1.
<nOut> is the number of OUT threads to create. There should be atleast 1.
<file> is the pathname of the file to be copied. It should exist and readable.
<copy> is the name to be given to the copy. If a file with that name already exists, it should be overwritten.
<bufSize> is the capacity, in terms of BufferItem's, of the shared array/buffer. It should be atleast 1.
<IN_Log> the IN threads write some trace information to this file. If a file with that name already exists, it should be overwritten.
<OUT_Log> the OUT threads write some trace information to this file.If a file with that name already exists it should be overwritten.
-------------------------------------------------------------------------------------------------------------------
I have completed almost 90% of the assignment, the only part I am stuck in is how to write to the copy file using the exact offsets from the source file. I have used fseeko(...) but still my program just writes to the copy file sequentially what the threads read from the buffer, ignoring the offset values hence the copy file is not an exact copy of the source file.