Issue using times function in Intel MAC

S

sjain

Guest
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
 
OP
S

sjain

Guest
!!!

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
-Sachin
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's Specs
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.
 
OP
S

sjain

Guest
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.
Thanks
-Sachin
 
Joined
Sep 21, 2004
Messages
199
Reaction score
20
Points
18
Your Mac's Specs
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.
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's Specs
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:

Code:
// 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:

Code:
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.
 
OP
S

sjain

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

I really needed this to work badly...

God bless!

-Sachin
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's Specs
MacBook 2.0GHz White, 512MB RAM, 60GB HDD
Happy to help :)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top