Results 1 to 8 of 8
  1. #1
    Unhappy Issue using times function in Intel MAC
    Hi All,

    I am getting this strange error on Intel MAC OS whereever i use
    times(NULL) function.

    I am using the following piece of code:

    #include <time.h>
    int main()
    1 clock_t myTime;
    2 myTime = times(NULL);
    3 return(0);

    The progarm compiles sucessfully, but on running the application, it throws the following error at line 2:
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
    0x900a3fb2 in times ()

    Can anybody help me out with this problem. This seems to be Intel MAC specific. I dont face any issue with this piece of code on any other unix based system.

    Thanks a lot in advance

  2. #2
    I Also understand that this function is obselete since 10.4.x MAC and has been replaced by gettimeofday and getrusage. However, gettimeofday returns the time in seconds, whereas i need the output of current time in clock ticks as given by times() function.

    I really need an answer to this one soon.

    All help is highly appreciated.

    Thanks in advance

  3. #3

    cazabam's Avatar
    Member Since
    Jun 06, 2006
    MacBook 2.0GHz White, 512MB RAM, 60GB HDD
    If you call sysconf with the _SC_CLK_TCK request, you can find out your clock ticks per second. Take the seconds returned by gettimeofday and multiply it up.

  4. #4
    Thanks for the response Cazabam.
    However, I did exactly the same before seeking help in this forum.

    But i have one issue regarding it. Kindly do correct if i am wrong in my assumption.

    I tried the following on linux where i have times function running fine:

    gettimeofday(&tp, NULL);
    clocks_per_second = sysconf(_SC_CLK_TCK);
    cpu_time = (tp.tv_sec * CLOCKS_PER_SEC);

    tm = times(NULL);

    However the output of cpu_time and tm is not the same. Isnt it supposed to be same if even times function does the same. The 2 values are coming out to be quite different, which made me dubious if this is the right way of calculating the value of time in clock ticks.

    Hoping for a quick early response.

  5. #5

    avcabob's Avatar
    Member Since
    Sep 21, 2004
    MacBook Pro 15" 2.16Ghz Core Duo 1GB Ram 80GB
    I'm not that great of a programmer, but wouldn't the difference in time be cause by the fact that you are getting the time, doing calculations on it, getting the time again, and then comparing the two? So wouldn't the time between the two calls for the current time cause a difference in it value returned? Unless I misunderstood what you are trying to do, which is very possible.

  6. #6

    cazabam's Avatar
    Member Since
    Jun 06, 2006
    MacBook 2.0GHz White, 512MB RAM, 60GB HDD
    avcabob has a point, that the two are several clock ticks apart (possibly several hundred, depending on what comes back from sysconf. How different are the two numbers that are being returned?

    However, the immediate problem I see with your implementation is that you have not taken into account the microsecond count. This can account for a difference of up to 1 second of clock ticks. Try this instead:

    // Get the output from the times() function for comparison
    clock_t clk;
    struct tms tm;
    clk = times(&tm);
    printf("Output from times(): %u\n", clk);
    // Get the time in seconds
    struct timeval tv;
    gettimeofday(&tv, NULL);
    // ... and the ticks per sec
    long ticks_per_sec = sysconf(_SC_CLK_TCK);
    // The whole seconds ...
    unsigned long ticks = tv.tv_sec * ticks_per_sec;
    printf("Exclude usec portion: %u\n", ticks);
    // ... and the microsecond portion
    ticks += (ticks_per_sec * tv.tv_usec) / 1000000;
    printf("Include usec portion: %u\n", ticks);
    On my machine, this gives the following output:

    Output from times(): 3764629764
    Exclude usec portion: 3764629704
    Include usec portion: 3764629764
    As you can see, adding the microsecond portion brought the values together. Of course, it may be a clock tick or two out depending on what else it happening.

  7. #7
    Thanks a lot Cazabam. Thanks a lot. The implmentation you have provided works perfectly fine.

    I really needed this to work badly...

    God bless!


  8. #8

    cazabam's Avatar
    Member Since
    Jun 06, 2006
    MacBook 2.0GHz White, 512MB RAM, 60GB HDD
    Happy to help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. What is the Mac function of this windows function?
    By navylord in forum Switcher Hangout
    Replies: 6
    Last Post: 11-28-2014, 12:30 AM
  2. Problem with times() function
    By ankur17 in forum OS X - Development and Darwin
    Replies: 12
    Last Post: 06-02-2011, 04:54 PM
  3. Javascript function call issue
    By Vimal in forum Community Suggestions and Feedback
    Replies: 0
    Last Post: 07-09-2007, 02:49 AM
  4. Issue using times function in Intel MAC
    By sjain in forum OS X - Operating System
    Replies: 0
    Last Post: 07-26-2006, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts