Thread: clock_getime()

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    clock_getime()

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are including time.h which defines something called time. So you shouldn't reuse that word for your variable name:
    Code:
    int time = clock_gettime(CLOCK_REALTIME, &init);
    If you run just a small program that checks time, does it work there?
    Code:
    #include<stdio.h>
    #include <time.h> 
    #include <sys/time.h>
    int main( void )
    {
        struct timespec init;
        int t = clock_gettime(CLOCK_REALTIME, &init);
        printf( "init.tv_sec: %d\n", init.tv_sec );
        printf( "init.tv_nsec: %d\n", init.tv_nsec );
    
        return 0;
    }
    Do you get the same thing for each clock type?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Ok. Got it fixed! Thanks for pointing out the time name issue; I hadn't realized I did that. The problem was in my print statements. I was using %f or %lf think of printing it as a float, changing it to %d gave me the times it was storing. Your example for the small program lead me to that. You saved me a lot of frustration! One day I'll learn the problem is never where I am so sure it is.

    Thanks,
    ~TS

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should turn up the warning levels on your compiler, it will help catch things like that.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Tags for this Thread