![]() |
[C] Problems with threading
So, I'm taking a course on Operating Systems, and just got a (relatively simple) project to complete. Catch is, it's in C, and I don't know C, and I have to use Unix, which I'm catching on to, at least. I know Java and Ada. The task is simple: I need to take the program supplied, and make it threaded. At least 3 threads need to be running concurrently. Anyway.. here is the code I get:
Code:
/* Programming Project #1 Code:
struct ranges I get a quick Seg Fault after running my program. I know it makes it to creating the threads, but never past creating all 4 threads, so the problem lies somewhere in there. Keep in mind I know nothing about C. My first reaction to the Seg Fault was "Well, maybe final_result is being used before all threads have returned", but, pthread_join is supposed to wait until threads have finished before going on, right? [edit]I'm almost certain now that my threads dont work right. Thinking I should add a field to my structs.. Or perhaps that this isn't where the threads should go in the first place.. :confused: [edit2]: Updated my attempt at code. Refer to my reply down the page.. |
Typically in threads, you don't attempt to pass values back in the pthread_join commands, what you typically do is you have them write their results into a shared structure. Otherwise you are casting things to and from a void * very often.
Other than that, it appears that a majority of your casting is incorrect, and that is what is causing your crash, it is attempting to dereference a pointer of different size. Sorry I can't be of more help, but I just like giving hints and not complete solutions to homework. |
hmm.
when i was in this kind of class, i had to do program a thread simulation from scratch :-X (side note: the exact point of this project of yours has me slightly at a loss, unless i'm missing something) i know nothing about pthreads.h but here's my advice for you on this particular problem: use GDB figure out what line its seg faulting in and see if that helps you any. |
Thanks for the replies.
What is GDB? You're right- the casting was all wrong- I didn't want to do any casting, I just substituted some code for other code, not knowing what I was doing.. got the casting out and lo! It produced some output. Okay. Trying to explain my situation better: The program that is given- I need to make 3 concurrently running threads, that do not share any variables, that can complete this task. (The program computes the number of primes, given by which_prime). My first thought was to split into 3 threads. If n = 100, the first thread computes 1..33, second computes 34..66, last computes 67..100. Okay. But for the second thread to compute the 34th prime number, it needs to know the 33rd prime number, right? Of course, the first thread could return the 33rd, then let the 2nd thread go, but now that's not concurrent. Also, I could use a shared variable to let each start where the last left off, but now they're sharing variables! Actually, here's exactly what the assignment is: http://math-cs.cns.uni.edu/~gray/project01.pdf How can I get around this? I'm beginning to hate this project because it seems not like a "programming project", but a "try to figure this out, and if you can't think outside the box you're screwed".. |
I read through the text, and I didn't see anything about not sharing variables.
But, if you really don't want to share variables, well, I would have each thread create a linked list for each prime number it finds. Add a link to the head of the list to the struct that you pass by reference into the thread functions. Then when all three (or four) threads finish, you will have three linked lists of all of the prime numbers in that area. This however assumes that you know how to handle linked lists in C. |
Yeah, we aren't supposed to share variables, it's under item 3.
Actually, I have a solution. I modified the entire framework quite a bit, so that the function call only returns whether a value is prime or not, and based loops around that. If I knew how to implement a linked list in C, it may be plausable, but this is my first time in C. Thanks for the suggestions, though. Here is my final code- it works, not quite as elegant as it could be since I have to account for small amounts, but I found it a nice solution: Code:
/* Programming Project #1 |
google or do a man on GDB. its very useful.
and i agree that this code is more of a problem solving than a coding project. |
DDD>GDB DDD has a nice front end gui on it. Use that for debugging especially if you haven't used GDB before. Although i'm not sure how well DDD/GDB work with threads.
|
All times are GMT -8. The time now is 01:49 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project