maximum of an array using threads ?? (a shout for help)

Reply

Join Date: Apr 2008
Posts: 6
Reputation: loly is an unknown quantity at this point 
Solved Threads: 0
loly's Avatar
loly loly is offline Offline
Newbie Poster

maximum of an array using threads ?? (a shout for help)

 
0
  #1
Apr 13th, 2008
hey guys .. ausome forum ... i know the basics of c programming but i am going on to the next level and i am having some diffculties ..with multi-threading ...

theres this program i read but i couldnt solve ,, hope some one here knows how to help me..

write a c program that computes the maximum of an array of integers using both the sequential fashion ,, & then again using multi-threads ,, the reasult must be the same.

the program takes 2 command-line arguments : an array size & the number of threads.
the program allocates an integer array of the given size and filles it with random integers.
the array should be split "logiclly" amoung threads "as equally as possible.
the prgram should output the value of the maximum element of the array through sequential fashion and through the threads and checks both results match,,,


i would truely be greatful if u even give me some pointers on how to devide the threads,,
thank u,,
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #2
Apr 13th, 2008
Post what you have done so far to resolve this. What compiler and operating system are you using, because it will make a difference how you create the threads.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: loly is an unknown quantity at this point 
Solved Threads: 0
loly's Avatar
loly loly is offline Offline
Newbie Poster

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #3
Apr 15th, 2008
well what i did so far is iv created the array and found the max and iv created the threads but i cant seem to figure the max function for the threads them selves
& im working on a linux o.s
thanx for replaying
Last edited by loly; Apr 15th, 2008 at 10:24 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #4
Apr 15th, 2008
I still can't see the code you wrote. If you want us to help you then you have to post code because we can't just guess what you did.
Last edited by Ancient Dragon; Apr 15th, 2008 at 10:29 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: loly is an unknown quantity at this point 
Solved Threads: 0
loly's Avatar
loly loly is offline Offline
Newbie Poster

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #5
Apr 15th, 2008
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<time.h>
  4. #include <pthread.h>
  5. void *thread_function(void *);
  6.  
  7. int main(void)
  8. {
  9.  
  10. srand(time(0);
  11.  
  12. int i , asize , max1,Num_Threads;
  13.  
  14.  
  15. printf("please enter the size of your array:");
  16. scanf("%d",&asize);
  17. printf("please enter how many threads u want:");
  18. scanf("%d",&Num_Threads);
  19.  
  20.  
  21. int a1[asize];
  22. Pthread_t threads[Num_Threads];
  23.  
  24. for (i = 0 ; i < Num_Threads ; i++){
  25. pthread_creat(&threads[i], NULL, thread_function, NULL );
  26. }
  27.  
  28. for (i = 0 ; i < Num_Threads ; i++){
  29. pthread_join (threads[i], NULL);
  30. }
  31.  
  32. for (i=0; i<asize; i++)
  33. {
  34. a1[i]= rand()%9;
  35.  
  36. }
  37.  
  38. max1=a1[0];
  39.  
  40. for (i=1; i<asize; i++)
  41. if (max1 < a1[i]){
  42. max1=a1[i];
  43. printf(" the max is %d",max1);
  44. }
  45. return 0;
  46. }

that is as far as i can go as u see im realy lausy with threads..

my questions are ...how can u do the max using threads ,, and how so i devide the array on threads
Last edited by loly; Apr 15th, 2008 at 11:10 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #6
Apr 15th, 2008
Like so
  1. pthread_creat(&threads[0], NULL, thread_function, &a1[0] );
  2. pthread_creat(&threads[1], NULL, thread_function, &a1[5] );

And
  1. void *thread_function(void *p) {
  2. int *arraySlice = p;
  3. }

What I haven't done (that's your thing to think about) is how you also convey the length of each array slice to each instance of the thread. If all the slices are the same size, that might make it easier.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: loly is an unknown quantity at this point 
Solved Threads: 0
loly's Avatar
loly loly is offline Offline
Newbie Poster

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #7
Apr 15th, 2008
hmmmmm,,,
first of all marry me salem i sware ull learn how to love me ,, 55555 thank u u gave me an idea
how about i slice the array like so asize/Num_Threads.
i know im gana do a for loop (this gurl is endlessly looping her whole program is a one big loop)
so the for loop goes like this
static int j=0
for (i=0;i<asize/Num_Threads && j<asize;i++,j++)

the problem is ,, again i dont know how to call the thread funnction ..

but anyway thanx for the tips.

p.s luv the signature.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: loly is an unknown quantity at this point 
Solved Threads: 0
loly's Avatar
loly loly is offline Offline
Newbie Poster

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #8
Apr 15th, 2008
The pthread_create() routine permits the programmer to pass one argument to the thread start routine. For cases where multiple arguments must be passed, this limitation is easily overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create() routine.

the array isnt global and its realy geting blurry
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #9
Apr 16th, 2008
don't make the array global. declare it in main.


i also think you've got a typo in the function name ... it's "pthread_create". you probably figured that out i'm sure, but just in case...


.
Last edited by jephthah; Apr 16th, 2008 at 1:19 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: maximum of an array using threads ?? (a shout for help)

 
0
  #10
Apr 16th, 2008
Well you managed to find the bit about passing a pointer to a struct, so I think you have everything you need to make it happen.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC