| | |
maximum of an array using threads ?? (a shout for help)
![]() |
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,,
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,,
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.
C Syntax (Toggle Plain Text)
#include<stdlib.h> #include<stdio.h> #include<time.h> #include <pthread.h> void *thread_function(void *); int main(void) { srand(time(0); int i , asize , max1,Num_Threads; printf("please enter the size of your array:"); scanf("%d",&asize); printf("please enter how many threads u want:"); scanf("%d",&Num_Threads); int a1[asize]; Pthread_t threads[Num_Threads]; for (i = 0 ; i < Num_Threads ; i++){ pthread_creat(&threads[i], NULL, thread_function, NULL ); } for (i = 0 ; i < Num_Threads ; i++){ pthread_join (threads[i], NULL); } for (i=0; i<asize; i++) { a1[i]= rand()%9; } max1=a1[0]; for (i=1; i<asize; i++) if (max1 < a1[i]){ max1=a1[i]; printf(" the max is %d",max1); } return 0; }
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.
Like so
And
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.
C Syntax (Toggle Plain Text)
pthread_creat(&threads[0], NULL, thread_function, &a1[0] ); pthread_creat(&threads[1], NULL, thread_function, &a1[5] );
And
C Syntax (Toggle Plain Text)
void *thread_function(void *p) { int *arraySlice = p; }
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.
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.
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.
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
the array isnt global and its realy geting blurry
![]() |
Other Threads in the C Forum
- Previous Thread: text program help needed
- Next Thread: Problem with struct i think
| Thread Tools | Search this Thread |
* ansi array asterisks binarysearch calculate changingto char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database feet fflush fgets file floatingpointvalidation fork forloop function givemetehcodez global grade gtkwinlinux hacking histogram homework i/o inches infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate looping loopinsideloop. lowest match meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf segmentationfault sequential single socket socketprograming socketprogramming standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windowsapi







thanx for replaying