--- freak_creeper21 <[EMAIL PROTECTED]>
wrote:

> hi, i wrote a program... but there is errors
> encountered... but i 
> cant seem to solve... any kind souls can help me
> woth the program... 
> thanx...
> 
> #include <stdio.h>
> #include <pthread.h>
> 
> #define DATA_SIZE 8000
> #define NUM_THREADS 8
> 
> int result_max;
> int result_min;
> 
> /* need long integer to hold so as to prevent
> overflow(in case) */
> long result_sum;
> int mark[DATA_SIZE];
> 
> /* compute max, min and total sum */
> void *Compute(void *i)
> {
>       int startIndex; /* starting index of i-th partition
> of 1000 
> size */
>       int j;
>       int max;
>       int min;
>       long sum;
> 
>       startIndex = (DATA_SIZE/NUM_THREADS) * ((int)i);
> 
>       sum = mark[startIndex];
>       max = mark[startIndex];
>       min = mark[startIndex];
> 
>       for (j=startIndex; j < startIndex +
> (DATA_SIZE/NUM_THREADS); 
> j++)
>       {
>           sum = sum + mark[j];
>           if (mark[j] > max) 
>                   max = mark[j];
>           if (mark[j] < min) 
>                   min = mark[j];
>       }
>     
>       /* update total sum, max, min */
>       result_sum = result_sum + sum;
>       if (max > result_max)
>       {
>               result_max = max;
>       }
>       if (min < result_min)
>       {
>               result_min = min;
>       }
>       /* terminate worker thread */
>       pthread_exit(NULL);
> }
> 
> 
> int main()
> {
>       pthread_t threads[NUM_THREADS]; /* declare array of
> 8 working 
> threads */
>       pthread_attr_t attr; /* pthread attribute variable
> */
>       int result;
>       int i;
>       int mean;
> 
>       for (i=0; i<DATA_SIZE; i++)
>       {
>         /* i+1 as starting mark is 1 instead of 0 */
>         mark[i] = i+1;
>       }
> 
>     /* update result sum, max, min to first mark */
>       result_sum = mark[0];
>       result_max = mark[0];
>       result_min = mark[0];
> 
>       /* initialize the attribute variable */
>     pthread_attr_init(&attr);
>       /* set the thread as joinable */
>     pthread_attr_setdetachstate(&attr,
> PTHREAD_CREATE_JOINABLE);
>       /* create 8 worker threads */
>     for(i = 0; i < NUM_THREADS; i++)
>       {
>               /* creates i-th thread which will execute compute 
> function */
>               result = pthread_create(&threads[i], &attr,
> Compute, 
> (void *)i);
>               /* if creation of thread failed */
>               if (result)
>               {
>                       fprintf(stderr, "Error: pthread_create() 
> failed, thread: %d, return code: %d\n", i, result);
>                       return 1;
>               }
>       }
>       
>       /* Free attribute and wait for the other threads */
>     pthread_attr_destroy(&attr);
>       /* wait for all 8 worker threads created to
> terminate */
>     for(i = 0; i < NUM_THREADS; i++)
>     {
>               /* wait for i-th thread termination */
>         result = pthread_join(threads[i], NULL);
>               /* if joining failed */
>         if (result)
>         {
>                       fprintf(stderr, "Error; pthread_join() 
> failed, thread: %d, return code: %d\n", i, result);
>             return 1;
>         }
>     }
>       
>       /* integer value for mean is just good enough :) */
>       mean = result_sum / DATA_SIZE;
> 
>       printf("min=%d, max=%d, mean=%d\n", result_min,
> result_max, 
> mean);
>       
>       /* terminate main thread */
>       pthread_exit(NULL);
>       return 0;
> }
> 
> 
What are the errors?  Are they compile errors or
runtime errors?  If compile errors, note the error on
the line it is happening.  If runtime, tell us what is
happening and what you expect to happen.

Ray



       
____________________________________________________________________________________
Sick sense of humor? Visit Yahoo! TV's 
Comedy with an Edge to see what's on, when. 
http://tv.yahoo.com/collections/222

Reply via email to