New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Issue using times function in Intel MAC


Post Reply New Thread Subscribe

 
Thread Tools
sjain
Guest
 
Posts: n/a

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
QUOTE Thanks
sjain
Guest
 
Posts: n/a

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
QUOTE Thanks
cazabam

 
cazabam's Avatar
 
Member Since: Jun 06, 2006
Posts: 1,153
cazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of light
Mac Specs: MacBook 2.0GHz White, 512MB RAM, 60GB HDD

cazabam is offline
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.
QUOTE Thanks
sjain
Guest
 
Posts: n/a

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
QUOTE Thanks
avcabob

 
avcabob's Avatar
 
Member Since: Sep 21, 2004
Posts: 199
avcabob is a jewel in the roughavcabob is a jewel in the roughavcabob is a jewel in the rough
Mac Specs: MacBook Pro 15" 2.16Ghz Core Duo 1GB Ram 80GB

avcabob is offline
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.
QUOTE Thanks
cazabam

 
cazabam's Avatar
 
Member Since: Jun 06, 2006
Posts: 1,153
cazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of light
Mac Specs: MacBook 2.0GHz White, 512MB RAM, 60GB HDD

cazabam is offline
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.
QUOTE Thanks
sjain
Guest
 
Posts: n/a

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
QUOTE Thanks
cazabam

 
cazabam's Avatar
 
Member Since: Jun 06, 2006
Posts: 1,153
cazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of light
Mac Specs: MacBook 2.0GHz White, 512MB RAM, 60GB HDD

cazabam is offline
Happy to help
QUOTE Thanks

Post Reply New Thread Subscribe


« gcc for 10.3 Panther | Wrapping a .Jar in a .App »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Hacker Upgraded Intel Mac Mini with Merom/Core 2 Duo Chip techster82 Apple Rumors and Reports 16 06-15-2006 07:40 PM
Intel Mac Mini in for service bowadoyle Apple Desktops 7 06-10-2006 10:10 PM
Mac Mini - Is it worth waiting for Intel? y2kt Apple Desktops 2 02-13-2006 02:39 PM
To Swich or Not To Swich, That is the Question EDIT-XTREEM Switcher Hangout 5 01-26-2006 04:28 PM
Mac Users Get More ISP Choices schweb Apple Rumors and Reports 1 03-06-2003 10:57 AM

All times are GMT -4. The time now is 12:19 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?