#include <pthread.h> 
#define NUM_THREADS     8
//#define _GNU_SOURCE
#include <sys/time.h>
#include <stdio.h>      /* standard I/O                */
#include <stdlib.h>     /* for exit   - 1 occurrence   */
#include <unistd.h>



double ge0,ge1,lsalu;
struct timeval starttime,endtime;




void *BusyWork(void *null)
{
//   int i;
/*   double r=0.0;
   for (i=0; i<1000000; i++)
   {
     r = r + 1;
(double)random();
   }
   printf("r = %e\n",r);
*/

   system("aludir/lsalu");
   pthread_exit((void *) 0);
}

int main (int argc, char *argv[])
{
   pthread_t thread[NUM_THREADS];
   pthread_attr_t attr;
   int a, t, s;

   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 gettimeofday(&starttime, NULL);

   for(t=0; t<NUM_THREADS; t++)
   {
      printf("Creating thread %d\n", t);
      a = pthread_create(&thread[t], &attr, BusyWork, NULL); 
    /*  if (a)
      {
         printf("ERROR; return code from pthread_create is %d\n", a);
         exit(-1);
      }*/
   }

   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);
   for(t=0; t<NUM_THREADS; t++)
   {
      a = pthread_join(thread[t], (void **)&s);
    /*  if (a)
      {
         printf("ERROR; return code from pthread_join is %d\n", a);
         exit(-1);
      }*/
      printf("Completed join with thread %d s= %d\n",t, s);
   }


        gettimeofday(&endtime, NULL);  
        ge0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
        lsalu = ge0/NUM_THREADS;
        printf("\n\n\tfinished.\n");
        printf("\nLSBENCH Results:");
        printf("\n\tALU index:              %lf\n", lsalu);
   pthread_exit(NULL);
}
