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.