473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error with threads opening files

5 New Member
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
Feb 28 '10 #1
1 1358
Banfa
9,065 Recognized Expert Moderator Expert
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.
Feb 28 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5006
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
7
4904
by: mattsniderppl | last post by:
Hi, i'm relatively new to C++ from java and am having a difficult time with pointers. I'm sure there is something simple that I am doing wrong, but I can't seem to write this in a way that doesn't...
4
3713
by: Patrick Masson | last post by:
Hello, Our configuration : Apache 2.0.53 PHP 5.0.4 PC Windows 2000 MATLAB 6.1 We work on a consulting project in France which involves MATLAB Web server,
6
2502
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
3
6178
by: Jerry Spence1 | last post by:
I am starting up a number of threads. Each one specifies a connection to the same database. Sub ReaderThread() Dim cn As New System.Data.OleDb.OleDbConnection cn.ConnectionString =...
6
3109
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for...
2
1950
by: Axel Gallus | last post by:
I am desperately looking for some resources dealing with error handling in c++. Don't get me wrong.... I have very detailed knowledge of exceptions, assertions and invariants, but what is...
11
1434
by: Jialiang Ge [MSFT] | last post by:
Hello Peter, I once came across the same error "ExecuteReader requires the command to have a transaction when the connection assigned? with running two threads talking with the database. There...
13
2971
by: bytesc | last post by:
Hey guys, I have created many picture files using gd_library, I recently decided to rename all of my pictures files on my web server to text instead of just numbers. Example: productsssh.jpg ...
2
6336
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.