Error with threads opening files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hjazz
    New Member
    • Nov 2009
    • 5

    #1

    Error with threads opening files

    Hi all,

    I'm using C and pthread on a Linux machine, and I'm having trouble parallelizing a program.

    I'm basically trying to take in a folder of data files, divide them into groups, each group handled by a thread, and run a function on each of the data file.

    The way I'm doing this is I have a global char **filename variable, where filename[i] = filename of a data file. In the main function, I'll read in the filenames of all the data files (minus "." and "..") using scandir and put them in the filename variable. Then 4 (arbitrary number) threads are created each calling the Process function. In Process(), each thread only opens (using a FILE *fin declared in Process()) and works on a portion of the data files using a start_index and an end_index. For example, if there are 100 files, then each thread will handle filename[0] to filename[24], filename[25] to filename[49], filename[50] to filename[74] and filename[75] to filename[99] respectively. After they're done, there is a pthread_join in main() for all 4 threads.

    I have checked that the filenames have been stored correctly, both in main() and Process(). However, I keep getting segmentation fault here, in Process():

    for (i = start_index, i <= end_index ; i++)
    fin = fopen(filename[i], "rb"); <--- Seg fault

    I don't really get why there should be an error since none of the threads are trying to open the same file.

    Please advise.

    Thank you.

    Regards,
    Rayne
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Hmmm it is probably not a good idea to access the same object, filename, from 4 separate threads. However if you have finished writing to the object by the time you start the threads then I would be slightly surprised if that was actually causing your problem.

    Eliminating that all you are left with is that filename has no been correctly initialised but since you haven't posted that code it is hard to tell.

    Comment

    Working...