07-29-2006, 03:26 AM #1sjainGuestIssue using times function in Intel MAC
I am getting this strange error on Intel MAC OS whereever i use
I am using the following piece of code:
1 clock_t myTime;
2 myTime = times(NULL);
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
07-29-2006, 03:29 AM #2sjainGuest!!!
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
07-29-2006, 07:24 AM #3
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.
07-31-2006, 12:39 AM #4sjainGuest
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:
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.
07-31-2006, 01:02 AM #5
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.
07-31-2006, 05:25 AM #6
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);
Output from times(): 3764629764 Exclude usec portion: 3764629704 Include usec portion: 3764629764
07-31-2006, 09:17 AM #7sjainGuest
Thanks a lot Cazabam. Thanks a lot. The implmentation you have provided works perfectly fine.
I really needed this to work badly...
07-31-2006, 09:30 AM #8
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
By navylord in forum Switcher HangoutReplies: 6Last Post: 11-28-2014, 12:30 AM
By ankur17 in forum OS X - Development and DarwinReplies: 12Last Post: 06-02-2011, 04:54 PM
By Vimal in forum Community Suggestions and FeedbackReplies: 0Last Post: 07-09-2007, 02:49 AM
By sjain in forum OS X - Operating SystemReplies: 0Last Post: 07-26-2006, 07:28 AM