Hi.
I am working on a project for a multi-threaded prime number generator. All is working good except for one piece. I need to get the time for my program to generate my prime list. It is suggested in the assignment to use clock_gettime. But all I get is zeros when I get the values stored in the timespec struct. Any hints as to which clock_id I should be using? CLOCK_REALTIME is the only one so far that the compiler doesn't have a hissy fit over.
Code:
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
void *primeGenerator();
float calcTime(struct timespec *,struct timespec *);
int highestChecked;
int maxNum;
int main(int arg, char *argv[])
{
int i;
highestChecked=0;
while(1)
{
if(argv[1]==NULL)
{
printf("Invalid parameters.\n");
exit(1);
}
if(argv[2]==NULL)
{
printf("Invalid parameters.\n");
exit(1);
}
else
{
struct timespec init;
int time = clock_gettime(CLOCK_REALTIME, &init);
if(time !=0) printf("Time Calculation Bad. Error in call to gettime\n");
maxNum = atoi(argv[1]);
printf("Max number: %d\n", maxNum);
int numThreads = atoi(argv[2]);
printf("Numthreads %d\n",numThreads);
pthread_t tid[numThreads];
pthread_attr_t attr;
pthread_attr_init(&attr);
//Creates the numThreads threads
for(i=0; i<numThreads; i++) pthread_create(&tid[i],&attr,primeGenerator, NULL);
//Join the threads
for(i=0; i<numThreads; i++) pthread_join(tid[i], NULL);
struct timespec final;
time = clock_gettime(CLOCK_REALTIME, &final);
if(time !=0) printf("Time Calculation Bad. Error in call to gettime\n");
fprintf(stderr, "%d,%d\n", maxNum, numThreads);
//Testers
printf("Sec: %f\n", init.tv_sec);
printf("NanSec: %lf\n", init.tv_nsec);
printf("End Sec: %f\n", final.tv_sec);
printf("End NanSec: %f\n", init.tv_nsec);
break;
}
}
return 0;
}
I get all zeros in all cases from the ending print statements.
Any hints would appreciated.
Thanks.