Thread: Thread synchronization Assignment help!!

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    2

    Thread synchronization Assignment help!!

    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.
    Last edited by rossi143; 03-25-2007 at 07:05 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Have you tried to change the open mode of the out file to "w+"?

    2. Why do you open and close it on any write operation? open it once and pass the fd to each thread that should write to file?

    3. I'm not sure you have to lock the write operations, actually - I'm sure the write operations in the multi-threaded library are thread-safe, so you should protect only your own shared memory - the char queue you use...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    2
    Quote Originally Posted by vart
    1. Have you tried to change the open mode of the out file to "w+"?

    2. Why do you open and close it on any write operation? open it once and pass the fd to each thread that should write to file?

    3. I'm not sure you have to lock the write operations, actually - I'm sure the write operations in the multi-threaded library are thread-safe, so you should protect only your own shared memory - the char queue you use...
    Thanks! The problem was with how I opened the copy file for writing...now its resolved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  2. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  5. Thread Synchronization :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-21-2002, 09:27 AM