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,,

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.

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

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.

#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

Like so

pthread_creat(&threads[0], NULL, thread_function, &a1[0] );
pthread_creat(&threads[1], NULL, thread_function, &a1[5] );

And

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.

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 :(

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...


.

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.

This piece of code gives the compilation error like

/tmp/cc8YlrH3.o: In function `main':
checkArray.c:(.text+0xea): undefined reference to `thread_function'
checkArray.c:(.text+0xfa): undefined reference to `pthread_creat'
checkArray.c:(.text+0x128): undefined reference to `pthread_join'

so modify this program as below:

#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include <pthread.h>
void *thread_function(void *arg)
{
         pthread_t pt;

         pt = pthread_self();

         printf("Thread %x ran!\n", (int)pt );

         pthread_exit( NULL );
}

int main()
{

    srand(time(0));

    int i ,j, asize , max1,Num_Threads,ret,status;


    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++){
     ret=pthread_creat(&threads[i], NULL, thread_function, &a1[0] );
     if (ret != 0) {
             printf( "Error creating thread %d\n", (int)threads[i] );
     }
    }

   for (i=0; i<asize/Num_Threads && j<asize; i++,j++){
     ret=pthread_join (threads[i], (void **)&status);
     if (ret != 0) {
             printf( "Error joining thread %d\n", (int)threads[i] );
      } else {
       printf( "Status = %d\n", status );
       }
    }

    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;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
Don't bump old threads
This article has been dead for over six months. Start a new discussion instead.